Building an IoT Intruder Alarm with a Raspberry Pi Pico W - PIR Detection and Telegram Alerts
In this project, I designed and built an
IoT-enabled intruder alarm using a Raspberry Pi Pico W, a PIR motion sensor, a
buzzer and an LED. When the system is armed and motion is detected, it sounds a
local alarm and sends a real-time alert to the homeowner's phone through a
Telegram bot, wherever they are.
The brief came from a (scenario) security
systems company whose existing product only rang a local bell. They wanted an
upgrade that could notify the owner remotely, stay affordable, run reliably and be usable without any technical knowledge. I built the system one component
at a time, testing each part on its own before combining them, so every feature
in the final build was backed by a confirmed test.

Pico on breadboard with PIR sensor, LED
and buzzer connected
Project Overview
The system follows a Machine to Machine
(M2M) IoT architecture split into three domains:
·
Device domain: the Pico W reads the PIR
sensor and drives the buzzer and LED
·
Network domain: Wi-Fi (IEEE 802.11)
connects the device to the LAN and out to the internet
·
Application domain: Telegram acts as the
user interface for alerts and remote control
Several factors were considered throughout:
·
Keeping the total hardware cost
under £30
·
Making alerts fast enough to be
useful in a real break-in
·
Preventing false alarms caused
by sensor noise
·
Making sure only the owner can
arm or disarm the system remotely
Tools Used
Hardware
·
Raspberry Pi Pico W
·
HC-SR501 PIR motion sensor
·
Active buzzer
·
1 x LED and 1 x 330 Ohm
resistor
·
Breadboard, jumper wires and a
micro USB cable
Software
·
Thonny IDE
·
MicroPython
·
Telegram Bot API (via
BotFather)
·
timeapi.io for timestamps
Wiring
|
Component |
Pico Pin |
|
PIR VCC |
VSYS (5V) |
|
PIR OUT |
GPIO 16 |
|
Buzzer (+) |
GPIO 5 |
|
LED (via 330Ω resistor) |
GPIO 9 |
|
All grounds |
GND |

Close-up of the Pico pin connections
Step 1: Testing the Pico and the LED
Before connecting any external components,
I needed to confirm the Pico W was working and that I could control a GPIO
output. The onboard LED is the simplest way to do this because it needs no
extra wiring.
|
from
machine import Pin |
Once that worked, I moved the LED to GPIO 9
and used a while True loop to flash it every 0.5 seconds. This confirmed the external
wiring and prepared the flashing pattern I would later use for the alarm.
Step 2: Testing the Buzzer
The buzzer is the main local alarm output.
Because it is an active buzzer, it produces a tone on its own when powered, so
I only needed to switch GPIO 5 on and off. The buzzer gave a continuous tone
when on, confirming it was wired correctly.

Buzzer wired to the breadboard
Step 3: Testing the PIR Motion Sensor
The PIR sensor is the core detection
component. It detects changes in infrared radiation caused by movement and
outputs a digital HIGH (1) or LOW (0).
I set GPIO 16 as an input with a pull-down
resistor and printed the reading in a loop. When I waved my hand in front of
the sensor, the value changed to 1.
|
pir
= Pin(16, Pin.IN, Pin.PULL_DOWN) |
I chose a PIR sensor over an ultrasonic sensor because PIR responds to body heat and movement, which makes it better suited to detecting people. Ultrasonic sensors are designed for measuring distance and are more easily affected by obstacles.
Step 4: Fixing False Triggers with a Debounce Filter
During testing I noticed a single hand wave
was triggering the sensor 3 to 5 times. In the full system, that would mean
several Telegram alerts for one event.
To fix this, I added a 300ms debounce
filter. The sensor now has to stay HIGH for at least 300ms before the movement
counts as a confirmed detection.
|
DEBOUNCE
= 300 |
I also added a cooldown timer so that one
confirmed movement can't flood the user with repeated alerts.
Step 5: Connecting to Wi-Fi
The Pico W needs an internet connection to
send alerts, so I tested Wi-Fi on its own before adding any Telegram code. I
created a WLAN object in station mode, connected to my network and printed the
IP address to confirm the connection worked.
|
wlan
= network.WLAN(network.STA_IF) |
In the final build I added reconnection
logic so the device restores its connection automatically if Wi-Fi drops.
Step 6: Sending Telegram Alerts
I created a Telegram bot with BotFather,
then found my chat ID by messaging the bot and checking the getUpdates endpoint. The Pico
sends alerts as an HTTPS POST to the sendMessage endpoint.
|
URL
= 'https://api.telegram.org/bot' + BOT_TOKEN + '/sendMessage' |
The message appeared on my phone within 2
seconds.
Step 7: Remote Arm and Disarm
To control the alarm without touching the
hardware, the Pico polls the getUpdates endpoint every 2 seconds and checks for /arm, /disarm, /status and /logs commands. An offset stops it from
re-reading old messages.
Security
consideration: anyone who finds the bot could try
sending it commands. To stop this, the system only accepts commands from my
specific Telegram chat ID, and all traffic to the Telegram API is encrypted
over HTTPS. This follows the risk-management guidance in ISO/IEC 27400 for IoT
security and privacy.
Step 8: Event Logging
The client needed an audit trail of every
detection and every arm or disarm action. I wrote each event to a log.txt file on the Pico's flash
memory, with a timestamp from timeapi.io. Because the file is stored on flash,
it survives reboots, and the /logs command sends the full log to Telegram.
|
def
log_event(event_type, detail=''): |
Testing Against Client Requirements
|
Requirement |
Result |
|
Detect movement using a PIR sensor |
Yes: Detects motion within 300ms on GPIO 16 |
|
Send a real-time alert to the user's phone |
Yes: Telegram alert delivered in 1-2 seconds |
|
Arm and disarm remotely |
Yes: /arm and /disarm work via Telegram |
|
Local alarm (buzzer and LED) |
Yes: Siren pattern on GPIO 5 and GPIO 9 |
|
Log all events with timestamps |
Yes: Written to log.txt on flash |
|
Retrieve the log remotely |
Yes: /logs returns the full
log |
|
Hardware cost under £30 |
Yes: Total cost was £14.50 |
|
Run continuously without crashing |
Yes: Ran for 12 hours without failure |
Optimisation Decisions
Looking back, a few changes would improve
the system:
·
A cleaner log format, for
example one line per event with clear labels
·
A fallback alert method, such
as SMS or email, for when Wi-Fi is down
·
Tuning the PIR sensitivity and
delay potentiometers to further reduce false positives
·
An enclosure and battery
backup, so the device can't simply be unplugged by an intruder
Key Takeaways
1.
Testing each component on its
own before combining them made faults easy to isolate. Every feature in the
final build had already been confirmed working.
2.
Raw sensor input is noisy. A
simple 300ms debounce and a cooldown timer turned 3 - 5 alerts per movement into
one.
3.
Even a small IoT device needs
basic security. Restricting control to a single chat ID and using HTTPS
protects the arm/disarm function from unauthorised access.
4.
Logging events with timestamps
produced an audit trail that can be reviewed remotely, the same principle used
in SOC monitoring.
5.
A working, measurable prototype
is possible on a tight budget. The full build cost £14.50 against a £30 target.




Comments
Post a Comment