News

Raspberry Pi time-lapse camera

Get beautiful views of the sunset using your Raspberry Pi, a Pi camera and a small Python script

The weather-proof PICE is a great way to get it outdoors.

We love the Raspberry Pi camera. It’s a lovely little piece of kit that is as versatile as the Pi and it doesn’t even take up any of the USB slots. We’ve done a bit of time-lapse photography in the past but that was using a proper camera attached to the Pi – now we’re doing it with just the Pi camera and a lot less code thanks to the picamera Python module.

The camera module has many uses
The camera module has many uses

We need to set up our Pi with a few things before we start with the code, though. We’ll start off with assuming you’ve got a freshly installed Raspbian SD card, so the first thing to do is an apt-get update followed by an apt-get upgrade to make sure your files are up to date. Follow this with an rpi-update to make sure your firmware is also up to date – this step is very important because if you’re using an outdated firmware then the camera won’t work.

The next step is to get the camera enabled. Open up the terminal if you’re on the desktop, or simply write into the command line:

$ sudo raspi-config

Find the Enable Camera option. Press Enter and key over to Enable. Go to Finish but don’t restart straight away as you have some more commands to get through. Install your picamera module using this:

$ sudo apt-get install python-picamera

You also need to install the software that you’re going to use for compiling your images into the finished time-lapse video. We’re using gstreamer to compile the video as it works well with the Raspberry Pi GPU. To get it installed, first add the repo to the sources list with:

$ sudo sh -c ‘echo deb http://vontaene. de/raspbian-updates/ . main >> /etc/apt/ sources.list’

Do an apt-get update and then install a huge selection of packages with the following:

$ sudo apt-get install libgstreamer1.0-0 liborc-0.4-0 gir1.2-gst-plugins-base-1.0 gir1.2-gstreamer-1.0 gstreamer1.0-alsa gstreamer1.0-omx gstreamer1.0- plugins-bad gstreamer1.0-plugins-base gstreamer1.0- plugins-base-apps gstreamer1.0-plugins-good gstreamer1.0-plugins-ugly gstreamer1.0-pulseaudio gstreamer1.0-tools gstreamer1.0-x libgstreamer- plugins-bad1.0-0 libgstreamer-plugins-base1.0-0.

Once that’s done, shut down the Raspberry Pi. Unplug the USB cable and locate the special DSI port for the camera, next to the HDMI port. Gently pull on the edges to lift the fastener and slot in your Raspberry Pi camera ribbon – make sure the silver connectors are facing the HDMI port. Turn your Raspberry Pi back on and get back into Raspbian. Everything should be about ready now, so give the camera a test by opening up the terminal and using:

$ raspistill -o test.jpeg

It will show the preview screen and then take a photo after five seconds; if it does so, you’re ready to get set up with our code.

Our preferred method for doing this is to first create a folder called timelapse in your home directory; you can do this simply by opening the terminal and typing:

$ mkdir timelapse

If you haven’t made any changes to your username and such, this should create the directory with the full path /home/pi/ timelapse. Keep this in mind and we’ll explain its importance in a bit. Now in the terminal we’ll download the Python code we’ve created for this project:

$ wget https://www.gadgetdaily.xyz/wp-content/uploads/2014/08/timelapse.zip

Unzip it and have a quick look through it using IDLE. There are a couple of things to note in the code in case you wish to modify it. The photos variable is set to 500 and that’s the number of shots it will take before compiling the video. In reality it will actually do one more due to the mathematical quirks of Python but that shouldn’t matter. The delay parameter is in seconds and using a bit of Google Fu you can figure out that 500 shots at 30-second intervals will take about four hours to complete.

The large subprocess.call line is used to compile the images into an AVI file at 24 frames per second. It uses the full path to the directory that we mentioned previously, so if yours is slightly different make sure you change it now.

The weather-proof PICE is a great way to get it outdoors.
The weather-proof PICE is a great way to get it outdoors.

There are a number of ways you can use it to get a time- lapse of the outdoors; the easiest and safest way is to locate a window with the view you wish to use. This may not always be easy, though, in which case we suggest trying out the PICE or PICE+ case. It’s a durable and waterproof case that is designed to house the Pi and a Pi camera. Once it’s in place, all you’ll need to do is hook it up to power, preferably with a long cable, and run the code. Once it’s finished, retrieve the Pi and the AVI file.

×