Make a Timelapse with OctoCam
2020-06-09 | By Pimoroni
License: See Original Project
Courtesy of Pimoroni
Guide by Tanya Fish
This tutorial will guide you through taking photos using a Pi Zero and camera, to make your OctoCam into a simple timelapse-capturing device. More complicated camera operations are covered in Sandy's tutorial about MotionOS, but this is a quick and simple bit of code. Use it to make a timelapse of a plant growing with the delay set to a day, or the progress on your building work with hourly photos, or a soldering project with a photo every 5 seconds.
Enable the camera
This tutorial assumes you have already set up your OctoCam as per the instructions. If you're using a camera and a Pi, make sure the camera is connected.
In the Terminal, type sudo raspi-config and press Enter. This will bring up a menu on the screen. You'll need to press 5, then choose option 1 to enable the camera, and then choose yes. Once you finish with the menu you should get prompted to reboot. This needs doing!
Get the libraries for the camera
Once your Pi has rebooted, open a Terminal again and type:
sudo apt-get install python3-picamera
This will install the libraries for the camera that we're going to use. You don't need to reboot after this step.
Write some code to take a picture
You can use the code timestamp to time stamp the image name, but it puts colons in it, which makes the filenames tricky, and it's why we need to create our own timestamp.
import time
import picamera
# Makes a shorter name for the Pi camera, and sets the resolution.
# Change the resolution if you want better/lower quality.
# A resolution of 1920 x 1080 is approx 1.1MB per photo.
camera = picamera.PiCamera()
camera.resolution=(1920, 1080)
# Gets the camera on and allows it time to detect light levels
camera.start_preview()
time.sleep(5)
# A loop that takes pictures and names them with the time and date.
# The timestamp has to be in the loop so it takes a current time for each photo.
# The image is then saved with the name img-timestamp.jpg.
try:
while True:
ts=time.strftime("%Y-%m-%d-%H%M%S", time.gmtime())
camera.capture('img-'+ts+'.jpg')
time.sleep(10)
except KeyboardInterrupt:
camera.stop_preview()
sys.exit()
If you run this code now, while you're hooked up to a monitor, you should see a preview of your camera and it will freeze every time it takes a photo. To get out of the preview, you need to press ctrl and d.
It's a good idea to have a look and check the image is the quality you want, even if you end up with something like this.
Last step. Running the code.
You can run your code now, and place the camera somewhere, or you can use Cron to make your code run when the Pi starts, which is what I did for this sunrise video.
Stitching the photos together
There are loads of ways of stitching the photos together, and there's a guide on the Raspberry Pi website detailing a few methods. We used a variant on the avconv option, called ffmpeg. If you are using Stretch it works straight off, if you're using Jessie you'll have to do a sneaky direct install.
Stretch version
sudo apt-get update
sudo apt-get install ffmpeg
ffmpeg -r 24 -pattern_type glob -i '*.jpg' -c:v copy output.avi
Jessie version
wget https://johnvansickle.com/ffmpeg/releases/ffmpeg-release-armel-32bit-static.tar.xz
tar -xJf ffmpeg-release-armel-32bit-static.tar.xz
sudo cp ffmpeg-3.4.1-armel-32bit-static/ffmpeg /usr/local/bin
ffmpeg -r 24 -pattern_type glob -i '*.jpg' -c:v copy output.avi
Bear in mind that this takes FOREVER - about 5 minutes on my Pi 3 for 429 photos. Go and make yourself a coffee, or two!
Here's the result:
The super-duper version!
When a Pi is on and taking pictures, if you don't have a screen connected, it's difficult to tell if anything is happening. Adding an LED that blinks when it's about to take a picture is one way of showing this. We took the biggest LED we could find and hooked it up to the GPIO pins.
If you want to do this, you'll need to connect a 330 Ohm resistor to the short leg of the LED to stop it sucking up too much current. Join the negative (resistor) end to a ground pin, and the positive end to BCM pin 18 on your Pi.
You can do this with a breadboard, or by soldering some jumper jerky onto the components. See the photo for our version.
You'll need to add in a few lines of code to define the LED, switch it on before taking a photo, and off after.
import time
import picamera
import RPi.GPIO as GPIO
# Sets up pin 18 as the one that controls the LED.
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(18, GPIO.OUT)
# Makes a shorter name for the Pi camera, and sets the resolution.
# Change the resolution if you want higher/lower quality.
# A resolution of 1920x1080 is approx. 1.1MB per photo.
camera = picamera.PiCamera()
camera.resolution=(1920, 1080)
# Enables the camera and allows it time to detect light levels.
camera.start_preview()
time.sleep(5)
# A loop that takes pictures and names them with the time and date.
# The light comes on 1 second before it takes a photo.
# The timestamp has to be in the loop so it takes a current time for each photo.
# The image is then saved with the name img-timestamp.jpg.
# The light goes off, then we wait 10 seconds before the next photo.
try:
while True:
GPIO.output(18, GPIO.HIGH)
time.sleep(1)
ts=time.strftime("%Y-%m-%d-%H%M%S", time.gmtime())
camera.capture('img-'+ts+'.jpg')
GPIO.output(18, GPIO.LOW)
time.sleep(10)
except KeyboardInterrupt:
camera.stop_preview()
sys.exit()
A note on storage space and power consumption
A Pi Zero and camera could draw up to 240mAh. There's not much difference between the Zero and the Zero W in this case. This means if you have a battery pack that says 1200mAh, it will last this combination 1200/240 = about 5 hours.
If you set the resolution to 1920x1080, where each picture is 1.1MB, then a 16GB SD card with NOOBS installed (approx 11GB left) will store approximately 10,000 photos. At one every second, that's about 2hrs 45mins or, at one every 10 seconds, that's about 27 hours! Note: all calculations are approximate!
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
Visit TechForum