Skip to content

Search the documentation

Kit
Firmware

Card door — Smart Home

A little door that only opens for your card. Tap a card on the RFID reader and the board prints its number. Tap the right card and the servo swings open, plays a note, waits three seconds and closes again.

This example is in two parts, and the first part is meant to “fail”. You have to find out your card’s number before you can teach the door to recognise it. That is the lesson.

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 "Show a card to the reader"
  • Set Servo Angle 0
  • repeat forever
    • set card to Read RFID Card
    • Print to serial (create text with "Card: " card)
    • if (card = "PUT-YOUR-CARD-NUMBER-HERE")
      • Print to serial "Welcome!"
      • Play Note C5 Duration: 300
      • Set Servo Angle 90
      • wait 3 seconds
      • Set Servo Angle 0
    • wait 1 seconds
  • A Smart Home board with the RFID reader and the servo motor.
  • One or two RFID cards or key fobs (the white cards that come with the kit).
  • A USB cable and the serial monitor.
  • Something light taped to the servo arm makes the “door” much more fun — a strip of card, a paper flag.
  1. Open 06-card-door.mobot in MoBot and press Send to Device.

  2. Open the serial monitor. You will see [MOBOT] Card: None once a second. None means “no card” — that is the reader working correctly with nothing in front of it.

  3. Hold a card flat on the reader. One of the lines changes:

    [MOBOT] Card: None
    [MOBOT] Card: 3906f6945d <- your card!
    [MOBOT] Card: None
  4. Write that number down. Every card has its own, and yours will not be 3906f6945d.

You may have to hold the card still for a second or two: the board only looks once a second, so a quick tap can slip between two looks.

  1. In MoBot, find the block that says PUT-YOUR-CARD-NUMBER-HERE.
  2. Type your card’s number into it instead, exactly as it was printed — all lower case, no spaces.
  3. Press Send to Device again.
  4. Tap your card.
  • [MOBOT] Welcome! in the serial monitor,
  • one clear note from the buzzer,
  • the servo arm swings from 0° to 90°, holds for three seconds, and swings back.

Tap a different card and the number is printed but the door stays shut. That is the test that matters: a door that opens for every card is not a lock.

Why the program remembers the card in a box

Section titled “Why the program remembers the card in a box”

Look at the first block inside the loop: set card to Read RFID Card.

Asking the reader is not free — each ask is a real radio conversation, and two asks in a row can give two different answers (a card may be gone by the second one). So the program asks once, keeps the answer in a box called card, and then uses that same box twice: once to print it, once to compare it. The door can never print one number and unlock on another.

block what it does
iot_rfidread Asks the reader. Gives the card’s number as text, or None.
variables_set / variables_get The box called card.
logic_compare “Is this card the right card?”
iot_servo Turns the servo to an angle from 0 to 180. The angle is a dial on the block.
iot_playnote Plays a named musical note. Duration in milliseconds — 300 is a third of a second.
iot_print, text_join, wait Commentary and timing.

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.

06-card-door.firmware-3.main.py
# Card door — iot (firmware 3.0)
#
# This is what MoBot puts on a robot running firmware 3.0 as main.py when you open
# 06-card-door.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 06-card-door.main.py.
from machine import Pin
import time
print("[MOBOT]", 'Show a card to the reader')
home.servo.set_angle(0)
while True:
card = home.rfid.read_card()
print("[MOBOT]", ('Card: ' + str(card)))
if card == 'PUT-YOUR-CARD-NUMBER-HERE':
print("[MOBOT]", 'Welcome!')
home.buzzer.play_note('C5', 300)
home.servo.set_angle(90)
time.sleep(3)
home.servo.set_angle(0)
time.sleep(1)
  • Add a second if for a second card, with a different note, so you can hear who came in.
  • Change the two servo angles from 0/90 to 0/180 and watch a much bigger swing.
  • Play success from Play sequence instead of a single note.