The Easy Way

Our ready-to-go image can be burned to a 4GB or larger micro SD card and will boot on any model of Raspberry Pi with a camera connected…

The “Using It” page of this guide provides configuration and operation details…but if nothing else: it’s super especially important that you press and hold the halt button and then wait about 20 seconds before switching the camera off. Don’t just turn off the power when you’re done…otherwise you may lose images and/or corrupt the SD card and have to start over.

By default, the camera will capture a 1280x720 pixel JPEG image every 15 seconds, recording it to the SD card in the “timelapse” folder. You can change these options by inserting the card in your regular computer and editing a configuration file, explained on the “Using It” page.

This ready-made SD card is limited to 2.5 gigabytes of captured images, regardless of the actual card capacity. If you require more than this and have a larger card, see our guide Resizing the Raspberry Pi Boot Partition and/or follow the “Long Way” directions below. This may require access to a second Raspberry Pi computer temporarily.

If the ready-made card does everything you need, you can skip ahead to the “Assembly” page.

The Long Way

If the 2.5 gigabyte limitation is not acceptable, or if you just want more control and tweaks, you can put together all the software parts manually. Some prior Raspberry Pi experience is required…basic system configuration, networking, etc.

Option 1 is to start with the SD card above and adjust the partition sizes as explained in our Resizing the Raspberry Pi Boot Partition guide. That’s usually easiest. Give the project a dry run first though, confirm everything works with the standard configuration.

Option 2 is to roll your own. Start with an existing Raspbian Jessie Lite image, then install all the software and scripts needed for the camera project.

This requires a Raspberry Pi with a network connection, which can be a nuisance if you’re working directly on the Pi Zero. It’s usually easiest to set up the SD card on a more capable system (like a Raspberry Pi 3) and then move the finished card over to the Pi Zero; it’ll boot all the same.

Download the latest version of Raspbian Jessie Lite from the Raspbian downloads page. Use Jessie “Lite” for this…do not use the full Raspbian Jessie, nor the NOOBS card image.

“Burn” the image to a micro SD card. On first boot, log in with the default username and password (“pi” / “raspberry”) and run the raspi-config utility…

sudo raspi-config

Select “Expand Filesystem” and “Enable Camera” at the very least. I also recommend configuring the keyboard (under “Internationalisation Options”), and ssh is handy if you plan to do the rest of this via remote login. Any other adjustments are up to you. When you’re done, tab to the “Finish” button, reboot when prompted and log back in.

The system will need to be on a network at this point, either wired Ethernet (usually easiest) or WiFi. The latter is beyond the scope of this guide, but others in the Adafruit Learning System cover this subject, or Google around for pointers.

Install WiringPi, which provides the gpio tool our camera script relies on:

sudo apt-get update
sudo apt-get install -y wiringpi

The file /boot/timelapse.sh will contain our camera script…

sudo nano /boot/timelapse.sh

Then, if you’re logged in remotely via ssh, you can copy-and-paste the script below into that file. Alternately, since this will be located in the Pi’s /boot directory (which also shows up on regular Windows or Mac computers), you can save the script on your desktop computer and move it to the card later.

#!/bin/sh

# Timelapse script, because timelapse options in raspistill don't power
# down the camera between captures. Script also provides a camera busy LED
# (v2 cameras don't include one) and a system halt button.
# 'gpio' command requires WiringPi: sudo apt-get install wiringpi
# Limitations: if DEST is FAT32 filesystem, max of 65535 files in directory;
# if DEST is ext4 filesystem, may have performance issues above 10K files.
# For intervals <2 sec, better just to use raspistill's timelapse feature.

# Configurable stuff...
INTERVAL=15            # Time between captures, in seconds
WIDTH=1280             # Image width in pixels
HEIGHT=720             # Image height in pixels
QUALITY=51             # JPEG image quality (0-100)
DEST=/boot/timelapse   # Destination directory (MUST NOT CONTAIN NUMBERS)
PREFIX=img             # Image prefix (MUST NOT CONTAIN NUMBERS)
HALT=21                # Halt button GPIO pin (other end to GND)
LED=5                  # Status LED pin (v2 Pi cam lacks built-in LED)
prevtime=0             # Time of last capture (0 = do 1st image immediately)

gpio -g mode $HALT up  # Initialize GPIO states
gpio -g mode $LED  out
mkdir -p $DEST         # Create destination directory (if not present)

# Find index of last image (if any) in directory, start at this + 1
FRAME=$(($(find $DEST -name "*.jpg" -printf %f\\n | sed 's/^[^1-9]*//g' | sort -rn | head -1 | sed 's/[^0-9]//g') + 1))

while :         # Forever
do
	while : # Until next image capture time
	do
		currenttime=$(date +%s)
		if [ $(($currenttime-$prevtime)) -ge $INTERVAL ]; then
			break # Time for next image cap
		fi
		# Check for halt button -- hold >= 2 sec
		while [ $(gpio -g read $HALT) -eq 0 ]; do
			if [ $(($(date +%s)-currenttime)) -ge 2 ]; then
				gpio -g write $LED 1
				shutdown -h now
			fi
		done
	done

	OUTFILE=`printf "$DEST/$PREFIX%05d.jpg" $FRAME`
	# echo $OUTFILE
	gpio -g write $LED 1
	raspistill -n -w $WIDTH -h $HEIGHT -q $QUALITY -th none -t 250 -o $OUTFILE
	gpio -g write $LED 0
	FRAME=$(($FRAME + 1)) # Increment image counter
	prevtime=$currenttime # Save image cap time
done

To launch the script automatically on startup, edit the file /etc/rc.local and insert the following line near the end, just before the final “exit 0”:

sh /boot/timelapse.sh 2>/dev/null &

Finally, follow the Resizing the Raspberry Pi Boot Partition guide to expand the boot partition.

Camera settings and such are explained on the “Using It” page.

This guide was first published on Jun 30, 2016. It was last updated on Mar 08, 2024.

This page (Software) was last updated on May 31, 2016.

Text editor powered by tinymce.