Skip to content

Search the documentation

Kit
Firmware

Doorbell — Smart Home

Press the button on the board and it plays a doorbell: two notes of your own, then the ready-made doorbell tune.

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 "Press the button"
  • repeat forever
    • wait 2 seconds
    • if is Button pressed ?
      • Print to serial "Ding dong!"
      • Play Note E5 Duration: 300
      • Play Note C5 Duration: 500
      • Play sequence : Doorbell
  • A Smart Home board with the button and the buzzer. Nothing to plug in.
  • A USB cable and the serial monitor.
  1. Open 09-doorbell.mobot in MoBot.
  2. Plug in, pick the port, press Send to Device.
  3. Press and hold the button for a moment — about a second.

In this order, every time:

  1. [MOBOT] Ding dong! in the serial monitor.
  2. A high note (E5), about a third of a second.
  3. A lower note (C5), about half a second.
  4. The doorbell tune — several notes, played by the board on its own.

Then a couple of seconds of quiet before it will ring again.

The test that matters is the order. All four things, every time, in that order. If the two notes play but the tune never does, or the tune cuts itself off half way, something is wrong with the buzzer’s background timer — say so, it is worth knowing.

Hold the button for a moment. The board looks at the button once every two seconds, so a very quick tap can fall in between two looks and be missed entirely. That is not the button failing.

If it rings without anyone touching the button, the button is stuck or miswired. If nothing happens at all, check the serial monitor: no Ding dong! means the button is never being seen.

This is the thing worth teaching from this example.

  • Play Note waits. The program stops at that block until the note is over. So the two notes play one after the other with no gap to arrange.
  • Play sequence does not wait. It starts the tune in the background and the program carries straight on. That is why the wait 2 matters: without it the loop would come round, see the button still held down, and start the tune again from its first note — so you would only ever hear the first note.

Putting the wait at the start of the loop rather than after the ring looks odd, and it is deliberate.

The two note blocks make the program wait while they sound, but the Play sequence block starts its tune and goes on at once. The two seconds at the top of the loop are what give that tune time to finish before the button is looked at again.

Both note blocks say Duration, and both are in milliseconds. 300 is about a third of a second; 1000 is one second. The wait blocks next to them are in seconds. Two different units on two neighbouring blocks — worth saying out loud to a class before somebody types 1 and hears nothing.

block what it does
iot_button True while the button is held down.
controls_if Rings only when the button is down.
iot_playnote One named note, from C4 up to B5. Waits while it plays.
iot_playsequence One of 23 ready-made tunes. Does not wait.
wait Two seconds — the gap between one ring and the next.

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.

09-doorbell.firmware-3.main.py
# Doorbell — iot (firmware 3.0)
#
# This is what MoBot puts on a robot running firmware 3.0 as main.py when you open
# 09-doorbell.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 09-doorbell.main.py.
from machine import Pin
import time
from sounds import SEQUENCES
print("[MOBOT]", 'Press the button')
while True:
time.sleep(2)
if home.button.is_pressed():
print("[MOBOT]", 'Ding dong!')
home.buzzer.play_note('E5', 300)
home.buzzer.play_note('C5', 500)
home.buzzer.play_notes(SEQUENCES.get('doorbell'))
  • The tune dropdown has 23 choices. victory, game_over, coin and level_up are the ones a class will ask for.
  • Write a real melody with four or five Play Note blocks in a row — E5, D5, C5, D5, E5 is the start of a song most children know. Keep Play sequence last, or delete it.
  • Make the wait 0.5 and the doorbell answers almost instantly — but hold the button down and hear the tune stutter as the loop restarts it.