How To Take Timelapse Photos with Raspberry Pi
As part of our CanSat endeavours, we are going to use a Raspberry Pi to do some time-lapse photography as we descend after launch. In order to do this we’ll need to configure our Pi to do this automatically. Read on for a description on how to do this.
In broad strokes, here is what we plan to do:
- Add a new User account
- Configure the system to automatically log the user in when the system starts.
- Write a script to take photos.
- Configure the user account to automatically run the script when the user logs in.
Add a new User
Change to the root user, or use sudo, and create a new user (called picam) with the following command. The list of groups are the same as the default pi user, so the new user should have the same system privileges. The -m switch also instructs the system to create a new home directory for the user at /home/picam
useradd -G adm,dialout,cdrom,sudo,audio,video,plugdev,games,users,netdev,input,spi,gpio -m picam
Once this user is created, set a password for it using:
Configure Automatic Login for Picam
We want to automate as much as possible now, so we have less to do on competition day. In that case, it would be useful for us to have the Pi take photos as soon as it is switched on, so we don’t have to log in and start it manually.
We can do this by having the system automatically log the picam user in, when the system starts. This is controlled using the /etc/inittab system configuration file. To be safe, make a back up of this before you edit, so you can restore the good version if anything goes wrong.
Edit /etc/inittab using:
sudo nano /etc/inittab
Scroll down to find the following line:
1:2345:respawn:/sbin/getty --noclear 38400 tty1
Edit the line by putting a # in front of it. This turns the line into a comment and the system won’t apply it when you next restart. On the following line, add this:
1:2345:respawn:/sbin/getty --autologin picam --noclear 38400 tty1
The only difference between the lines is the instruction to automatically log the picam user in. Save and exit nano with Ctrl+O, Ctrl+X.
Write a Script to Take Photos
Create the following script in /etc/init.d/timelapse.sh using nano:
sudo nano /etc/init.d/timelapse.sh
Enter the following script code:
#!/bin/bash # To take timelapse photos USER=picam SAVEDIR=/home/$USER/images mkdir -p $SAVEDIR while [ true ]; do filename=img-$(date -u +"%Y%m%d%H%M%S").jpg echo "Shooting $filename" raspistill -n -o $SAVEDIR/$filename # sleep for 10 seconds sleep 10 done
Save and exit nano. To make this script executable, you’ll need to change the file permissions. You can do this with:
sudo chmod +x /etc/init.d/timelapse.sh
This script will run forever and repeatedly call raspistill to take a photo, every 10 seconds. The photo files will be saved to /home/picam/images, and are date stamped.
Automatically Running the Script
When a user logs in the operating system gives them a ‘shell‘. The system runs the shell on the user’s behalf, and all other programs the user runs are children of the shell process. When a user logs out, the shell process completes. We can use this to our advantage by configuring the shell to start the timelapse.sh script when the picam user logs in. To do this we’ll edit the .bashrc configuration file for the picam user.
Change directory to the picam users home directory:
Edit the .bashrc file with nano. This is a script that gets automatically run by the shell when it starts, and is typically used to reconfigure the shell in some way. Add the following lines to the end of the .bashrc file:
echo .bashrc: Running timelapse /etc/init.d/timelapse.sh &
Save and exit your editor.
Test Your New Time-lapse Rig
Restart the Raspberry Pi, and when you log in, you should find photos being automatically stored in /home/picam/images.
We’ve configured the timelapse.sh script to run when picam logs in, and we’ve also configured the system to log picam in. If you log picam in multiple times, then the timelapse script will run multiple times, which can cause errors as raspistill can only take photos one at a time.
To stop the timelapse.sh script, you’ll have to kill the process. This can be done (as root or with sudo) with:
To return the system to normal operation, you’ll have to restore the original /etc/inittab configuration file.