The MagPi - February 2020

(Greg DeLong) #1

07


This might get loud
The siren is capable of an ear-splitting
120 dB. Always wear ear defenders in case you
accidentally set the siren off. This particular model
is tolerant of voltages less than 12 V and the volume
reduces accordingly. We recommend starting no
higher than 5 V, which is still incredibly loud. The
siren requires its own power supply, so you’ll need
to source one (we recommend something with
variable voltages so you can experiment with noise
levels). Before continuing, make sure you’re happy
with the siren’s noise levels. If you don’t want a
loud siren, there are many buzzers and quieter
alarms available that will work to demonstrate the
project and will be much safer for younger makers
to handle.

08


Relay race
Relays are magnetic switches, allowing one
device to control another without their circuits
‘touching’. The Automation HAT comes with three
relays and we’ll use one to switch the siren on and
off. Making sure it’s unplugged, snip the siren’s
positive line (red wire) and strip a bit of wire on
both ends. Now, connect the part going to the
power supply to one of the relays on the terminal
marked ‘COM’ (common). Connect the other part
of the red wire to the same relay on ‘NO’ (normally
open). Double-check everything and make sure the
wires are secure.

09


Software
We can now read an input and create an
output using the relay. Using your favourite editor,
enter the roomguard.py code here. This is a simple
loop that will check the sensor twice a second to
see if movement has been registered. If so, the
relay is switched into place, allowing current to
flow to the siren and it sounds. Once the sensor no
longer registers movement, the relay is deactivated
and it all goes quiet. If you don’t fancy typing it
in, you can download all this code and a few extras
from magpi.cc/roomguard.
To run the code, enter the following on the
command line:

05


Connect the sensor
Although this type of PIR sensor is widely
available, they do vary in wiring. In particular, the
orientation of Vcc (power in) and ground can be
different. The sensor we’ve recommended here
has both clearly marked on the PCB. Connect the
ground pin to any of the GND connectors on the
Automation HAT, then connect any 5 V power
output to the 5 V in pin on the sensor. So that we
know what state the sensor is in, connect the data
line to any one of the buffered inputs on the HAT.
Carefully check each of the screw terminals for a
solid connection.

06


Time for a test
When you installed the software for the
Automation HAT, examples and documentation
were included. We can use these to quickly test and
calibrate the sensor. From the command line, run
the following:

python3 ~/Pimoroni/automationhat/examples/
input.py

You will now be shown the reading from all
the input sensors. Watch the one to which you
connected the data line in the previous step. It
should report a 1 when it is activated (motion has
been detected) and 0 when it ‘times out’. You can
carefully adjust the two potentiometers on the
sensor to fine-tune the sensitivity and timings to
your preferences. Press CTRL+C to stop the Python
script when you’re done.

Always wear ear defenders


in case you accidentally set


the siren off


There’s plenty of
scope for adding
more to the
project, such as an
inexpensive keypad
or a camera

Top Tip


Calibrate
carefully

Be patient when
calibrating
the sensor.
Start with both
potentiometers
turned fully anti-
clockwise, and
work from there.

TUTORIAL


44 magpi.cc Build an internet-connected room guard

Free download pdf