Raspberry Pi Time-Lapse in Four Easy Steps

In this guide, I will take you through from start to finish on how to set up your very own Raspberry Pi time-lapse setup.

raspberry pi time lapse

We make use of the Raspberry Pi camera in this guide. You’re able to pick this cool camera at any good electronic store that sells the Raspberry Pi.

I will take you through everything you need to know to get it all set up and ready to go. The process is pretty easy, and you will learn how to set up cron jobs and more.

This tutorial is also an excellent way to learn more about the Raspberry Pi camera and the things that you can do with it. The time lapse that you end up producing is pretty cool and so easy to set up.

Equipment

The equipment you will need in this tutorial is listed below.

Recommended

Video on Setting up the Time-Lapse Machine

If you’re not much of a reading person then check out the excellent video on how to do the Raspberry Pi time-lapse, I have made it just for you.

It goes through step by step everything you need to do to get your Pi taking photos, so you’re able to use these in a time-lapse video.

Adblock removing the video? Subscribe to premium for no-ads.

Installing the Camera

Probably the most important step is to connect the Raspberry Pi camera correctly to your Pi.

This process is pretty straightforward, but for anyone new to this kind of stuff will find this section helpful.

Hardware

To install the camera, you will need to go to the ribbon cable attachment and press gently with two fingers on either side of the connector and pull up.

This technique will open the attachment, now insert the cable with the metal leads facing away from Ethernet port.

Once lined up and the cable is inserted far enough, gently press the connector back down, and the cable will now be locked in place.

clips-and-ribbon-cable

Software

To start this Raspberry Pi time lapse project you will need a copy of Raspbian installed. If you had followed my guide on installing NOOBS, you would have turned on the Raspberry Pi camera functionality.

If you haven’t, then you can turn it on by going into the terminal and writing the following line.

sudo raspi-config

After this select enable camera and select yes. You will then need to reboot the Raspberry Pi.

Testing the Camera

1. If your Pi is off now is the time to turn it on.

2. Go to the terminal screen and enter the following command.

raspistill -o cam.jpg

3. If you now type ls you will be able to see the image file now exists(cam.jpg). Open this image in the file browser or from your machine if you are using SSH.

If you are new to Linux, then you may find the guide on the ls command helpful.

4. If the image is upside down, then your camera is not positioned the correct way. You can either fix the camera up or just simply flip the image by using the following command.

raspistill -hf -vf -o cam2.jpg

5. You can now confirm that the image is now facing up the correct position.

Write a Script

It is now time to write a script that will take a picture and stamp the file name with the date and time.

If you would prefer not to write a script, then you can download the script I have already made here camera.sh. Alternatively, you can download it directly to the Raspberry Pi using the wget command.

sudo wget https://files.pimylifeup.com/timelapse/camera.sh

The script is straightforward, but remember, you will need to add -hf -vf flags if your camera is currently upside down.

Make the directory and call it timelapse by entering the following command.

mkdir timelapse

Move into this folder by entering the command below.

cd ./timelapse

Now we need to create a bash script called camera.sh using a text editor such as nano. You can do this by entering the following.

sudo nano camera.sh

In this file, copy and paste the bash script below.

#!/bin/bash
DATE=$(date +"%Y-%m-%d_%H%M")
raspistill -o /home/pi/timelapse/$DATE.jpg

Test the script by running the following line.

sh camera.sh

If it fails to run, look at the error message and try to debug the code. Also, try cross-checking with my code above.

If it doesn’t fail, then we are onto the next stage, setting up a schedule for the Pi to run.

Schedule the Script to take Pictures

The script above is now complete, and while you can make further adjustments to it, we will keep it basic for this guide.

Next, we will need to schedule the script to run so we can run this over x amount of time, allowing us to capture hundreds or thousands worth of pictures.

Let’s open the crontab for editing by entering the following.

sudo crontab –e

If you have never used cron, it will prompt you to select a text editor. I recommend choosing nano as I find this the easiest to quickly learn.

Once in, scroll down to the bottom where you will find some helpful column headers that look like below. These are your guide for setting a cron job.

Check out our tutorial on cron jobs to learn more about how the cron system works. Perfect if you’re just starting out and would love to learn more.

# m h dom mon dow   command

To schedule the time-lapse to go off every minute, add the following line. You can change this to whatever you would prefer.

* * * * * sh /home/pi/timelapse/camera.sh 2>&1

Once done, exit and save by pressing CTRL + X, then Y. Lastly, press ENTER.

You should then get the following message.

crontab: installing new crontab

If you type in watch ls in our new timelapse folder, it should now start to fill up with images you can use in making a time-lapse.

You’re able to shut down and move this to wherever you would like, and it will start retaking photos when you reboot the Pi.

If you ever need to edit or remove the cron job follow the same steps and edit or delete the line that we added.

Now, this method just saves the pictures directly to the Pi, and you can set it up to save to a USB stick, network drive and other places to store data.

To get your data off the Pi simply insert a USB into the Raspberry Pi and using the GUI (Graphical User Interface) copy and paste the files onto the USB stick.

If you want to make a time-lapse out of your images, take a look at my guide 5 Easy Steps To Turn Time Lapse Images To Video.

Deploying your Raspberry Pi Time-Lapse

One of the most amazing things about the Pi is how portable and energy-efficient it is. If you have a battery pack, you will be able to run the Pi for a few hours, and the best thing is you can place it anywhere you would like.

Once you have done setting up the Raspberry Pi camera and the cron job, you can just disconnect and place anywhere you would like. You don’t need to have a screen connected to the Pi for it to work.

There you have it, your very own Raspberry Pi time-lapse that you’re able to take some pretty awesome photos now and then transform it into a fantastic time-lapse.

52 Comments

  1. Avatar for Stephen Clark
    Stephen Clark on

    Excellent tutorial
    When I followed the steps precisely and copied the code from this page, everything worked exactly as it should
    Thank you

  2. Avatar for Steve Davies
    Steve Davies on

    Hello, how do I stop this from running? Even after a reboot it seems to keep running!

    1. Avatar for Emmet
      Emmet on
      Editor

      Hi Steve,

      I will assume that you setup the cronjob by following the guide. You will need to remove this to stop the script from running.

      You can begin editing the crontab by running the following command on your device.

      sudo crontab -e

      Within this file you should see a line that looks like the following.

      * * * * * sh /home/pi/timelapse/camera.sh 2>&1

      Delete this line then quit and save the crontab changes.

      Cheers,
      Emmet

  3. Avatar for Gregor
    Gregor on

    Hello, a very good tutorial. Everything worked well.
    Thanks Gregor

  4. Avatar for Elo
    Elo on

    I’m trying to set up a timelapse program in my Raspberry.

    When I try to add the line

    ***** sh /home/pi/timelapse/camera.sh

    using sudo crontab -e, it allows me to write to the file, but when I try to save it, it tells me, that

    crontab: installing new crontab
    “/tmp/crontab.04i0QM/crontab”:24: bad command
    erros in crontab file, can’t install
    Do you want to retry the same edit? (y/n)

    What to do?

    Kind regards, Elo

    1. Avatar for Gus
      Gus on
      Editor

      Make sure you have spaces between the stars. Eg. * * * * *

  5. Avatar for Zoe
    Zoe on

    Is there a way to put a time stamp either on the photo itself or in the filename? Thanks

    1. Avatar for Gus
      Gus on
      Editor

      The timelapse script currently does use a timestamp for the filename (DATE=$(date +”%Y-%m-%d_%H%M”)). This can be updated so it’s in a different format.

      Adding it on the photo itself will require a package like imagemagick.

  6. Avatar for Joan Casalprim Busquets
    Joan Casalprim Busquets on

    How do I save the images to a usb flashdrive instead of the SD card?

    What do I have to modify from the script?

    Thanks!

  7. Avatar for Victok
    Victok on

    Hello Gus,

    I was wondering if you could do this with a normal webcam, if it’s possible could you instruct me on how to do it? I am pretty sure it is very similar to doing it with the camera module but I want to make sure.

    Thanks,
    Victok

  8. Avatar for Jon
    Jon on

    Hello every time i try to create the crontab using sudo crontab -e
    it tells me there is no crontab for root – using an empty one
    888

Leave a Reply

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