picam – Raspberry Pi battery powered timelapse camera – part 2 – software

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


  • 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.

The couple scripts are written in bash and are available in my github here: https://github.com/dewoodruff/picam


Start by installing Raspbian and fully update the OS. You can follow the official documentation here: https://www.raspberrypi.org/documentation/installation/installing-images/

Next, get the MoPi setup by install its software, configuring your power source, and testing it. The manufacturer has great instructions here:

  1. Installing simbamon
  2. Configuring your power supplies
  3. MoPi from the command line

Next, obtain the picam script and put it in the correct location.

sudo su -
apt install git
git clone https://github.com/dewoodruff/picam

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.

cd picam
tar xvf sunwait0.8-pi.tar
cd sunwait
./sunwait list 43.049876N -77.593224E

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.

root@picam1:~/picam# blkid -t TYPE=vfat -sUUID
/dev/sda: UUID="0F5F-3CD8"

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:


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.

Leave a Reply

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