G-D00XZP85S3 How to Make an Intruder Alarm With Raspberry Pi Pico - KangMus Official
Skip to content Skip to sidebar Skip to footer

How to Make an Intruder Alarm With Raspberry Pi Pico

Microcontrollers are often used in commercial security products such as burglar alarms. The latter can be quite expensive, however. So why not create a homemade alarm using a $4 Raspberry Pi Pico microcontroller?

This one uses a PIR (passive infrared) sensor – as used by many commercial alarm systems – to detect the presence of an intruder. It’s ideal for catching anyone sneaking into your room or snooping around your desk, or for protecting your stash of cookies. Using MicroPython, you can program your alarm to react with an audible alert and flashing light.

1. Building the Alarm

To build the alarm, you’ll need a selection of standard electronic components.

What you’ll need:

  • Raspberry Pi Pico with soldered male pin headers
  • Breadboard
  • LED (any colour)
  • 330-ohm resistor
  • Active piezoelectric buzzer
  • HC-SR501 PIR sensor
  • 4x Male-to-male (M2M) jumper wires
  • 3x Male-to-female (M2F) jumper wires

Note: If you don’t fancy soldering male pin headers to your Raspberry Pi Pico, it’s possible to buy a Pico with headers already attached.

Related: A Peek at the Pico, Raspberry Pi's Newest Petite Powerhouse

Before wiring everything up, take a look at the underside of the Pico to see the pin labels.

On the top of the Pico, you can also see how the physical pin numbering works, from 1 to 40, counter-clockwise from the left of the micro-USB port.

On the breadboard, insert the Pico’s male pin headers into the holes at one end. Push it down firmly to ensure good connections – it should fit snugly. Then use female-to-male jumper wires to connect the PIR sensor to it: the VCC pin should be wired to Pico’s 5V VBUS, digital OUT to GP28, and GND to a GND pin (e.g, pin 3), as shown in the wiring diagram below.

Connect one of the breadboard’s ground rails (marked by a blue line) to another GND pin on Pico (e.g. physical pin 23, as here). For a flashing light, insert an LED into the breadboard, its legs either side of the central divide.

The shorter leg (cathode) should then be connected to the same ground rail. The longer leg (anode) of the LED needs to be connected to the GP15 pin via a resistor to limit the amount of electric current passing through it, which might otherwise damage the LED or the Pico.

Finally, add a buzzer to make a beeping noise when the alarm goes off. Place its legs on either side of the breadboard’s central divide and connect the shorter leg or black wire to the ground rail and the longer leg (sometimes marked on top of the buzzer with ‘+’) or red wire to GP14.

2. Programming the Alarm

You’ll need to install MicroPython onto the Pico. This process involves four simple steps:

  1. Download MicroPython for Raspberry Pi Pico from the Raspberry Pi website
  2. Connect the Pico to your computer via its micro-USB socket while holding the BOOTSEL button
  3. Wait for the Pico to appear as an external drive
  4. Drag and drop the .uf2 MicroPython file to copy it to the Pi Pico; it will automatically reboot

While numerous programming IDEs (integrated development environments) are available for MicroPython, here we’ll use Thonny. It’s already pre-installed in Raspberry Pi OS (if you’re using a Raspberry Pi computer connected to the Pico), or can be downloaded for any computer system from the official website by clicking the link in the top right corner.

Download: Thonny (Free)

With your Pico connected to the computer, open Thonny. In the bottom-right corner of the Thonny window, you’ll see the version of Python you’re currently using. Click on it and select MicroPython (Raspberry Pi Pico).

You are now ready to program your intruder alarm on the Pico. Add the following lines of code to the main pane of Thonny.

import machine
import utime
pir = machine.Pin(28, machine.Pin.IN, machine.Pin.PULL_DOWN)
led = machine.Pin(15, machine.Pin.OUT)
buzzer = machine.Pin(14, machine.Pin.OUT)
def pir_handler(pin):
utime.sleep_ms(100)
if pin.value():
print("Motion detected. Intruder alert!")
for i in range(50):
led.toggle()
buzzer.toggle()
utime.sleep_ms(100)
pir.irq(trigger=machine.Pin.IRQ_RISING, handler=pir_handler)

Here, import the machine and utime libraries at the top. Next, set up objects for the PIR, LED, and buzzer – connected to GP28, GP15, and GP14 pins respectively.

Note that the PIR is set as an input with machine.Pin.IN, with a machine.Pin.PULL-DOWN parameter to set its Pico pin’s resistor to pull-down mode; this means it will read as zero until an electric current is sent to it from the PIR being triggered.

Related: Getting Started With MicroPython on the Raspberry Pi Pico

At the bottom of the code, an IRQ (interrupt request) is set up to trigger the pir_handler function as soon as a signal is detected on the input pin (GP28) from the PIR sensor.

In the function itself, to avoid repeated triggering within a short time, add a 100ms delay before checking the pin value again and, if it is non-zero, triggering the alarm. It then toggles the LED and buzzer on and off, to flash the light and make a beeping noise.

Save the program to your Pico with a relevant name, such as alarm.py. Run the program and, when you wave your hand over the PIR sensor, the buzzer should beep and the LED flash rapidly.

3. Adjusting Sensor Sensitivity

If the alarm is going off too easily, or not at all, you may need to adjust the sensitivity of the PIR sensor. The HC-SR501 has two plastic screws – usually labelled Sx and Tx – attached to two tiny potentiometers to adjust its settings.

Using a small screwdriver, you can turn the Sx screw counter-clockwise to increase its sensitivity (or vice versa). Turning the Tx screw alters the length of time the triggered signal is sent after intruder detection – we found it best to turn it fully counter-clockwise, for the shortest delay of 1 second.

By default, the PIR will sense any movement in the 360° around it. If you want to limit its detection scope, try placing it at the bottom of the cardboard inner tube from a toilet roll and angling it in the direction you want to cover.

Make Your Own Mobile Intruder Alarm

Once your intruder alarm is working to your satisfaction, you may well want to move it away from your computer. By saving the program as main.py, you can then disconnect it from the computer and connect a standard mobile power bank to its micro-USB port.

The Pico will then automatically run the main.py program as soon as it’s powered up. Congratulations: you now have a mobile intruder alarm to place anywhere you want.



source https://www.makeuseof.com/how-to-make-an-intruder-alarm-with-raspberry-pi-pico/

Post a Comment for "How to Make an Intruder Alarm With Raspberry Pi Pico"