in , , , ,

Room Guard: build a Raspberry Pi motion sensor alarm

Reading Time: 6 minutes

Room Guard: You’ll Need

Why an Automation HAT?

Many Raspberry Pi physical computing projects directly use the GPIO to connect things like sensors, buzzers, and LEDs. In this case, we’re going use the incredibly versatile Pimoroni Automation HAT. This ‘input expander’ allows us to control devices that would normally either be incompatible or even damage your Raspberry Pi. As a small buzzer may not be particularly deterring to a would-be room invader, we’ve chosen a 120dB siren that requires more current that our Raspberry Pi can safely handle. So, we will give the siren its own power supply and safely control it using one of the Automation HAT’s relays.

- Werbung -

Prepare your Raspberry Pi

It’s up to you what model of Raspberry Pi you use for this project. A Raspberry Pi Zero W is more than capable of running the code and we originally prototyped our guard using one. If you’re thinking of getting clever, though, such as adding facial recognition using a camera, you might want to go for the horsepower of a Raspberry Pi 4. Either way, start by attaching the Automation HAT carefully to the GPIO header, and secure with the provided standoff posts on the opposite side to ensure the HAT doesn’t wiggle about and come loose.

Set up the software

We’re going to use Raspbian Buster Lite as the operating system as there’s no need for a user interface, just a basic command-line operating system. That said, there’s no harm in installing the full version if you’re more comfortable with that. Download the image from the Raspberry Pi website and flash to a microSD card. Now, as ever, it’s time to log in and update everything with sudo apt update && sudo apt upgrade. Once finished, run sudo raspi-config and set up networking (if using WiFi). Now, install the Automation HAT software by running the following command:

curl https://get.pimoroni.com/automationhat | bash

This will guide through setting up for driver software and examples. You may need to reboot your Raspberry Pi afterwards.

Get sensitive

The PIR sensor has three connectors: one for 5V power in (which can be provided by the HAT), ground, and in the centre is the data line. Operation is very simple. If the sensor is triggered by movement, the data line goes ‘high’ (outputs current). After a period of no movement, it goes low (no current). Its sensitivity can be controlled by adjusting one of the potentiometers (the left one when it’s turned upside down – see Figure 1). The other potentiometer sets the minimum time for which the sensor reports movement. We could connect the PIR directly to the GPIO, but as the HAT is now in the way, we’ll use its inputs instead.

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 5V power output to the 5V 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.

Test the Room Guard Automation HAT

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.

Warning! This might get loud

The siren is capable of an ear-splitting 120dB. Always wear ear defenders in case you accidentally set the siren off. This particular model is tolerant of voltages less than 12V and the volume reduces accordingly. We recommend starting no higher than 5V, 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.

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.

Set up the roomguard.py 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 GitHub.

To run the code, enter the following on the command line:

- Werbung -
python3 roomguard.py

Try moving about!

Now Add notifications

We now have a working motion-detection alarm, but this is meant to be internet-connected, so let’s make it smarter. It would be useful to have a notification sent to a smartphone when the alarm is activated. There are a many different types of notification service, and adding support for most is possible using just one Python library, Apprise. Have a look at the code for roomguard_notify.py on GitHub for an example of how to add notifications. Also, check out Apprise’s documentation.

Add a camera

Now the basics are working, you can augment the alarm with additional sensors and/or notifications. The Automation HAT has plenty of remaining inputs and outputs for you to play with. A simple step would be to add a Raspberry Pi Camera Module. See if you can change the code to take a photo of the intruder and then forward that image to your notification. Look at the GitHub repository for an example.

Make it your own

- Werbung -

What else could you add? One aspect of an alarm not implemented is any kind of deactivation. Could you add a web server so you can control the alarm from your phone? How about using a keypad to set an activation code? There’s potential to add batteries and additional PIR sensors to create a standalone unit. Use facial recognition software to identify who was in your room. There’s lots of avenues to explore. Over to you.

What do you think?

26 Points
Upvote Downvote

Written by tmedia

Schreibe einen KommentarAntworten abbrechen