Simple Time-Lapse Camera Using The Raspberry Pi

Simple Time Lapse Camera Using RPi
This post will show you how you can build a simple time-lapse camera using a Raspberry Pi. The resolution, duration and time can be easily updated in the script. We have created something similar using the ESP32-CAM board but the Raspberry Pi camera quality is much better than that and hence the results are also better than the previous version.

The video above gives you an overview of how everything comes together. I would recommend watching it first to get a better understanding of how you can make and tweak this project.

Step 1: Prepare the Raspberry Pi

Components Excluding The DisplayComponents Excluding The Display

You do not need a display for this project but I decided to use one to show you how the scripts operate. I also will be using a Raspberry Pi 3 in this example, but this will work with any variant that supports using a camera.

The first thing you need to do is download the Raspbian image. I used the Raspbian Desktop version as I would like to access the Raspberry desktop to show you the files and such. So if you plan on using a display then I would recommend downloading that too. Next, you need to flash the image onto a suitable microSD card and for this, I would recommend using Etcher. I decided to use WiFi instead of an Ethernet connection and I also decided to SSH into the board to control it. Instead of doing this, you can also connect a keyboard to the Raspberry Pi and execute all the commands directly from the terminal. The choice is yours.

If you plan on using SSH over WiFi, then you will need to create a file with the network details. You can download the template file from the link below to get started. Simply open it up using a text editor like notepad++ or Sublime Text and then fill in your country code, network name and password. Once done, save the file and copy it to the boot drive on the Raspberry Pi. To enable SSH, you simply have to create a blank file with the name ssh and place it in the boot drive. You do not need to add any extension. Once done, you can insert the microSD card, connect everything and boot the Raspberry Pi.

WPA Template File: https://www.bitsnblobs.com/wp-content/media/fw/common/wpa_supplicant.conf

Obtaining The IP Address Using AngryIP ScannerObatining The IP Address Using AngryIP Scanner

HDMI Configuration For The DsiplayHDMI Configuration For The Display

Correct Display ResolutionCorrect Display Resolution

As can be seen in the video, the display resolution was initially incorrect and I had to add the correct HDMI configuration to get it to work. This depends on the display you are using so I will not list the statements here, but you can refer to the video and the image if you are interested.

Enabling The CameraEnabling The Camera

The next thing we need to do is open up a terminal on your computer and SSH into the board by using it’s IP address. You can use software like AngryIP scanner to obtain the board’s IP address. Then, open up a terminal and type in “ssh [email protected]” but be sure to change the IP address to match the one for your board. You will have to enter a password and the default password is “raspberry”. Then, we need to enable the camera and this can be done by running the “sudo raspi-config” command. The camera option is listed under the interfacing options and once enabled, you will have to reboot the board.

Step 2: Test the Camera and Write the Script

Testing The CameraTesting The Camera

Before we write the script, it is advisable to make sure the camera is working. To do this, simply SSH back into the board and then run the “raspistill -o test.jpg” command. This will capture an image and save it to the current directory with the name test.jpg. You can then run the “ls” command to check if the file is created. Running the “ls” command will list out all the files and folders that are present in the current directory. If the file exists then you can be sure that the camera is working correctly.

Creating The ScriptCreating The Script

Next, we need to write the script and this can be done by running the “sudo nano time-lapse.sh” command, which will open up a text editor. You then need to type in the following as shown in the image:

#!/bin/bash

raspistill -t 20000 -tl 1000 -w 1920 -h 1080 -o /home/pi/images/img%05d.jpg

This causes the board to capture images continuously for 20 seconds (20000ms), at an interval of 1 second (1000ms). It also defines the resolution of 1920×1080 and also specifies the location where the images should be stored. The “%05d” specifies that the image names should contain a total of 5 digits with leading zeros. Once done, simply save the file by pressing “CTRL+X”, then “Y” and then the “Enter” key.

Creating The Images DirectoryCreating The Images Directory

Image FilesImage Files

We then need to make the script executable which can be done by running the “sudo chmod +x time-lapse.sh” command. Before we can run the script, we need to create the image directory, and this can be done by running the “mkdir images’. You can then test the script by running the “./time-lapse.sh” command. The board will then capture 20 images and save them to the images folder. You can navigate to the folder and run the “ls” command to view the files.

Creating The ScriptCreating The Script

Automating The ScriptAutomating The Script

Next, we have to automate the script for this, we will use the systemd method we used in the previous post where we created an IP camera. We first need to run the following command “sudo nano /etc/systemd/system/time-lapse.service” which will open up a text editor. You then need to type in the following but I would recommend simply copying it over.

[Unit]
Description=time lapse
After=multi-user.target

[Service]
Type=simple
ExecStart=/home/pi/time-lapse.sh
User=pi
WorkingDirectory=/home/pi
Restart=on-failure

[Install]
WantedBy=multi-user.target

Enabling The ScriptEnabling The Script

Then, you simply need to save the file like before and then enable the service by running “sudo systemctl enable time-lapse.service”. We then simply reboot the file. Once the board boots, you will be able to see it capture the images and save them to the location.

Step 3: Edit the Script to Capture Indefinitely and Save to USB

Indefinite Capture Mode With No PreviewIndefinite Capture Mode With No Preview

The script currently only captures images for the first 20 seconds and if we need to edit it to make it capture images indefinitely. To do this, SSH back into the board, then stop the service by running “sudo systemctl stop time-lapse.service”. Then, open up the script file by running “sudo nano time-lapse.sh”. Pass the value 0 to the “-t” flag as this will make it capture images indefinitely. I also went ahead and disabled the image preview by adding the “-n” flag as can be seen in the image. You then have to save the script and start it by running “sudo systemctl start time-lapse.service”.

USB Drive PathUSB Drive Path

Updating Save Location To USBUpdating Save Location To USB

Restarting The ScriptRestarting The Script

You can also update the storage location to a USB drive. First, plug in the USB drive and open it up in the File Manager. Make a note of the location which should be similar to “/media/pi/USB_DRIVE_NAME”. Then, simply stop the service like above and open it up to edit it. You can then change the storage location. Make sure the folder you specify exists, if not, simply create it by using the mkdir command. Once done, start the script again and reboot the board. It will capture and save images to the new location.

Creating Video FileCreating Video File

Once you obtain the images, you can use several different applications to convert this to a video. Time Lapse Creator is one such application that can be used.

So that’s how easy it is to create a time-lapse camera using the Raspberry Pi. If you liked this project, then please consider supporting us by subscribing to our YouTube channel.

Thank you for reading!