Computer Shopper - UK (2020-10)

(Antfer) #1

ISSUE392|COMPUTER SHOPPER|OCTOBER 2020 91


ififGPIO.iGPIO.inpnput(ut(SESENSONSOR_R_PIPIN):N):
print(print("Moti"Motionon detedetectected"d"))
cameracamera.capt.capture('ure('/home/home/pi/D/pi/Desktoesktop/p/
image%s.jpg'%datetime.datetime.now())image%s.jpg'%datetime.datetime.now())
time.sleep(1)time.sleep(1)

We’ll explain whatthis code does. First, we
need to importthe functions thatweneed,
including the camera, the GPIOcontrol, and
acouple of extrafunctions fortime. Next, we
need to create the camerainsoftware:
camera=PiCamera().
We next set avariable with the GPIOpin
thatwe’reusing to read the motionsensor
(SENSOR_PIN=23). Now we’reontothe
main bit of code.
Here, we’refirst firing up thecamera
preview window with camera.start_preview().
This shows alive feed fromthe camera,
although you can remove this line if
you don’t want to see the file.
Thenexttwo lines cover
configuring the GPIO,
telling the systemwhich
mode we want the pins
to be read in and enabling
our live sensor. We’ve
gone for the Broadcom
number (BCM), where
physical pin 16 is referred
to as GPIO23.
Then, we’reinto our main
loop,which will run until we
hit Ctrl-C.Each time around
the loop we check to see if
motion has been detected
(if GPIO.input(SENSOR_PIN).
If it has, we print amessage
on screen, then we use the

camera.capturefunction to save afile to
the desktop.The line thatwe’ve used uses
the datetime.now() function to append the
date and time to afile, so we can see when
it was captured.
We then sleep forone second to prevent
adeluge of shots. All sample photos are
saved to the desktop.

ADJUSTING SENSITIVITY


If you get too many image captures, you
can tweakthe systemby adjusting the
potentiometers on the side of the motion
sensor.Withthe sensor stood uprightwith
the dome pointing up,the potentiometer
on the leftadjusts the stickiness, the one on
the right sensitivity.
Forthe left, if you turn the dial clockwise,
the sensor continues to reportmovement
after it has been detected, giving you afew

extrashots; all theway anticlockwise gives
you morereal-time movement.
Sensitivity increases how much motion is
required to set offthe sensor,and helps
detect motion at range. Yo ucan gently
adjust both settings, using ascrewdriver,to
get the results you want.

TAKING IT FURTHER


So far so,good, but you can also startto
tweak how the softwareworkstoget it to
do more. First, you can try adjustingthe
systemto save files to somewhereelse on
your computer,oradjust the systemso that
it records video instead, using this code for
video capture:

camera.start_recording('/home/pi/Desktop/camera.start_recording('/home/pi/Desktop/
video.h264')video.h264')
sleep(10)sleep(10)
camera.stop_recording()camera.stop_recording()

Thesystemwe have built
has the components loose so
thatthey need to be
propped up.However, if you
want to build something a
bit better,you can use the
mounting holesonthe
cameraand sensortoplace
themupright.
Thereare plenty of
waterproof housing kits
available, too,soyou can
move your Raspberry Pi
outside and get it to
capturewildlife. Unless
you’ve got awaterproof
power supply nearby,you’ll
need to have abattery pack
to keep your Pi powered.
Yo ucan find plenty of
options online attinyurl.
com/392piprojects1.

ABOVE:Enable thecamera beforeyou canstarttouse it in software


ABOVE:Our script detects motion and captures what’shappening, saving
the files to thedesktop as well as showing alive preview

RIGHT:Youcan adjust the
motion sensitivityusing the
controls on the sideofthe
motion sensor

PI PROJECTS

Free download pdf