Raspberry Pi Automated Time Lapse (Updated Sept. 18, 2017)

Update: If you want a Time Lapse Raspberry Pi image you can burn to an SD card? Click here to download the zipped image. Just extract and burn to a 16GB SD card, pop it into a Pi with a camera module and you'll be on your way.
Update #2: Following Phillip Martin's blog post, I set up the Pi's WiFi adapter as an access point so I can connect my iPad and Macbook directly to it without needing to rely on another WiFi network. Now I can remote into it with VNC Viewer in any location I'm recording. See this post for details.

We’re working on some cool video pieces to showcase the awesomeness happening on our campus. As part of that effort, we built an automated time lapse video recorder using a Raspberry Pi 3, case, camera and an Anker PowerCore battery pack. Set this puppy down, point it in the right direction and let it sit for an hour (or 12). Then, stop the script and combine your hundreds (or thousands) of photos into a cool time lapse video.

Here’s a quick example of what’s possible.

Some pictures, with an explanation to follow.

 Front of Raspberry Pi time lapse camera 

Physical build, challenges and prototyping

During the early iterations, the camera module was external to the case and basically taped it to the outside making it vulnerable to damage. Cutting a small hole in the front cover to allow the camera lens to see through and taping the camera module down to hold it in place resolved the issue. There were some small plastic tabs on the inside that needed to be removed in order to get the camera to sit flush. Depending on your case, you’ll likely need to make similar modifications. Be careful to avoid damaging the ribbon cable as it can be kinked, pinched or come loose.

One early frustration was the need to tote around a keyboard, mouse, monitor and power adapter to each filming location. To get around this, a little script runs on startup so that the Raspberry Pi will just start taking pictures as soon as it boots up. Adding the Anker battery pack (perfectly sized to work with the Pi’s case), attached with 3M Command Strip hook and loop fasteners resolved the power issue. Now, it can be set up anywhere. Just attach the power cable and let it run. As soon as it boots, the camera starts taking pictures – one each second, until the power is disconnected or the device is shut down. While we haven’t timed it exactly, the 10000 mAh battery looks like it might be able to run the Pi for  24 hours or longer.

Another challenge was not being able to see how the pictures looked to frame the shot without a monitor. So a VNC server was installed on the Pi. Now an iPad can be used to remote into the Pi to see the pics as they are being taken. Looking at the first few pictures allows adjustments to be made to get the best shot.

How it works [updated 09/03/2017]

This line is added to /home/pi/.config/lxsession/LXDE-pi/autostart file:

@/home/pi/lapsescript

This runs a one-line script named lapsescript stored in the home directory. To create the lapsescript file:

  1. Open the Terminal app on the Pi
  2. Type “cd ~” and hit enter to be sure you’re in the home folder
  3. Type “sudo nano lapsescript” and press enter to create the lapsescript file in the nano text editor
  4. Type lxterminal -e sudo /usr/bin/python2 /home/pi/lapse.py in the nano editor
  5. Press “ctrl-x” to exit nano
  6. Press “y” to save the file
  7. Press enter to confirm saving the file
  8. Now that you’re back in the Terminal window, type “sudo chmod a+x lapsescript” and press enter. This makes the script executable.

So, when the Pi boots up, the line at the top of this section runs lapsescript. If you read the lapsescript line from left to right, lxterminal is executed (opened), the Python interpreter starts, and runs the lapse.py Python script. This script creates a new folder with the current date and time in the home directory and stores pictures (taken one per second – customizable in the script), storing each with a sequential filename of image0000.jpg, image0001.jpg, image0002.jpg, etc. Each time the script runs, a new folder is created so that different shots don’t need to be separated by hand – each has its own folder. Note that this all happens automagically when the Pi boots. No need to type anything. Just set it down where you want to shoot, plug in the external battery and let it sit for as long as you’d like. The Python script will continue to run until you shut it down (Ctrl-c, disconnect the power or otherwise shut down the Pi) or your sd card gets filled.

These pics can then be converted to a movie on the Pi with avconv or transfer them to your favorite video editor. Navigate to the pictures folder in Terminal and use the following command to create a 24 frames/sec mp4 video named timelapse.mp4 scaled to 1280×720:

avconv -r 24 -i image%04d.jpg -r 24 -vcodec libx264 -vf scale=1280:720 timelapse.mp4

How to put it together:

These instructions assume you want to set up your Raspbian installation from scratch. If you just want to download an image that’s all ready to go, just download the image linked at the top of this post, write it to your SD card, pop it in your Pi, point your camera somewhere interesting and turn on the power.

  1. First, connect and enable a camera module. Once connected, open the Pi’s configuration (Preferences > Raspberry Pi Configuration) and select the Interfaces tab. Ensure that the camera is enabled. Also enable VNC and ssh if you want to access your Pi remotely.
  2. Python script creates a directory in the Pi home directory using the current date and time to keep each time lapse sequence separated. The linked .txt file would need to be renamed timelapse.py and in order to work with this automated setup and saved to the home (/home/pi/) directory. To change the length of time between pictures, change the number in the sleep(1) line. The number in parenthesis is the number of seconds to pause between pictures. You can also change the resolution to suit your particular camera module.
  3. To run the script at startup, add the line
    @/home/pi/lapsescript
    to /home/pi/.config/lxsession/LXDE-pi/autostart file. To do this, open a terminal window and type:
    sudo nano /home/pi/.config/lxsession/LXDE-pi/autostart
    This opens an editor with the autostart file. At the bottom of the file, add the line “@/home/pi/lapsescript/” at the bottom. Press control-x, then ‘y’ then enter to save the file.

Future upgrades

A dynamic DNS service would help make the VNC server accessible regardless of the current network or ip address (future upgrade). Ideally, it would have some buttons in order to shut down the Pi properly rather than just disconnecting the power and to change the time between shots. The newer 8MP camera module and a touchscreen are being shipped with hopes that the higher resolution will look awesomer on our new 4k displays. The touchscreen may just help with setup and preclude the need for a VNC/iPad monitor setup. Alternatively, I’d like to add some buttons that could pause or stop the script, change the image resolution and/or the length of time between pictures. I’ll update the post as those get implemented.

Links used in researching this project.

Some links to resources.

  1. Setting up VNC server on the Raspberry Pi
  2. Running Python script on startup. This was particularly helpful. In earlier setups, the script would run, but there was no way of shutting it off. This method opens a Terminal window so that Ctrl-c will interrupt the script.
  3. Python code inspiration came from the second page of this post.
  4. Other Raspberry Pi time lapse posts:
    1. Trevor Appleton
    2. Jeff Geerling – Headless Pi Zero