Skip to content

Search the documentation

Kit
Firmware

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 blocks of this example, from top to bottom.
Click the picture to view it larger.Open full sizeThe same program written as Python is further down this page, in The Python code.
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
    • wait 0.2 seconds
  • A Smart Home board with the PIR sensor (the white plastic dome) and the buzzer.
  • A USB cable and the serial monitor.
  1. Open 05-motion-alarm.mobot in MoBot.
  2. Plug in, pick the port, press Send to Device.
  3. 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.
  4. Now wave your hand in front of the dome.
  • [MOBOT] Watching for movement once, 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.

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 1 there 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.

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 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.

05-motion-alarm.firmware-3.main.py
# 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 Pin
import 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)
  • 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-alarm first, because that block swaps red and green.