Pages

Friday, November 23, 2018

Alarm with Raspberry, camera and PIR

For an index to all my stories click this text

This is (for now) the last in a series about working with the Raspberry Photocamera.

The first story that describes how to make a photocamera can be found here: https://lucstechblog.blogspot.com/2018/11/raspberry-pi-camera.html And I really urge you to read that story first before starting this project. This project will use many steps explained in the Photocamera story that will not be repeated here.

The second story is a project for making a timelapse camera. You can re-read that story here:

https://lucstechblog.blogspot.com/2018/11/raspberry-timelapse-camera.html

And this story is about making a camera that takes a picture when movement is detected. The story uses a PIR which basic functionality is described here:
https://lucstechblog.blogspot.nl/2017/01/pir-basics-movement-detection.html

You can use this as a security camera that takes pictures when someone enters a room or approaches your front door. You can also put this at a bird (or other animals) feeding place. As usual: use your imagination.

The difference with the photocam project

The difference in using a PIR in stead of a pushbutton for making a photo is in the use of the GPIO pins. Normally the state of an IO pin is high and we pull it LOW when pushing a button.

The PIR works different as you may have read in my story about it (https://lucstechblog.blogspot.nl/2017/01/pir-basics-movement-detection.html). The PIR normally sends out a LOW signal and switches to HIGH when movement is detected. We will look at the software adjustments for this later on. First let us have a look at the hardware.

The Hardware

The hardware is basically the same as the hardware for the Photocamera.



The ON/OFF switch and the leds are the same. The switch for taking the picture has been replaced with the PIR. The PIR gets its power from Pin No2 on the raspberry which delivers 5 volts.
The PIR (as described in the pir-basics story) works at 5 volts but delivers 3.3 volts on its trigger pin. However your's might be different so check that !!! Remember the Raspberry Pi is not 5 volts compatible. So only send a 3.3 volts signal to the IO pins.

The Software



#!/usr/bin/python3

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

sleep(5)

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

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.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def takepic(picture):
        GPIO.output(15, GPIO.HIGH)
        global number
        # number2 = int(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()
        sleep(6)
        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)         
GPIO.add_event_detect(21, GPIO.RISING, callback=takepic, bouncetime=10)


while True:
    GPIO.output(18, GPIO.HIGH)


As you can see the program in Python is not that much different from the Raspberry fotocam software which you can find here:

Like discussed in that previous story, write the program with the nano editor and put it in the home/pi directory.

As the program needs a way to wait between taking pictures the sleep function is imported from the time library.

At the start of the program the line:

sleep(5)

makes sure that the PIR is settled before the program really begins. The story about the PIR Basics showed that the PIR needs about a minute to test its environment before it can be used.
The PIR gets power as soon as the Raspberry Zero is powered up. So the PIR should be settled when the program starts. Test if that is indeed the case in your situation and alter the sleep(5) in a longer time when needed. If you are using a Raspberry PI 3 you should really test this well as the Pi3 boots many times faster as the Pi Zero does.

The GPIO setup for pin 21 has been changed in:


GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

This makes sure that the PIN is kept in the LOW state until the PIR puts a HIGH signal on the pin.

The def takepic(picture) routine has a line added that stops the program for 6 seconds after taking a picture. This makes sure the picture that is just taken is written safely to disk and the PIR is reset again if no movement has been detected anymore.

The interrupt routine for pin 21 (where the PIR is attached to) is also altered:

GPIO.add_event_detect(21, GPIO.RISING, callback=takepic, bouncetime=10)

The interrupt wil now be triggered when GPIO 21 receives a HIGH signal.

As I said the alterations to the Photocam software are few.

Enter the file in the nano editor and save it in the /home/pi/ directory. Call it alarm.py or any name you like.

Alter the startup file as I showed in the fotocam story but now with the same name of the just saved file:

@lxpanel --profile LXDE-pi
@pcmanfm --desktop --profile LXDE-pi
python3 /home/pi/alarm.py
@xscreensaver -no-splash
@point-rpi

That's all folks.

Modifications.

You now have a full working Raspberry motion detecting camera.
If you want to use it as a safety camera that takes pictures when someone enters your home/room or steals your butterfingers I suggest to leave out the leds and hide the camera in a non obvious place. This way no intruder will know a picture is taken. If you leave out the leds in the hardware you can remove all references to GPIO 15 en 18 in the software too.

I am thinking of making a double function camera. Using a button to take a picture and also have the possibility of using it as a motion camera. Should not be to difficult. Use a switch to choose between the PIR and the button. Make sure that the button is connected to a pull-down resistor (in stead of pull-up) and connected to +5 volts so it gives a LOW when not pushed and a HIGH when pushed just like the PIR. That way you would need almost none alterations to the software.

Till next time

Have fun

Luc Volders