Raspberry Pi Tutorial: Timelapse Machine

Introduction

Timelapse photography is a photographic technique of taking a sequence of frames at set intervals to record changes that take place slowly over time. When the frames are shown at normal speed the action seems much faster.

The images are then stitched together into a video to produce an animated sequence of images.

In this tutorial, we explain how to build a Timelapse-Taking Machine with Raspberry Pi, a camera and a Python program.

Hardware Required

In this example we are using Raspberry Pi Starter Kit, which includes various sensors and modules to get started right out of the box.

You may also choose to purchase the individual components here:

  1. Raspberry Pi 4 4GB or Raspberry Pi 8GB
  2. Raspberry Pi Camera 5MP or Raspberry Pi Camera 8MP
  3. Raspberry Pi 4 Power Supply 15.3W USB-C
  4. Micro SD Card 16GB or Micro SD Card 32GB
  5. Raspberry Pi 5.5" HDMI Capacitive Touch AMOLED + Case 1080×1920 (optional)
  6. Wireless Keyboard with Touchpad for Raspberry Pi and LattePanda (optional)

 

Raspberry Pi 4 4GB/8GB

A Raspberry Pi is a compact computer board offering endless opportunities. From the beginning, the Raspberry Pi was designed to be simple to use and simple to adapt to what you want to do with it.

Simply plug into a TV or monitor, keyboard, mouse, and power supply, and you are ready to go. The great thing about Raspberry Pi is that it is suitable for almost all age groups. Whether it’s introducing programming to children, or used by engineers to make complex computer-controlled systems, anyone can use one.

Raspberry Pi Camera 5MP/8MP

The Raspberry Pi Camera module uses the Omnivision 5647 Chipset. It lets you take photos with a resolution of 2592 x 1944 resolution and videos up to 1080p at 30 fps (frames-per-seconds).

Raspberry Pi 4 Power Supply 15.3W USB-C

The Raspberry Pi 4 USB-C Power Supply provides the necessary 5.1V/3.0A DC power to power the Raspberry Pi 4.

Micro SD Card 16GB/32GB

The Micro SD Card stores the Raspberry Pi's operating systems as well as files that you will be creating and reading from in your projects. Read more about how you can install the Raspberry Pi OS here. Or better still purchase an SD Card with the Raspberry Pi OS installed at our online store!

Raspberry Pi 5.5" HDMI Capacitive Touch AMOLED + Case 1080×1920

The Raspberry Pi 5.5" HDMI Capacitive Touch AMOLED + Case is an all-in-one LED display with a capacitive touchscreen as well as an enclosure to keep your Raspberry Pi all together in an attractive-looking package.

Wireless Keyboard with Touchpad for Raspberry Pi and LattePanda

This is a wonderful combo, 2.4GHz Mini Wireless QWERTY keyboard mouse, TouchPad combo, with USB interface adapter for Raspberry Pi, Cubieboard, Mini PC and others. The Ergonomically handheld design is easy to carry and operate. Build-in removable rechargable Li-ion battery that has longer standby time. Perfect for PC, Pad, Andriod TV Box, Google TV Box, Xbox360, PS3, HTPC/IPTV, and Raspberry Pi etc.

Connecting the Hardware

Insert the Micro SD Card that contains the Raspberry Pi OS into the Raspberry Pi.

Mount the Raspberry Pi into the Raspberry Pi 5.5" HDMI Capacitive Touch AMOLED + Case Enclosure. Refer to the YouTube Video and the product website for detailed setup instructions.

Connect the pi camera into the camera slot on the Raspberry Pi. The blue tape on the cable should be facing towards the Ethernet port.

Connect the Power Supply, a mouse, and a keyboard to the Raspberry Pi. Power on your setup and you are ready to start setting up the software!

Setting up the Software

Turn on the Raspberry Pi. Open the Raspberry Pi Configuration.

Enable the camera.

Save and restart the Raspberry Pi. To check if you have connected and set up the camera correctly, open the terminal from the taskbar and type "raspistill -o /home/pi/Desktop/image.jpg".

You should see a new image file appearing on the desktop.

Coding the Timelapse Program

Let's start coding our timelapse project. We will be coding this program in Python. Open "Thonny Python IDE".

from picamera import PiCamera
from time import sleep
from os import system
import datetime

The lines of codes above give the program access to the camera and date/time libraries necessary to capture images at various intervals.

#set the time to run the timelapse
runningMinutes = 10
#delay between each photo taken in seconds sleepDelay = 1 #total number of photos to take based on running minutes and sleep delay totalPhotos = int((runningMinutes*60)/sleepDelay)

The timelapse program is configured to run for 10 minutes. During this time, it will capture 1 image every second.

#create camera object
camera = PiCamera()

#set the image resolution
camera.resolution = (1024, 768)

The camera instance is created and set to take photos at a resolution of 1024x768.

#create a loop to take multiple images
for i in range(totalPhotos):
    #capture image and name it in a 4-digit sequence
    camera.capture('/home/pi/Pictures/image{0:04d}.jpg'.format(i))
    #wait before taking next image
    sleep(sleepDelay)

A loop is defined to capture images every second and save these images in the folder "/home/pi/Pictures".

#get the current datetime
dateraw= datetime.datetime.now()
#format datetime into year, month, date, hour, minute
datetimeformat = dateraw.strftime("%Y-%m-%d_%H:%M")
#use ffmpeg to create video from images
system('ffmpeg -r {} -f image2 -s 1024x768 -nostats -loglevel 0 -pattern_type glob -i "/home/pi/Pictures/*.jpg" -vcodec libx264 -crf 25  -pix_fmt yuv420p /home/pi/Videos/{}.mp4'.format(30, datetimeformat))

The program finds out the current date/time. It then performs a stitching routine to stitch all the images that it has captured into one video. It names this video based on the date/time where it performed this stitching routine.

Click on “Run”. Save the file as “timelapse”. You should see “>>> %Run timelapse.py”. Go into the pictures folder, you will be able to see the images slowly coming in.

Check the videos folder to see your new timelapse video.

Double-click the video to watch it.

Resources