How To Take Timelapse Photos with Raspberry Pi

CanSatLogoAs 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:

  1. Add a new User account
  2. Configure the system to automatically log the user in when the system starts.
  3. Write a script to take photos.
  4. 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:

passwd picam

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/ using nano:

sudo nano /etc/init.d/

Enter the following script code:

# To take timelapse photos
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

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/

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 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:

cd /home/picam

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/ &

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


Tags: , , , , , , , , ,

6 responses to “How To Take Timelapse Photos with Raspberry Pi”

  1. eanao says :

    i followed these instructions and now my raspi sits in this loop all the time. I have no opportunity to type kill all. totally stuck :(

    • ccollins says :

      My apologies @eanao, this is a rather quick and dirty script.

      That said, here are a few things you can try:
      1. Connect your Pi to a TV and let it boot. When the script starts taking photos, hit enter, and you should see a command prompt behind the photo preview. You can still use this, even if it is a bit obscured by the photo being taken. Use this to ‘sudo killall’ (or whatever you named your script).
      2. If enter doesn’t work, then try Ctrl-C to interrupt the currently running process and get back to a command prompt. Then try step 1 again.
      3. If you boot to the desktop, then you’ll have to either exit or get a command terminal open to kill the timelapse script.
      4. Connect your Pi to a network and use a different computer to open an remote ssh connection to it. This assumes that you have previously configured your Pi using raspi-config to enable ssh connections. Once you have a command prompt you can try step 1 again.
      5. If you do manage to get your Pi back under control, you could consider using the -n option to raspistill in the script, so that it doesn’t create a preview pane when it takes photos.

      I hope this helps,

  2. Masumi Yamamuro says :

    Thank you very much for this great instruction.
    I noticed with this script, picture which is being taken does not show up on the screen.
    Do you have any idea how we can show the photo on the screen (so that I can inow exactly where I am shooting at)?

    I have my pi hooked up to 1.5′ screen.

    • ccollins says :

      In our script we’re using the ‘-n’ option to raspistill. This suppresses the image preview feature. Removing this will re-enable the defaults for this option.

      Depending on your needs, you can also specify different options and time-outs for the preview pane. See the raspistill documentation for all the information you need.


      • Masumi Yamamuro says :

        Thank you very much for your very quick response.
        Yes, it is waorking as I like. Appreciated your instruction.

        I have looked many site for time lapse camera with Raspberry Pi. Your site is by far the most instructive for me ( a newbie to computer world).

Trackbacks / Pingbacks

  1. A Better Time-lapse Photography Script | Chris Collins - March 1, 2014
%d bloggers like this: