In part 1 we covered the hardware setup of my Raspberry Pi based timelapse camera. Now we tackle the software.

Features

Configurable image capture interval.

Images are stored on a USB flash drive for easy retrieval.

Only takes pictures during the day. Daytime is determined by the awesome sunwait utility that calculates sunrise and sunset based on latitude and longitude.

The image capture script can be bypassed by flipping a switch connected to GPIO so you can boot into the OS.

A log is written to keep track of battery voltage over time.

The script is updated from the USB flash drive on every boot so you don’t need to manually boot into the OS every time you want to tweak the interval. Simply update the script on the flash drive and on second boot, you’ll be running the new code.

Next, extract sunwait and test it. Sunwait can be compiled for any OS by downloading it from the Sourceforge page, but I have precompiled it for the Pi and included it in this package. I did not want to install gcc and all the associated build tools to keep the OS of this project as minimal as possible, so I precompiled it on my development Pi.

Next, setup the flash drive. Format it as FAT32 (you can use Windows if you want). Then we need to get its UUID when it is plugged into the Pi. The UUID is a unique identifier for that flash drive. By mounting with UUID instead of the /dev/sd* device name, we can avoid mounting issues if the name of the device changes, such as when you use a USB hub vs directly plugged in.

Copy the UUID of the correct device and drop it in the variable at the top of takePic.sh. While you are at it, choose your image interval and set the SLEEPBETWEENPICS variable. Lastly, find your approximate latitude and longitude and plug those into LAT and LON in the script.

Next, create a directory called ‘scripts’ on the flash drive and copy takePic.sh and timediff.sh into that directory. On every run, these scripts will be copied to the Pi to make updating the code or picture interval easier.

The flash drive will be mounted to the directory ‘/images’ so create that.

mkdir /images

Lastly, configure the Pi to run takePic.sh every time it boots.

vim /etc/rc.local

Just before the ‘exit 0’ line, add:

/root/picam/takePic.sh

That should be it! Make sure the hardware is all connected, then reboot your Pi. If the switch is in the “off” position, it should boot, take a picture, then shut down again until its time to take the next picture. If the switch is in the “on” position, the script will exit and the OS will stay booted.

I hope this was useful. If you have any questions, leave a comment below.