Computer Shopper - UK (2020-10)

(Antfer) #1

90 OCTOBER2020|COMPUTER SHOPPER|ISSUE392


T


he Raspberry Pi camerais
one of the most versatile
add-ons forthe small
computer,and with the
launch of the new higher-
resolution model (see
AdvancedProjects,page 114), you can
now captureimages in evenmoredetail.
While it’s handy to have acamerathatyou
can activate at will, you can also use the
systemwith aPIR motion sensor to capture
an image only when there’smovement.
This lets youcreateyourown security
camera, or you can go further and use the
systemto capturewhatpets areuptointhe
home or move thesystemoutside to see
whatwildlife is in your garden. In this guide,
we’ll take you through the steps to create a
motion-activated camera.

CONFIGURE THE


MOTION SENSOR


Yo uneed to startwith your Pi turned off.
First, you need to get aPIR motion sensor,
which works by detecting heat moving
across the sensor.It’sthe same technology
used in many security cameras and alarm
systems. PIR motion sensors arepretty
cheap: we’reusing the SB components
model (£3,tinyurl.com/pimotionsensor).

Thereare other PIR sensors available that
work in asimilar way.
Our chosen model comes with the cables
to connect it to theGPIO,although the
pin-outs on the sensor aren’t labelled on our
model. To connect this model, turn the PIR
sensor upsidedown withthe three pins
facing you. Fromleft to right,they’rethe
power,signal and ground pins. It’s worth
checking your sensor carefully,just in case
the pin layout is different.
To hook this sensor up to the Pi,you
need to connect the power pin to the GPIO
pin 2(5V), the sensor pin to pin 16 (GPIO

23) and the ground to
pin 4(ground).

CONFIGURE


THECAMERA


Now you can
connect the camera.
Open up the tabs
on the side of the
ribbon connector,
and then slide the
camera’scable into the slot. Thepinsonthe
cable should faceforwards towards the pin
on the socket. Yo ucan then slide the clips
on the side down,and the cameracable is
locked into place.
Next, you need to enable the camera. To
do this, open up Raspberry Pi Configuration
fromthemain menu,select Interfaces and
select CameraasEnabled. Click OK and
reboot your computer when prompted.

WRITE THECODE


Next, you needtoinput the code to
get the motion capturesystemin place.
Yo ucan download the code fromthe
motioncamera.py file (www.
shopperdownload.co.uk/pi/motion.py).
We’ve included the code here, too:

import RPi.GPIOasGPIOimport RPi.GPIO as GPIO
import timeimport time
import datetimeimport datetime
from picamera import PiCamerafrom picamera import PiCamera

camera =PiCamera()camera =PiCamera()
SENSOR_PIN =23SENSOR_PIN =23

camera.start_preview()camera.start_preview()
GPIO.setmode(GPIO.BCM)GPIO.setmode(GPIO.BCM)
GPIO.setup(SENSOR_PIN,GPIO.IN)GPIO.setup(SENSOR_PIN,GPIO.IN)

while True:while True:

Buildamotion-

controlled camera

Want to see what pets or wildlife areupto?

Tu rn your RaspberryPiintoatrigger trap

ABOVE:Themotion sensor has three pins that
have to be connected to the Raspberry Pi

ABOVE:Thecamera connector slides intoplace
and has to be locked down

LEFT:Youneedto
connect the motion
sensor to theRaspberry
Pi’s GPIO header

PROJECT2

Free download pdf