DEV Community

Cover image for Simple Time-lapse with a Raspberry Pi
Christian
Christian

Posted on • Originally published at cri.dev

Simple Time-lapse with a Raspberry Pi

Originally posted on cri.dev

Time-lapses always fascinated me. So I wanted to create one on my own.

Had a spare Raspberry Pi Zero W catching dust, you can purchase one for ~ 25$ and the related Raspberry Pi Camera module for < 20$.


Sneak peek

Partial time-lapse of 2020-09-08 in 20 FPS (1 snapshot taken every minute)

Check it out on cri.dev

Requirements

To build your own you'll need

There is even an official High Quality Camera, that will give even better quality time-lapses!

Table of contents

Preparation

Flash the Pi

You'll need to flash Raspberry Pi OS on a fresh SD Card.

Either with Balena Etcher or the official Raspberry Pi Imager. The procedure is extremely straight forward.

Just go through the steps and you have Rasperry Pi OS installed on a SD card.

Set up Wi-Fi connectivity

To get the Pi connect to your Wi-Fi, you'll need to create a file called wpa_supplicant.conf in the SD card.

The contents of wpa_supplicant.conf should look like this (configure it accordingly):

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
ap_scan=1
update_config=1

network={
  scan_ssid=1
  ssid="YOUR_WIFI_ACCESS_POINT"
  psk="YOUR_WIFI_PASSWORD"
}
Enter fullscreen mode Exit fullscreen mode

Enable SSH access

To enable SSH access (with the user pi and password raspberry), simply create an empty file (without extension), called ssh in the SD card.

Enable Camera module

To enable to camera module, you can configure it through the raspi-config utility.

But also with the file config.txt in SD card.

Open it and add the following to it:

start_x=1
gpu_mem=128
Enter fullscreen mode Exit fullscreen mode

Just make sure there are no other occurrencies for start_x or gpu_mem

Create the time-lapse

Take image with raspistill

raspistill is the command line tool for capturing still photographs with the camera module.

The idea is to take a snapshot every x minutes using raspistill

To take a single image, I'm using the following bash script called /home/pi/take-snapshot:

#!/bin/bash

set -e

DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
SNAPSHOTS_DIR=/home/pi/snapshots
SNAPSHOT_PATH="$SNAPSHOTS_DIR/$DATE.jpg"
CURRENT_PATH="$SNAPSHOTS_DIR/current.jpg"

echo "$DATE"
mkdir -p "$SNAPSHOTS_DIR"
echo "taking snapshot $SNAPSHOT_PATH"
raspistill \
    --width 1280 \
    --height 960 \
    --quality 100 \
    --output "$SNAPSHOT_PATH"
echo "snapshot saved $SNAPSHOT_PATH"

echo "adding watermark $SNAPSHOT_PATH"
cd $SNAPSHOTS_DIR
montage \
  -label "$DATE" \
  $SNAPSHOT_PATH \
  -pointsize 30 \
  -gravity Center \
  -geometry +0+0 \
  $SNAPSHOT_PATH

cp $SNAPSHOT_PATH $CURRENT_PATH
Enter fullscreen mode Exit fullscreen mode

This script will take a snapshot, name it in the format %Y-%m-%dT%H:%M:%SZ.jpg and put it in the folder /home/pi/snapshots.

At the end this photo is also watermarked using montage from the ImageMagick family.

Schedule capture with crontab

To take a photo every 5 minutes using the above script, you can set up a simple cron job for that.

Just run crontab -e to edit the cron schedules and add the following lines

SHELL=/bin/bash
*/5 * * * * sh /home/pi/take-snapshot >> /home/pi/snapshots.log 2>&1
Enter fullscreen mode Exit fullscreen mode

Directory listing of snapshots

You can expose a web server on port 80 to easily list all images (and time-lapses) taken in the folder /home/pi/snapshots.

Start the server at boot by adding the following to your crontab

@reboot sudo python3 -m http.server --directory /home/pi/snapshots 80
Enter fullscreen mode Exit fullscreen mode

Create time-lapse with ffmpeg

Taking a time-lapse for the day 2020-08-31 with ffmpeg is as easy as running

cat 2020-08-31*.jpg | ffmpeg -r 10 -i - -c:v libx264 -vf "format=yuv420p,scale=1280:-2" time-lapse-2020-08-31.mp4
Enter fullscreen mode Exit fullscreen mode

raspberry-pi-time-lapse repository

You can clone / fork the repo christian-fei/raspberry-pi-time-lapse and create your first time-lapse in no time.

There are various scripts that simplify the steps above:

  • take-snapshot to take a watermarked and timestamped photo in the folder snapshots/
  • server to start a HTTP server on port 80 listing all snapshots and time-lapses taken so far
  • rsync-snapshots to sync the snapshots to from the Raspberry Pi to your PC
  • create-time-lapse to create a time-lapse for a specific date
  • crontab to schedule the snapshots and spin up the server

Top comments (0)