Set Up a Time-lapse Webcam

Connecting a webcam to your Raspberry Pi is a great first project. The hardware (a USB webcam) is easy to source, and you may have one already. It’s also pretty easy to set up and download the software.

FILE RECORDING

We’re going to connect the webcam to the Raspberry Pi, and use a package called fswebcam to snap images as jpegs. We’ll then create a script that snaps photos on a regular basis and saves them into a folder.

STEP 1

First connect the webcam to a spare USB port on your Raspberry Pi. If you are using a Raspberry Pi Model A with no spare ports, you’ll need a USB hub. The quality of your images will vary depending on your webcam, and typically you’ll get higher quality photos using the Raspberry Pi Camera Module.

STEP 2

You need the fswebcam package to capture images. Enter sudo apt-get install fswebcam in the command line (if you are in LXDE, open terminal and enter the command). You’ll need to enter your admin password. You can learn more about fswebcam on the Firestorm website (http://www.firestorm.cx/fswebcam/).

STEP 3

You can take an image by entering fswebcam followed by the filename you want the image saved as. Enter fswebcam image.jpg to take a photo from your webcam and save it in the home directory. You won’t be able to view this image if you are working in the command line, so enter startx to switch to the desktop.

STEP 4

Open a terminal window and enter ls to view the files in your home directory. You should see image.jpg (highlighted in pink) When using terminal from inside the desktop, you can open files from the command line using xdg-open. Enter xdg-open image.jpg to view the captured image using Image Viewer.

STEP 5

If you find images coming out blank or distorted, you need to set a slight delay to the webcam. Use the -S option to skip a couple of frames before snapping. Enter fswebcam -S 2 image. jpg to skip a couple of frames, then grab the shot.

STEP 6

You should also use the -r option to adjust resolution, and remove the time and date banner using --no-banner. Enter fswebcam -r 1024x768 --no-banner -S 2 image2.jpg to take a shot at a set resolution, with no banner and a slight delay. Now enter xdg-open image2.jpg to view it in Image Viewer.

TIME-LAPSE WEBCAM

Now that you’ve got a webcam program running on your Raspberry Pi, you can script it to run every minute, or every hour. With this script and a webcam you can turn a Raspberry Pi into a time-lapse camera.

STEP 1

We need a directory to store the images and a script to capture them. Enter mkdir webcam to make the directory and nano webcam.sh to create the file. Now enter the following code:
#!/bin/bash
DATE=$(date +”%y-%m-%d_%H_%M”)
fswebcam -r 640x480 --no-banner -S 2 /home/pi/webcam/$DATE.jpg
Press Control+X to exit (enter Y to save the file).

STEP 2

Our script saves images with the time and date in the webcam folder. The #!/bin/bash is known as a shebang and tells the shell to interpret it. The DATE= creates a variable set to the current time and date, which is used when saving the file. You need to make webcam.sh executable though so it can run;
enter chmod 755
webcam.sh.

STEP 3

We have an executable file. Enter ./webcam.sh to run the script (the ./ specifies the current directory) and ls webcam to check the directory for a saved file. Now we need to run this script once a minute. For that we need to edit a special file known as crontab. Enter crontab –e to view this file.

STEP 4

The crontab file opens in nano. Scroll down to the end and enter:


“* * * * * /home/pi/webcam.sh 2>&1”. 

Press Control+X to exit and save the file. It will say installing new crongtab. The webcam now takes a snap every minute and places it in the webcam directory. You can find more information about crontab at www.adminschoice.com/ crontab-quick-reference.

More information