Friday, November 16, 2018

Raspberry timelapse camera

For an index to all my stories click this text

In my previous story about the Raspberry Pi Camera I showed you how to build a Lomography-like photocamera. As most steps described in this story are derivated from that story please read it carefully before proceeding: http://lucstechblog.blogspot.com/2018/11/raspberry-photo-camera-2.html

Now suppose you want a time-lapse camera. For those of you who are not familiar with the term: a timelapse camera is a camera that automatically takes pictures at certain pre-defined intervals. The fun in that is that you can make subsequent pictures of seeds germinating, molds growing, flowers opening and closing, a garden through the seasons, the build of a 3d print, a build of a project etc. etc. etc. It is a bit like a slow motion movie. The difference with a movie is that you will get seperate pictures, however there is software available to stich all photo's together to make a movie.

The Software

Actually we can use almost the same software as in the story about building the photocam. I just made some small alterations.


#!/usr/bin/python3

import picamera
import RPi.GPIO as GPIO
import os
from time import sleep

numberfile = open('/home/pi/number.txt','r')
number = numberfile.readline()
numberfile.close()
camera = picamera.PiCamera()
camera.resolution = (1920, 1080)

os.system('printf "\033c"')

GPIO.setmode(GPIO.BCM)
GPIO.setup(15, GPIO.OUT)
GPIO.setup(18, GPIO.OUT)
GPIO.setup(3, GPIO.IN, pull_up_down=GPIO.PUD_UP)

GPIO.output(18, GPIO.HIGH)

def takepic(picture):
        GPIO.output(15, GPIO.HIGH)
        global number
        number2 = int(float(number))
        number2 = number2 + 1
        namefile = "image" + str(number2) + ".jpg"
        camera.capture(namefile)
        numberfile = open('/home/pi/number.txt','w')
        numberstr = str(number2)
        number = numberstr
        numberfile.write(numberstr)
        numberfile.close()
        GPIO.output(15, GPIO.LOW)
        
def progend(stopprog):
 GPIO.output(18, GPIO.LOW)
 os.system('sudo shutdown now')         

GPIO.add_event_detect(3, GPIO.FALLING, callback=progend, bouncetime=300)

while True:
 takepic(21)
 sleep (10)

So where is the difference.First we only need one button. We only need the button to set the Raspberry ON or OFF.

As you can see there is just one interrupt and that is attached to that button:

GPIO.add_event_detect(3, GPIO.FALLING, callback=progend, bouncetime=300)
 

The line tests wether the button has been pressed and subsequently shuts the Pi down by calling the progend routine.

The takepic routine is exactly the same as the one in the Photocam story. It is not triggered by a button but triggered in the while True: routine

while True:
    takepic(21)
    sleep (10)


The sleep function is waiting for 10 seconds, then takepic(21) is called, and after that the routine is repeated.

The program is taking a picture around every ten seconds set by sleep (10).

Alter the figure 10 in any amount of seconds you might need.
If you want to take a picture every 30 minutes that would be sleep (30 x 60)

Here is the breadboard setup:


Wildlife adaption

The above software and hardware works great when making timelapse photo's of plants, molds, melting ice etc etc etc. However when you are making time-lapse photo's from animals I would remove the led attached to GPIO 15. This led is put ON everytime a picture is taken and then put OFF again. This could scare animals and ruin the sessions.

If you do not attach the LED on GPIO 15 you can leave out the next lines from the program.

GPIO.setup(15, GPIO.OUT)

This is in the setup section

GPIO.output(15, GPIO.HIGH)
GPIO.output(15, GPIO.LOW)

These can be found in the takepic(picture) routine.

Installing

My setup was made with the Raspberry Pi Zero which makes this very cheap. You could use the same setup and software with a Raspberry Pi 3

You will need to write the program with an editor and make it autostarting. This is all explained in the story about making the Photocamera so click here:
and follow the steps described there.

Real life example 

Naturally I tested it all using a simple setup and here are the results:








Take a carefull look at the pictures and you will see that they are not taken exactly 10 seconds after eachother. And that is no flaw.
Remember: the program takes a picture and then waits ten seconds. Taking the picture and writing it to the SD card will take some time.

In this short timelapse in which each time 10 seonds is waited this lead to time shifting. However if you're program waits 10 minutes or half an hour or even longer between pictures this will not be significant.

One last experiment I will be making with this setup is attaching a PIR to the Raspberry to make photo's of intruders or animals.

Till next time
Have fun

Luc Volders