Motion alarm — Smart Home
The board watches the room with its PIR motion sensor. Sit still and nothing happens. Wave, walk past, or stand up — and it beeps.
This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Smart Home.
The program
Section titled “The program”Read the program as text
- MoBot Start
- Print to serial
"Watching for movement" - repeat forever
- if Motion Detected (PIR) ?
- Print to serial
"Movement!" - Play Tone Frequency: 1200 Duration: 300 .
- wait 2 seconds
- Print to serial
- wait 0.2 seconds
- if Motion Detected (PIR) ?
What you need
Section titled “What you need”- A Smart Home board with the PIR sensor (the white plastic dome) and the buzzer.
- A USB cable and the serial monitor.
How to run it
Section titled “How to run it”- Open 05-motion-alarm.mobot in MoBot.
- Plug in, pick the port, press Send to Device.
- Stand back and keep still for about a minute. A PIR sensor needs that long to settle after power-up, and until it has it will beep at nothing.
- Now wave your hand in front of the dome.
What you should see and hear
Section titled “What you should see and hear”[MOBOT] Watching for movementonce, at the start.- Then silence, and no printed lines, while the room is still.
- Wave a hand and you get, together:
[MOBOT] Movement!in the serial monitor,- one short high beep from the buzzer.
- Then two seconds of quiet before it will beep again, however much you wave. That is the two-second wait inside the if, so one person walking past makes one beep and not thirty.
The real test is the silence. A PIR that beeps every 0.2 seconds with an empty room in front of it is not detecting anything — it is stuck high. Give it the settling minute first, and try again with the dome pointed at a blank wall.
If it never beeps, check you are in front of the dome, not behind the board. A PIR sees a cone in front of it, and it notices warm things moving — a hand, a person, a cat. It will happily ignore a book waved at it.
About the beep
Section titled “About the beep”The block says Play Tone Frequency: 1200 Duration: 300.
- Frequency 1200 is how high the note is, in hertz. 200 is a low growl, 4000 is a shriek.
- Duration 300 is in milliseconds, not seconds — a third of a second. If
you type
1there hoping for a one-second beep you will get a click you can barely hear. For one second, type 1000.
This block waits while it plays: the program stops at that line until the beep is over.
The blocks
Section titled “The blocks”| block | what it does |
|---|---|
iot_pir |
True when the sensor sees movement, false otherwise. |
controls_if |
Does the beeping only when the answer is true. |
iot_buzzer |
Plays one tone. Frequency in hertz, duration in milliseconds. |
wait |
The 2 stops the alarm repeating; the 0.2 stops the program spinning flat out. |
The Python code
Section titled “The Python code”The same blocks write different Python for each firmware, and MoBot picks the one that fits the robot you use. The 2.x tab is for reference. This is a reading copy: editing it changes nothing, so change the blocks instead. The name map lists the names that changed.
# Motion alarm — iot (firmware 3.0)## This is what MoBot puts on a robot running firmware 3.0 as main.py when you open# 05-motion-alarm.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.# The firmware 2.x version is 05-motion-alarm.main.py.
from machine import Pinimport time
print("[MOBOT]", 'Watching for movement')while True: if home.motion_sensor.motion_detected(): print("[MOBOT]", 'Movement!') home.buzzer.play_tone(1200, 300) time.sleep(2) time.sleep(0.2)# Motion alarm — iot## This is what MoBot puts on the board as main.py when you open# 05-motion-alarm.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.
from machine import Pinimport time
print("[MOBOT]", 'Watching for movement')while True: if mobotIOT.pir.motion_detected(): print("[MOBOT]", 'Movement!') mobotIOT.buzzer.play_sound(1200, 300) time.sleep(2) time.sleep(0.2)Try changing it
Section titled “Try changing it”- Make it a low warning instead: frequency 300, duration 800.
- Take the two-second wait out and see how a burglar alarm sounds when nobody told it to calm down.
- Add a Set RGB LED block next to the beep so the alarm flashes as well as
sounds — see
03-gas-alarmfirst, because that block swaps red and green.