Pinterest Stumbleupon Whatsapp
Ads by Google

The flexibility of the Raspberry Pi knows no bounds, and just when you think you’ve achieved everything possible, something else comes along. This might be thanks to a great idea you or someone else had, or inspired by a newly released piece of expansion hardware for the device.

One of the first expansions you should buy for the Raspberry Pi Five Great Raspberry Pi Expansions That Make It Even More Useful Five Great Raspberry Pi Expansions That Make It Even More Useful Your Raspberry Pi is versatile and at times amazing, but are you harnessing the full power of that little box? Power-up your Pi with these 5 expansions! Read More is the camera module. With a dedicated connector, the camera can be used for a variety of tasks. Let’s take a look at them.

First: Enable the Camera

Begin by making sure you have connected your Raspberry Pi camera to the mini-computer. Next, boot the device, and log in (we’re assuming you’re using the default Raspberry Pi OS, Raspbian Optimize The Power Of Your Raspberry Pi With Raspbian Optimize The Power Of Your Raspberry Pi With Raspbian As befits a man with too much tech on his hands, I’ve been playing with my Raspberry Pi recently, configuring the device so that it works to its fullest potential. It continues to run as... Read More ). At the command line, enter

sudo raspi-config

In the menu select Enable Camera.

muo-diy-picamera-enable

From here, select Enable, then Finish and Yes to reboot.

Ads by Google

Take A Photo

When your Pi restarts, login again, and at the prompt enter

raspistill –o image.jpg

This will capture your first image, which you will be able to view in the GUI. If you’re not already using Terminal from the GUI, you should switch to this, by using the command

startx

Subsequent commands can be run in Terminal, and the results checked in the Raspbian file manager. You can take as many photos as you like with this command, although note that the filename, image.jpg, will need to be changed with each iteration of the command, to avoid overwriting the previous image.

Let’s get a little more advanced, and instruct the Pi to take a timed photo following a single keypress.

Begin by installing the Python support for the camera.

sudo apt-get install python-picamera python3-picamera

Once done, enter

sudo idle &

This will start the Python environment. Python pops up regularly in Raspberry Pi tutorials, and is a surprisingly easy language to get to grips with. For more help with this, we suggest you check our five best websites for learning Python The 5 Best Websites To Learn Python Programming The 5 Best Websites To Learn Python Programming Over the past decade, the Python programming language has exploded in popularity amongst programmers in all areas of coding. From web developers to video game designers to in-house tool creators, many people have fallen in... Read More , and visit Lynda.com if you’re interested in taking your Python skills further.

Go to File > New Window to open a text editor and enter the following code:

import time

import picamera

with picamera.PiCamera() as camera:

    camera.start_preview()

    time.sleep(0)

    camera.capture('/home/pi/Desktop/image.jpg')

    camera.stop_preview()

Use File > Save to save your work, naming it something like timedsnap.py. When you’re ready to run the script, go to Run > Run Module, or just tap F5.

We can use this same script – with some modifications – to use the Raspberry Pi camera module for other projects.

A PiCamera with a Timer

muo-diy-picamera-device

That same script can be reused with a small tweak to create a camera with a timed countdown, a huge benefit for any selfie-obsessed snappers. Let’s face it, this is a Raspberry Pi, so you can probably find some way of mounting the case and camera on a selfie stick and go out in public with it.

To add a 5 second countdown, change the line

time.sleep(0)

to

time.sleep(5)

When you’re done, remember to save and press F5 to begin the countdown. Say “Cheese!”

Record Video with Your Raspberry Pi Camera

Taking stills is one thing, but what about video? Just as with a smartphone camera or standard desktop webcam (which is essentially what the Pi’s camera is, just without the casing) you can record video too.

In the command prompt, modify the script as follows:

import time

import picamera



with picamera.PiCamera() as camera:

    camera.start_preview()

    camera.start_recording('/home/pi/Desktop/video.h264')

    time.sleep(30)

    camera.stop_recording()

    camera.stop_preview()

You’ll notice I’ve set the time.sleep() value to 30, meaning the script will start recording, wait for 30 seconds, then stop. Save this script as videocapture.py, and press F5 to run.

Notice the use of the camera.start_recording() function. This saves the footage as a file called video.h264, a high definition video clip that you can open from the Raspbian desktop. The best way to do this is to browse to the Desktop folder (or whatever your chosen file path in the above script is), press F4 to open the terminal and enter

omxplayer video.h264

Add a suitable battery for the Raspberry Pi and a display, and you’ve got yourself a compact camcorder!

Time-Lapse Photography

Time-lapse photography 4 Ways To Take Time-Lapse Videos 4 Ways To Take Time-Lapse Videos Time-lapse videos are some of my favorite videos to watch. They’re just so fascinating, whether the video shows the passage of time in a desert or the workflow of a sketch artist. There’s just something... Read More has increased in popularity with the explosion of smartphone cameras in the past few years, making what was once the province of specialist photographers accessible by almost everybody.

The downside of using a smartphone for that sort of photography is obvious; it is time consuming, and hogs a resource that you might need for, well, making and receiving phone calls. The Raspberry Pi with its attached camera makes a good alternative, and with a battery attached can prove just as portable and versatile as an Android or iPhone app, and makes more sense than just using your Pi as a time-lapse trigger for a DSLR How To Capture Time-Lapse Photography With Your Raspberry Pi and DSLR or USB Webcam How To Capture Time-Lapse Photography With Your Raspberry Pi and DSLR or USB Webcam Time-lapse photography can really give you a sense of how the world works on a macroscopic level, beyond anything conceivable to the normal human experience of time. Read More .

Before proceeding, install ffmpeg:

sudo apt-get install ffmpeg

Then, use this Python script to capture the time lapse images:

import time

import picamera

 

VIDEO_DAYS = 1

FRAMES_PER_HOUR = 60

FRAMES = FRAMES_PER_HOUR * 24 * VIDEO_DAYS

 

def capture_frame(frame):

    with picamera.PiCamera() as cam:

        time.sleep(2)

        cam.capture('/home/pi/Desktop/frame%03d.jpg' % frame)

 

# Capture the images

for frame in range(FRAMES):

    # Note the time before the capture

    start = time.time()

    capture_frame(frame)

    # Wait for the next capture. Note that we take into

    # account the length of time it took to capture the

    # image when calculating the delay

    time.sleep(

        int(60 * 60 / FRAMES_PER_HOUR) - (time.time() - start)

)

muo-diy-picamera-timelapsesnaps

You’ve created a collection of images recorded over a 60 minute period with this script. To view the images as a film, compile the images as follows:

ffmpeg -y -f image2 -i /home/pi/Desktop/frame%03d.jpg -r 24 -vcodec libx264 -profile high -preset slow /home/pi/Desktop/timelapse.mp4

You can run the video in your Raspberry Pi with a Terminal command:

omxplayer timelapse.mp4

The video will then be played full screen. It might look something like this…

The Raspberry Pi Security Camera

We’ve previously explored how to build a home webcam security system with your Raspberry Pi Build a Motion Capture Security System Using a Raspberry Pi Build a Motion Capture Security System Using a Raspberry Pi Of the many projects that you can build with the Raspberry Pi, one of the most interesting and permanently useful is the motion capture security system. Read More , with a tutorial that predated widespread availability of the Pi’s dedicated camera. Things have of course changed since then, but you can use the same principles and software to turn the Pi into a far more compact security camera solution. In theory, you can monitor the comings and goings in and out of your house for under $100 using one or more Raspberry Pi security cameras.

We’ve given you five uses for your Raspberry Pi camera module, but we reckon you might be able to add to the list. How do you use yours? Tell us in the comments.

  1. rahul
    August 16, 2016 at 10:44 am

    can i connect my underwater camera's wifi with Raspberry Pi
    If yes how its works

  2. taha azami
    July 9, 2016 at 8:38 pm

    hi, I want to recognize a hole in a steel plate that is moving by raspberry pi.Thank's

  3. Lloyd Hans
    March 31, 2016 at 12:36 pm

    Is there a way to schedule taking a picture based on real-time. For example, commanding the pi to take a pic at exactly 12 o'clock? thank you

    • Freek
      April 8, 2016 at 7:48 pm

      There is the "at" program and there are cronjobs.

  4. Brett
    March 8, 2016 at 12:15 am

    Yep, my bad. It works rather well if the user does it right. Very good article and software. I will use this often. Thank you

    • Christian Cawley
      March 8, 2016 at 8:23 am

      Glad you got it sorted. Enjoy!

  5. Brett
    March 7, 2016 at 11:56 pm

    I am guessing it is something I missed. As silly as this sounds could it be the ffmpeg line of code for compiling the pics into a mp4 should be run from the terminal instead of python?

  6. Brett
    March 7, 2016 at 12:55 am

    I tried the time lapse video with the Raspberry Pi camera module and it works great! Excellent code, thank you for sharing it. I tried the ffmpeg section to compile it into a video and got syntax errors. I am not sure what I am doing wrong as I copied and pasted it. Any suggestions would be appreciated.

    • Christian Cawley
      March 7, 2016 at 8:29 am

      Hmm, not sure why the code for compiling would display errors, unless there have been some updates. Can you give me a specific error are you getting?

Leave a Reply

Your email address will not be published. Required fields are marked *