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.
We make use of the official Raspberry Pi camera in this guide. You’re able to get this cool mini camera at any good electronic store that sells the Raspberry Pi.
I will take you through everything you need to know to get the time-lapse photo capturing set up and ready to go. The process is easy, and you will learn how to set up cron jobs, a bash script, 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 cool and very easy to create.
Equipment
The equipment you will need in this tutorial is listed below.
Recommended
- Raspberry Pi ( Amazon )
- Raspberry Pi Camera ( Amazon )
- Power Supply ( Amazon )
- Micro SD Card ( Amazon )
- USB Keyboard ( Amazon )
- USB Mouse ( Amazon )
This tutorial was last tested on Raspberry Pi OS Bookworm running on the Raspberry Pi 5.
Video
If you are not much of a reading person, check out the excellent video on how to do the Raspberry Pi time-lapse.
It goes through step by step everything you need to do to get your Pi taking photos, so you can use them in a time-lapse video.
Adblock removing the video? Subscribe to premium for no-ads.
Installing the Raspberry Pi Camera
The most important step is correctly connecting the Raspberry Pi camera module to your Raspberry Pi board. This process is straightforward, but if you are new to this kind of stuff, you will find this section helpful.
Hardware
If you are using a Raspberry Pi 5, you may need to acquire a ribbon cable (Amazon) designed to fit into the new ports.
1. To install the camera, you will need to go to the ribbon cable port and pull gently with two fingers on either side of the connector. This port is labeled CAM/DISP 0 on the Raspberry Pi 5. The connector should be lifted slightly.
2. Now insert the cable with the metal leads facing away from Ethernet port or towards the Ethernet port on the Raspberry Pi 5.
3. 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. You can test that it is locked in place successfully by gently pulling on the cable.
Software
To start this Raspberry Pi time lapse project, you will need a copy of the Raspberry PI OS installed to the SD card.
Confirm the software required to run the camera is installed to the Raspberry Pi by entering the following line into the terminal.
sudo apt install rpicam-apps
Testing the Camera
A quick note, if you are using a version of Raspberry Pi OS older than Bookworm, you will need to replace rpicam-still
with libcamera-still
.
1. Go to the terminal application and enter the following command.
rpicam-still -o cam.jpg
2. 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.
ls
If you are new to Linux, you may find the guide on the ls command helpful.
3. If the image is upside down, your camera is not positioned correctly. You can either fix the cameras position or simply flip the image by using the following command.
rpicam-still --hflip --vflip -o cam2.jpg
4. You can now confirm that the image is now facing up the correct position.
Now you can proceed to creating the script that will take the photos required for a Time-Lapse. You can either create a manual script that is fired using cron or use the built-in command.
Writing a Time-Lapse Script
It is time to write a script that will take a picture and create a file name with the date and time.
The script is straightforward, but remember, you will need to add –hflip –vflip flags if your camera is currently upside down.
If you are running a version of Raspberry Pi OS or Raspbian that is older than Boookworm, you will need to replace rpicam-still
with libcamera-still
.
1. Make a directory and call it timelapse by entering the following command.
mkdir ~/timelapse
2. Move into the timelapse folder by entering the command below.
cd ~/timelapse
3. 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 command.
nano camera.sh
4. In this file, copy and paste the bash script below.
This script creates a string that contains the year, month, day, hours, and minutes. Lastly, it captures a photo and stores it with the specified date and time as the name.
#!/bin/bash
DATE=$(date +"%Y-%m-%d_%H%M")
rpicam-still -o ~/timelapse/$DATE.jpg
5. Next, we make the script executable by running the chmod command below.
chmod +x camera.sh
6. Now, test the script by running the following line.
sh ~/timelapse/camera.sh
7. If it fails to run, look at the error message and try to debug the code. Also, try cross-checking with the code above.
You can confirm it ran by running the following command. There should be a single image with the timestamp as the file name.
ls ~/timelapse
If it doesn’t fail, then we are onto the next stage, setting up a schedule for the script to run.
Schedule the Script using the cron
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.
1. Let’s open the crontab for editing by entering the following.
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.
2. Once in, scroll down to the bottom, you will find some helpful column headers that look like the one below. These are your guide for setting up a cron job.
Check out our tutorial on cron jobs to learn more about how the cron system works. Perfect if you’re starting out and would love to learn more.
# m h dom mon dow command
3. Add the following line to schedule the time-lapse script to fire every minute. You can change this to whatever you would prefer.
* * * * * sh ~/timelapse/camera.sh 2>&1
4. 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
5. 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 can shut down and move the Raspberry Pi to wherever you like. When you reboot the Pi, it will start taking photos again.
If you ever need to edit or remove the cron job, follow the same steps and edit or delete the line we added.
Using the In-Built Time-Lapse Mode
Instead of using the manual script and cron method, you can simply use the rpicam-still
command to take all the photos required for a timelapse. Each method has its pros and cons, but if you are looking for something basic this command is perfect.
1. Make a directory and call it timelapse by entering the following command.
mkdir ~/timelapse
2. Move into this folder by entering the command below.
cd ~/timelapse
3. The command below will run for 60 seconds and take a photo every second. Each photo will be four digits long and increment by 1 per image. For example, 00001.jpg
, 0002.jpg
, and so on.
rpicam-still --timeout 60000 --timelapse 1000 -o ~/timelapse/%04d.jpg
I will briefly explain each option we use in the command below.
--timeout
is in miliseconds and controls how long the command will run before stopping. In our case, it will be 60 seconds.--timelapse
is in milliseconds and determines how often a photo will be taken. In our case, it will be every second.-o
specifies where to output the files.
Creating the Time-Lapse Video
Both of these methods save the photos directly to the Raspberry Pi. Alternatively, you can set it up to save it to a USB stick, cloud storage, or network drive.
Transferring data from the Pi to a USB stick is a simple process. Just insert the USB into the Raspberry Pi and use the GUI (Graphical User Interface) to copy and paste the files onto the USB stick.
If you want to make a time-lapse out of your images, look at our 5 easy steps to turn time lapse images to video.
Below is the video we created from the images captured by our Raspberry Pi.
Adblock removing the video? Subscribe to premium for no-ads.
Deploying your Raspberry Pi Time-Lapse
One of the most amazing things about the Raspberry Pi is how portable and energy-efficient it is. If you have a battery pack, you can run the time-lapse for a few hours, and the best thing is that you can place it anywhere you like.
Once you have set up the Raspberry Pi camera, script, and cron job, you can disconnect it and place it anywhere you like. You don’t need to have a display connected to the Raspberry Pi for it to work.
Conclusion
There you have it, your very own Raspberry Pi time-lapse that you are able to take some awesome photos now and then transform it into a fantastic time-lapse. I find it a great way to capture the sunrise or sunset.
I recommend checking out our plenty of other projects that use cameras. For example, we look at installing OpenCV, controlling a DSLR, and much more.
If you have any feedback, corrections, or anything else, please do not hesitate to leave a comment below.
Excellent tutorial
When I followed the steps precisely and copied the code from this page, everything worked exactly as it should
Thank you
Hello, how do I stop this from running? Even after a reboot it seems to keep running!
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.
Within this file you should see a line that looks like the following.
Delete this line then quit and save the crontab changes.
Cheers,
Emmet
Hello, a very good tutorial. Everything worked well.
Thanks Gregor
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
Make sure you have spaces between the stars. Eg. * * * * *
Is there a way to put a time stamp either on the photo itself or in the filename? Thanks
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.
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!
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
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