Tutorial STEP BY STEP
(^96) The Official Raspberry Pi Projects Book raspberrypi.org/magpi
PISPY CAM
Set up a motion-activated spy camera in your room to find out
if anyone’s coming in when you’re not there
e’ve all been there. You’ve gone out for the day
and you know you closed your bedroom door,
but you come back and it’s slightly ajar. Who’s
been in there? Were they friend or foe?
STEP-01
Connect up the camera
Attaching the camera to the Raspberry Pi Zero is easy
- see the camera starter guide (magpi.cc/QrMnng).
You need to first switch out the cable in the Camera
Module with the adapter for the Pi Zero. Gently
pull down on the clasp that keeps the ribbon on the
Camera Module attached and remove the cable. Take
note of the orientation of the silver connectors on the
W
ROB ZWETSLOOT
Tinkerer, sometime maker,
other-times cosplayer, and
all-the-time features editor
of The MagPi.
magpi.cc / @TheMagPi
cable you removed, and make sure your adapter cable
is the same way round when you insert it. Put the
other, smaller side into your Pi Zero, with the silver
connectors facing towards the board.
>STEP-02
Wire up the circuit
The circuit for this is fairly simple, especially as the
PIR doesn’t need a resistor as part of its setup. The PIR
comes with three connections: VCC, GND, and OUT.
VCC needs to be connected to a 5V power pin, GND
needs to go to a ground pin, and then there’s the OUT
wire which will be our input. We’re connecting it to
pin 8, also known as GPIO 14.
If your Pi Zero has GPIO pins attached, you can do
this via a breadboard; otherwise you can loop the wire
around the pin holes and use a bit of Blu Tack to keep
them in place, or a dab of glue from a glue gun on a
low setting. Soldering is also an option.
>STEP-03
Get the code
Write up or download our code for this project. This
code uses two libraries: GPIO Zero and the standard
picamera library. GPIO Zero can be used to get a
reading from the PIR motion sensor very easily, which
can then be tied into the picamera code so it takes a
photo when motion is detected. We make sure the
photo is given the time so you know when it was
taken, and it then waits five seconds before seeing if
it should take another photo. Save it as spy.py in the
default home folder.
>STEP-04
Final preparations
You can run it first to give it a test. You might want to
change the sensitivity, which you can do by adjusting
> Pi Zero
> PIR motion
sensor
magpi.cc/
CWnNll
> Camera Module
magpi.cc/
bASFoG
> Pi Zero camera
cable adapter
magpi.cc/
2gT2KwE
> Portable battery
You’ll
Need
A very simple Fritzing diagram
just to make sure you know
how to wire up the sensor