Skip to content
MakerBuddy

Motion Sensor Alarm: Build a Room Security Alarm With a PIR Sensor

Build a room security alarm that beeps and flashes red when someone walks in. You get the parts list, two no-code rules, where to put the sensor and a checklist to prove it works.

By the MakerBuddy teamPublished 19 min read

A student walks in through an open door across the fan of detection zones from a white-domed PIR sensor plugged into a board on a shelf, whose colour light glows red while its buzzer beeps
A student walks in through an open door across the fan of detection zones from a white-domed PIR sensor plugged into a board on a shelf, whose colour light glows red while its buzzer beeps

A motion sensor alarm watches a room with a PIR sensor and sounds a buzzer when something warm moves in front of it. You can build one in about an hour with three parts: a PIR motion sensor to notice people, a small computer to decide, and a buzzer and a light to raise the alarm. This guide builds that alarm on the MakerBuddy board with two no-code rules. One rule beeps and flashes red while there is movement. The other turns the buzzer off and shows green when the room is quiet.

You will also find where to put the sensor, how to arm it, four upgrades and a test checklist. This is a learning build, a burglar alarm project for a desk or classroom, not real home security.

How a motion sensor alarm works

Every security alarm system has the same three jobs. Wikipedia's article on security alarms (opens in a new tab) names the parts: sensors detect an intruder, a control panel reads the sensors and tracks whether the system is armed, and alerting devices such as sirens show that something is wrong. Your room alarm has the same three parts, only smaller.

Job
Notice
In a home security system
Several sensors, such as motion detectors
In your room alarm
One PIR motion sensor (HC-SR501)
Job
Decide
In a home security system
A control panel with a keypad
In your room alarm
The ESP32 on the MakerBuddy board, running two rules
Job
Warn
In a home security system
A siren, a strobe light, a call to a monitoring centre
In your room alarm
The buzzer and the colour light

PIR stands for passive infrared. Anything warm, including you, gives off infrared light that eyes cannot see. The PIR takes no pictures. It notices when the pattern of warmth in front of it changes, and switches its output pin on. Adafruit's PIR overview (opens in a new tab) says PIRs are almost always used to detect whether a person has moved in or out of range. Our guide to how a PIR motion sensor works, its range and its settings explains the inside of the sensor in detail.

The room alarm as three jobs: NOTICE, where the PIR motion sensor sees a person cross its zones; DECIDE, where the ESP32 runs two rules, Intruder (Motion = True) and AllClear (Motion = False); and WARN, where the buzzer beeps and the colour light shows red for motion and green when the room is quiet. In order: the PIR output goes HIGH, the Intruder rule starts and the alarm goes off.
The room alarm as three jobs: NOTICE, where the PIR motion sensor sees a person cross its zones; DECIDE, where the ESP32 runs two rules, Intruder (Motion = True) and AllClear (Motion = False); and WARN, where the buzzer beeps and the colour light shows red for motion and green when the room is quiet. In order: the PIR output goes HIGH, the Intruder rule starts and the alarm goes off.

Parts list for the room security alarm

Everything below is part of the MakerBuddy IoT Kit V1.0. The board comes in purple or green; both colours are the same board.

Part
HC-SR501 PIR motion sensor
Where it goes
Plug-in socket marked 18 (GPIO 18)
Its job
Notices a warm body moving
Needed?
Yes
Part
Buzzer
Where it goes
Built into the board (GPIO 4)
Its job
Beeps while the alarm is on
Needed?
Yes
Part
Colour light (RGB)
Where it goes
Built into the board
Its job
Red for alarm, green for all clear
Needed?
Yes
Part
A laptop, iPhone or iPad
Where it goes
On the same 2.4 GHz Wi-Fi as the board
Its job
Creates the rules, arms and disarms
Needed?
Yes
Part
1602 text screen
Where it goes
Plug-in socket (I2C)
Its job
Shows a message that stays after the alarm
Needed?
Upgrade
Part
Light sensor (LDR)
Where it goes
Built into the board (GPIO 35)
Its job
Makes a night-only alarm
Needed?
Upgrade
Part
HC-SR04 distance sensor
Where it goes
Plug-in socket (trig 15, echo 2)
Its job
Double-checks a doorway
Needed?
Upgrade

You also need a micro-USB cable and a 5 V USB charger. On a laptop, use Chrome, Edge or Firefox with the free MakerBuddy extension, or Safari on a Mac. On an Android phone, use the free MakerBuddy app from Google Play.

The kit's buzzer plays one tone. It is a beeper, not a siren. Why some buzzers play tunes and others only beep is explained in active vs passive buzzers and how to tell them apart.

Plan the alarm before you build it

Plan on paper first: write what should happen in each situation, then turn each line into a rule.

text
IF   motion is true    THEN  flash red and beep, again and again   (Rule 1: Intruder)
IF   motion is false   THEN  buzzer off, light green              (Rule 2: AllClear)

Why two rules? A MakerBuddy rule has no ELSE box. "Otherwise, stop the alarm" has to be its own rule with the opposite test. Week 28's gas alarm uses the same pair, an alarm rule and an all-clear rule, in Smart Safety Systems. The guide to if-then statements explained with real sensors shows the same idea with other sensors.

Both rules use the Condition loop mode. A Condition rule watches its sensor test and runs while the test is true. It needs the test to stay true for 0.3 seconds before it starts. When the test turns false, it finishes the round it is in, then stops. These rules run on the board itself, so the alarm keeps working after you close the dashboard. The rules section of the technical details explains this in full.

Flowchart of the two rules. If Motion = True, Intruder runs RGB Red, Buzzer On, wait 0.5 s, Buzzer Off, RGB Off, wait 0.5 s, a 1-second round; if not, AllClear runs Buzzer Off, RGB Green, wait 0.5 s. Both then check again, and every round ends quiet, so the alarm cannot get stuck beeping when motion stops.
Flowchart of the two rules. If Motion = True, Intruder runs RGB Red, Buzzer On, wait 0.5 s, Buzzer Off, RGB Off, wait 0.5 s, a 1-second round; if not, AllClear runs Buzzer Off, RGB Green, wait 0.5 s. Both then check again, and every round ends quiet, so the alarm cannot get stuck beeping when motion stops.

Worked example: build the alarm with two no-code rules

Step 1: plug in the sensor and let it settle

  1. 1

    Unplug the USB cable. Plug the PIR into the three-pin socket marked 18. It fits only one way, so never force it.

  2. 2

    Plug the USB cable back in and open the dashboard.

  3. 3

    Wait one full minute before you test anything. The HC-SR501 datasheet (opens in a new tab) says the module needs about a minute after power-on and may switch its output on up to three times while it settles.

  4. 4

    Sit still and watch the Motion Sensor card. It should show None. Wave your hand: it should change to Motion within about 3 seconds.

Week 13, Security Sensing teaches this part, and its Try It demo lets you practise in the browser before you touch the hardware.

Step 2: set the two knobs on the back of the PIR

The HC-SR501 has two small knobs. In class, ask your teacher before you turn them.

  • Time knob: sets how long the PIR output stays on after it sees movement. The datasheet's adjustment notes give about 5 seconds fully anticlockwise and about 300 seconds fully clockwise. Start with it fully anticlockwise, so tests are quick.
  • Sensitivity knob: sets how far it reaches, about 3 metres anticlockwise to about 7 metres clockwise, according to the same HC-SR501 datasheet (opens in a new tab). Start in the middle.

Leave the trigger jumper on H. On H, every movement restarts the timer, so the output stays on while someone keeps moving.

The time knob decides how long your alarm rings, because the rules keep beeping while the PIR output is on.

Step 3: Rule 1, the Intruder alarm

Open the Rule Engine tab and create a rule:

  • Name: Intruder
  • Loop Mode: Condition
  • Sensor: Motion Sensor, Operator: Equal to (==), Threshold: True

Add four steps:

Step
1
Action
RGB Red
Delay (ms)
0
Why
The light turns red at once
Step
2
Action
Buzzer On
Delay (ms)
500
Why
Beep for half a second
Step
3
Action
Buzzer Off
Delay (ms)
0
Why
Stop the beep
Step
4
Action
RGB Off
Delay (ms)
500
Why
Dark and quiet for half a second

Save Rule. One round takes 1 second. While motion is still true at the end of a round, the rule runs it again, so the alarm flashes and beeps once a second.

The round ends quiet on purpose. In a rule, Buzzer On keeps the buzzer on until a Buzzer Off step, as the automation rules documentation notes. Ending every round quiet means the alarm cannot get stuck beeping when the rule stops.

Step 4: Rule 2, the AllClear rule

Create a second rule:

  • Name: AllClear
  • Loop Mode: Condition
  • Sensor: Motion Sensor, Operator: Equal to (==), Threshold: False
Step
1
Action
Buzzer Off
Delay (ms)
0
Why
A safety net: the buzzer is always silenced
Step
2
Action
RGB Green
Delay (ms)
500
Why
Green means armed and quiet

Save Rule. While nobody moves, this rule runs every half second and keeps the light green.

Keep its wait short. When someone walks in, AllClear finishes its round and holds the colour light until its wait ends, so a 5-second wait could delay your alarm by 5 seconds.

Step 5: predict, then walk in

Predict what will happen before you test it. With the time knob fully anticlockwise, the timings come out like this:

Time
0 s
What happens
You walk across the sensor's view. The PIR output switches on
Time
0 to 1 s
What happens
The board reads the motion sensor, once a second
Time
0.3 to 1.8 s
What happens
The 0.3-second check passes, AllClear finishes its last wait, and Intruder starts: red and beeping
Time
While you move
What happens
The output stays on, so the alarm keeps beeping once a second
Time
About 5 s after you stop
What happens
The PIR output switches off
Time
Up to 2.3 s later
What happens
The board sees False, Intruder finishes its round, AllClear turns the light green

The 1-second read, the 0.3-second check and the finish-the-round behaviour come from the rules section of the technical details. The 5-second figure assumes the datasheet's shortest on-time, so time your own sensor.

Where to put the sensor in a room

A PIR notices people walking across its view far more easily than people walking straight towards it. The HC-SR501 datasheet (opens in a new tab) says that walking straight at it can leave the sensor "insensitive or does not work", and asks you to mount it so people pass from side to side.

Situation
How far it sees
What the datasheet gives
Within 7 m; the knob sets about 3 m to 7 m
What to do
A bedroom or classroom, not a sports hall
Situation
How wide it sees
What the datasheet gives
Under 120 degrees (page 1), at most 110 degrees (page 4)
What to do
Aim it across the room, not at a wall
Situation
Hot rooms
What the datasheet gives
Range gets slightly shorter at 30 to 32 °C
What to do
Expect less range in a hot summer room
Situation
Lights and moving air
What the datasheet gives
Can cause false signals
What to do
Keep it away from lamps, heaters, fans and vents

All four rows come from the HC-SR501 datasheet (opens in a new tab), which disagrees with itself on the angle, so test your own room.

The best spot is a side wall or shelf near the door, looking across the doorway, so anyone who enters crosses its zones. Avoid facing the door from across the room, sitting above a heater or fan, or pointing at a sunny window. Adafruit (opens in a new tab) adds that PIRs are sometimes set off by pets, so keep the cat out of the test.

A room seen from above, with the PIR on the side wall next to the door and its fan of zones reaching about 7 m. A person coming in through the door crosses the zones and is detected; a person walking straight at the sensor gives a weak signal; a sunny window, a fan and a heater inside the zones are marked as false-alarm sources.
A room seen from above, with the PIR on the side wall next to the door and its fan of zones reaching about 7 m. A person coming in through the door crosses the zones and is detected; a person walking straight at the sensor gives a weak signal; a sunny window, a fan and a heater inside the zones are marked as false-alarm sources.

Test checklist: prove your alarm works

A Wikipedia summary of a US Department of Justice estimate (opens in a new tab) says that 94% to 98% of alarm calls to police are false alarms. The same article says system reliability and user error cause most false alarms. An alarm nobody trusts is useless, so test yours properly. Do each test, write down what happened and tick it only when it passes.

No.
1
Test
Warm-up
How
Power on, wait 60 s without moving
Pass when
No alarm after the first minute
No.
2
Test
Quiet room
How
Leave the room still for 2 minutes
Pass when
The light stays green, no beeps
No.
3
Test
Walk in
How
Walk across the doorway at normal speed
Pass when
Red and beeping within about 2 seconds
No.
4
Test
Alarm stops
How
Stand still out of view
Pass when
Green again within about 10 seconds
No.
5
Test
Straight approach
How
Walk slowly straight at the sensor from 4 m
Pass when
Note the distance where it triggers
No.
6
Test
Range
How
Cross the room at 1 m, 3 m and 5 m
Pass when
Note the farthest that works
No.
7
Test
Slow creep
How
Cross the view very slowly
Pass when
Note whether it notices you
No.
8
Test
Dashboard closed
How
Close the dashboard, walk in
Pass when
It still beeps: rules run on the board
No.
9
Test
Power cut
How
Unplug, plug back in, wait a minute, walk in
Pass when
It beeps again: rules are saved on the board
No.
10
Test
Disarm
How
Stop the Intruder rule, walk in
Pass when
No alarm

Tests 5 to 7 echo the Red Light, Green Light challenge from Week 13. Write the results down: they show the alarm's weak spots.

How to arm and disarm the alarm

A real alarm has a keypad. Yours has the Rule Engine tab.

  • To disarm: press Stop on the Intruder rule. The alarm stops watching. A stopped rule stays stopped after a power cut, until someone starts it again.
  • To arm: press Start on the Intruder rule. On firmware 2.10, Start sends a Condition rule back to watching, and it runs once motion is true.
  • Leave AllClear running all the time. Pressing Stop never switches outputs off, so if you stop Intruder in the middle of a beep, the buzzer could stay on. AllClear switches it off as soon as nobody is moving.

While a rule watches, the dashboard locks the cards for the parts it uses, so you cannot fight the rules by hand. AllClear also uses the buzzer and the colour light, so their cards stay locked while it runs, even after you stop Intruder.

The exit-delay problem

Real alarms give you time to leave after you arm them. This one does not: press Start in the room and it sees you walk out. Arm it from outside the door instead, on an iPhone, iPad or laptop on the same Wi-Fi. The board also has no clock, so it cannot arm itself at 10 pm; the night-only upgrade uses darkness instead.

Four upgrades for your burglar alarm project

Each upgrade changes the tests or adds a step. AllClear must always be the exact opposite of Intruder. Otherwise there will be moments when neither rule runs, or when both do.

Night-only alarm with the light sensor

Cover the light sensor with your hand and read the Light Level card, for example 15. Read it again in normal light, for example 70. Pick a number between them, such as 30.

  • Intruder: Motion Sensor equal to True, then tick Add a second sensor, choose AND, and add Light Level (LDR) less than 30.
  • AllClear: Motion Sensor equal to False, OR Light Level (LDR) at least (>=) 30.

The opposite of "motion AND dark" is "no motion OR bright". That swap is De Morgan's law, and the guide to visual programming for hardware and how no-code rules teach real logic works through it. Week 27, Range Logic and Multiple Sensors is the lesson for two-sensor rules.

Double-check the doorway with the distance sensor

Aim the HC-SR04 across the doorway, like an invisible tripwire. Read the Distance card with nobody there, for example 90 cm. Someone in the doorway will read less, so pick a number clearly below it, such as 70.

  • Intruder: Motion Sensor equal to True AND Distance (HC-SR04) less than 70.
  • AllClear: Motion Sensor equal to False OR Distance (HC-SR04) at least 70.

Now both sensors must agree. Wikipedia's article on motion detectors (opens in a new tab) describes dual-technology sensors that work the same way: both must trip together, which lowers the chance of a false alarm. The downside: fool either sensor and the alarm stays quiet. The HC-SR04 measures 2 cm to 400 cm, according to its datasheet (opens in a new tab), and the guide to how an ultrasonic sensor measures distance with sound covers it.

A message that stays on the screen

Plug in the 1602 text screen and add a fifth step to Intruder: LCD Display, Custom Text, line 1 INTRUDER ALERT, line 2 Check the room. Both fit the screen's 16 columns.

The message stays after the alarm stops, so you know it went off while you were out. A screen step shows no Delay box, but the dashboard still gives a new step a 1-second wait, so the beat can slow to one beep every 2 seconds. Time one round to check. To clear the message, stop Intruder, then choose Default on the LCD card. With no clock, the board cannot say when it happened, so keep a logbook, like the Intruder Alert idea in the MakerBuddy project idea library. Week 19, Visual Output teaches the screen.

Silent alarm

Delete the buzzer steps from Intruder and keep the red flashing. You see that someone came in, and they never know.

Version
Basic alarm
Extra part
None
Intruder test
Motion = True
AllClear test
Motion = False
What it teaches
Condition rules, clean-up steps
Version
Night-only
Extra part
Light sensor (built in)
Intruder test
Motion = True AND Light < 30
AllClear test
Motion = False OR Light >= 30
What it teaches
AND, OR and opposites
Version
Doorway double-check
Extra part
HC-SR04
Intruder test
Motion = True AND Distance < 70
AllClear test
Motion = False OR Distance >= 70
What it teaches
Two sensors agreeing, false alarms
Version
Message alarm
Extra part
1602 text screen
Intruder test
Motion = True
AllClear test
Motion = False
What it teaches
Memory of an event
Version
Silent alarm
Extra part
None
Intruder test
Motion = True
AllClear test
Motion = False
What it teaches
Designing for the user

Room alarm vs a real security alarm system

Feature
Sensors
Home security alarm system
Several: doors, windows, motion
MakerBuddy room alarm
One PIR, plus optional extras
Feature
Arming
Home security alarm system
A keypad with a code
MakerBuddy room alarm
The Rule Engine tab on a trusted device
Feature
Warning
Home security alarm system
A loud siren and often a call to a monitoring centre
MakerBuddy room alarm
A one-tone buzzer and a colour light
Feature
Memory
Home security alarm system
An event log with times
MakerBuddy room alarm
No clock, so no times; an optional screen message
Feature
Security of its own
Home security alarm system
Protected controls
MakerBuddy room alarm
The board's local web connection has no password
Feature
Power
Home security alarm system
Usually its own supply
MakerBuddy room alarm
5 V USB: unplug it and it stops

The left column follows the parts in Wikipedia's security alarm article (opens in a new tab). The no-password row matters most: anyone on the same Wi-Fi could open the dashboard and stop your alarm. Use the board only on a Wi-Fi network you trust and never open it to the internet, as the security notes in the technical details explain. Treat the alarm as a model of how real ones think, never as protection for a home, a shop or a person.

Common mistakes and how to fix them

The alarm goes off by itself just after power-on

  • Symptom: one or two beeps in the first minute, with nobody there.
  • Cause: the PIR's warm-up. The HC-SR501 datasheet (opens in a new tab) allows up to three outputs in the first minute. Rules are saved on the board and start watching at power-on.
  • Fix: stop Intruder before you unplug the board. After power-on, wait a minute, then start it again.

The buzzer will not stop

  • Symptom: a steady tone that keeps going after everyone has left.
  • Cause: a round that ends with Buzzer On, or an AllClear rule that is stopped or missing its Buzzer Off step.
  • Fix: make Buzzer Off the third step of Intruder and the first step of AllClear, and keep AllClear running.

The light gets stuck or flickers between red and green

  • Symptom: after an upgrade, the light stays on one colour, or red and green fight each other.
  • Cause: the two tests are not exact opposites. If Intruder uses Motion = True AND dark but AllClear only uses Motion = False, then movement in bright light matches neither rule, and the light keeps whatever it showed last. If the tests overlap, both rules run at once and fight.
  • Fix: write AllClear as the exact opposite of Intruder, swapping AND for OR and each comparison for its opposite.

It beeps when nobody is there

  • Symptom: alarms in an empty room, often on sunny afternoons.
  • Cause: warm air from a heater, a fan or a vent, or sunlight moving across the floor. The HC-SR501 datasheet (opens in a new tab) warns about lights near the lens and moving air.
  • Fix: move the sensor, turn the sensitivity down a little, or add the doorway double-check.

The alarm starts late or keeps ringing for minutes

  • Symptom: a long wait before the alarm, or a long tail after.
  • Cause: a long Delay in AllClear holds up the red light, and a time knob turned far clockwise keeps the PIR output on for up to about 300 seconds.
  • Fix: keep AllClear's wait at 500 ms or less, and turn the time knob anticlockwise.

Use the alarm in class or at a science fair

The build fits one 45 to 60 minute lesson for students aged 10 and up. Week 13 introduces the sensor, Week 26, Sensor-Based Control introduces Condition rules, and Week 28 adds the alarm and all-clear pattern.

Minutes
0 to 10
Activity
Plug in the PIR, wait out the warm-up, read the card
Minutes
10 to 25
Activity
Plan both rules on paper, then build them
Minutes
25 to 45
Activity
Work through the test checklist in pairs
Minutes
45 to 60
Activity
Placement challenge: protect the classroom door, then share results

For a science fair, turn tests 5 to 7 into an experiment. Hypothesis: if a person crosses the sensor's view instead of walking straight at it, the alarm triggers from farther away. Change only the walking direction and measure the trigger distance five times each. More ideas with hypotheses are in electronics science fair projects with sensor ideas, and the pillar list of ESP32 and Arduino project ideas for students has more builds that use the same sensor.

Want to write the alarm in Arduino code instead of rules? The guide to motion sensors with Arduino or ESP32, wiring and code covers that. Uploading your own sketch replaces the MakerBuddy firmware, and the rules with it, until you put the firmware back. The PIR, the buzzer and the colour light all come in the MakerBuddy IoT Kit, and more builds are on the MakerBuddy blog.

FAQ

What is a motion sensor alarm?

A motion sensor alarm sounds a warning when something moves in the area it watches. Most use a PIR sensor, which notices the infrared warmth of a moving person or animal, and a small computer that switches on a buzzer, siren or light.

What are the three main components of an alarm system?

Sensors, a control panel and alerting devices. The sensors notice an intruder, the control panel decides and tracks whether the system is armed, and alerting devices such as a siren raise the alarm. Keypads for arming and disarming are often counted too.

What is a PIR on an alarm?

On an alarm, a PIR is the motion detector. PIR means passive infrared: it senses the warmth that people give off and sends nothing out itself. When that warmth moves across its view, it tells the alarm panel.

How far can a PIR motion sensor alarm detect?

The HC-SR501 in the MakerBuddy kit reaches up to 7 metres, and its sensitivity knob sets about 3 to 7 metres, according to its HC-SR501 datasheet (opens in a new tab). Real range is shorter when people walk straight at the sensor, move very slowly or the room is hot. Test your own room with the checklist above.

Can a pet set off a PIR alarm?

Yes. A cat or dog is warm and moves, so a PIR sees it like a person, and Adafruit's PIR overview (opens in a new tab) notes that pets sometimes set PIRs off. Keep pets out of the protected room, or add a second sensor that must agree.

What is a laser security alarm?

A laser or light-beam alarm shines a beam across a doorway onto a detector. When an intruder breaks the beam, the detector notices the change and sounds the alarm. Science World's burglar alarm activity (opens in a new tab) describes real alarms working this way. A PIR needs no beam, because it senses body warmth across a whole room.

Can I build a motion sensor alarm without coding?

Yes. On the MakerBuddy board you build it from two Condition rules in the browser dashboard: one that beeps and flashes red while Motion Sensor equals True, and one that turns the buzzer off and shows green while it equals False. The rules run on the board, so the alarm works with the dashboard closed.

Can the alarm send a notification to my phone?

Not by itself. The board cannot send email or app notifications. The Motion Email Alert idea in the MakerBuddy project library uses a small program on a computer that reads the motion sensor from the board and sends the email. Readings stay on your local network, and the board needs no account.

Sources

Keep reading

Learn with MakerBuddy

32 free lessons, one kit, no wiring to start.

Every part in these guides has its own lesson. Read them free, or get a kit for your class.