Skip to content

Search the documentation

Kit
Firmware

Stop before you hit it — Smart Car

The ultrasonic sensor on the front of the robot measures how far away things are, by sending out a click too high for you to hear and timing the echo. This program creeps forward while the way is clear and backs off, with a warning sound, whenever something comes closer than 15 cm.

This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Smart Car.

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 "Obstacle sensor. Wave your hand in front of the robot."
  • wait 2 seconds
  • repeat forever
    • Print to serial (create text with "distance in cm = " Read ultrasonic sensor in cm)
    • if (Read ultrasonic sensor in cm < 15)
      • Set LED 1 ON
      • Play Error sound
      • set Motor 1 to Direction 2 with speed 50 %
      • set Motor 2 to Direction 2 with speed 50 %
      • wait 0.6 seconds
      • set Motor 1 to Direction 1 with speed 0 %
      • set Motor 2 to Direction 1 with speed 0 %
    • else
      • Set LED 1 OFF
      • set Motor 1 to Direction 1 with speed 35 %
      • set Motor 2 to Direction 1 with speed 35 %
    • wait 0.2 seconds
  • A Smart Car with the ultrasonic sensor (the one that looks like two eyes) fitted on the front.
  • A USB cable.
  • A clear table or floor, and your hand.
  1. Open 07-obstacle-avoid.mobot, plug in, press Send to Device.
  2. Put the robot on the floor with plenty of space in front of it.
  3. Let it creep forward, then put your hand in its way.
  • The serial monitor prints distance in cm = … about five times a second. Move your hand towards the sensor and the number must get smaller. That line alone is the sensor test.
  • With nothing in front: LED 1 is off and the robot creeps forward slowly.
  • Bring your hand within about 15 cm: LED 1 lights, the buzzer plays a short error buzz, and the robot reverses for about half a second and stops.
  • Take your hand away: LED 1 goes out and it creeps forward again.

The test passes if the printed distance changes as you move your hand, and if the robot reacts at roughly a hand’s width and a half. Measure it with a ruler once — 15 cm should be 15 cm.

When the number is 200, and when it is −1

Section titled “When the number is 200, and when it is −1”

If nothing is in range, the sensor never hears an echo, and the driver reports the far end of its range: about 200 cm. In an empty room the printed number sits near 200 and the robot creeps forward.

A −1 is rare: it means the sensor could not be read at all. And −1 is less than 15, so the robot treats “I could not read anything” as “something is right in front of me” and backs away. If your robot reverses in an empty room, look at the printed number: a steady −1 points to a problem with the sensor or its wires, not to a ghost.

That is worth showing a child rather than hiding: sensors do not always answer.

block what it does
mobot_distance how far away, in cm or mm. −1 means “no answer”.
logic_compare is the distance less than 15?
mobot_playsound the warning buzz.
mobot_motordirspeed forward at 35 %, backwards at 50 %.

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.

07-obstacle-avoid.firmware-3.main.py
# Stop before you hit it — robotics (firmware 3.0)
#
# This is what MoBot puts on a robot running firmware 3.0 as main.py when you open
# 07-obstacle-avoid.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 07-obstacle-avoid.main.py.
from machine import Pin
import time
from indicators import SONGS
print("[MOBOT]", 'Obstacle sensor. Wave your hand in front of the robot.')
time.sleep(2)
while True:
print("[MOBOT]", ('distance in cm = ' + str(robot.distance_sensor.distance_cm())))
if robot.distance_sensor.distance_cm() < 15:
robot.led1.set(1)
robot.buzzer.play_song(SONGS.get("error"))
robot.left_motor.forward(50)
robot.right_motor.forward(50)
time.sleep(0.6)
robot.left_motor.backward(0)
robot.right_motor.backward(0)
else:
robot.led1.set(0)
robot.left_motor.backward(35)
robot.right_motor.backward(35)
time.sleep(0.2)
  • Change cm to mm in the distance block — and then remember to change 15 into 150, or the robot will panic at everything.
  • Change 15 to 40 and the robot becomes very shy.
  • Add a second if: when the distance is under 5 cm, play the siren instead. Now it has two levels of alarm.