Skip to content

Search the documentation

Kit
Firmware

Mini eyes — Mini (Python)

This example is written twice, once for each firmware. Pick the tab that matches the firmware of your Mini: the tab you pick is remembered on the other examples.

Firmware 3.0 renamed the Python names a program writes, and kept none of the old ones: a Mini on 2.x does not know the 3.0 names, and a Mini on 3.0 does not know the 2.x ones. The name map lists every change. If you open the example from inside MoBot, it picks the version for the firmware of your Mini. If you download a file from this page, use the one in the tab for your firmware.

A parking sensor, and then a deliberate accident. The first half turns the Mini into a reversing sensor: green when you are far away, red when you are close, and beeps that get faster as your hand comes in. The second half breaks it on purpose, to show you why.

This is the version for a robot on firmware 3.0. For a Mini that still runs firmware 2.x, MoBot opens the 2.x version of this example instead.

Project for firmware 3.0: 04-mini-eyes.firmware-3.mobot. Open it in MoBot.

The Mini starts in Manual driving and only runs your main.py once it is set to Programmable. Press Save and answer Switch to Programmable, or tap the Programmable tile in the MoBot app. Full explanation in 01-mini-drive.

  • A MoBot Smart Car Mini with the ultrasonic distance sensor fitted (trigger → GPIO 27, echo → GPIO 26).
  • A USB cable and the serial monitor.
  • Your hand.

Open 04-mini-eyes.firmware-3.mobot, press Save, and move your hand slowly towards the front of it.

Part 1 — about twenty seconds:

your hand the light the beeps
more than 40 cm away green slow, about twice a second
20–40 cm amber faster
8–20 cm orange fast
closer than 8 cm red very fast, almost a continuous tone

and the distance in centimetres printed the whole time. Wave your hand in and out and you should be able to make the colour walk up and down.

Part 2 — the accident:

The program wakes the two line eyes, and then tries the distance sensor five more times. It stops giving sensible answers: a number stuck at 200 cm whatever you do with your hand.

Run the program again and part 1 works again.

The Mini has two pairs of eyes and only one pair of wires:

ask for takes pin 26 takes pin 27
robot.enable_distance_sensor() echo (listens) trigger (shouts)
robot.enable_line_sensor() left line eye right line eye

enable_line_sensor() turns pin 27 into an input. The distance sensor needs pin 27 to be an output — that is the pin it shouts down. Once it is an input, the shout never happens, no echo comes back, and the sensor answers with its “I heard nothing” number.

Nothing warns you. Both blocks sit in the same toolbox drawer, and dragging both into one program is the most natural thing in the world. On the Mini, pick one.

The other trap: “far away” can mean “heard nothing”

Section titled “The other trap: “far away” can mean “heard nothing””

When the distance sensor hears nothing back, it does not say “I don’t know”. It says 200 cm. So an empty room and a broken sensor look exactly the same. If your program says “drive forward while the distance is more than 30 cm”, a broken sensor makes the Mini drive forward forever.

distance_cm() keeps -1 for a reading that fails with an error — which is why measure() in this file checks for it. Hearing nothing is not an error, though: it is 200.

This is a reading copy of the program in the project: editing it changes nothing, so open the project and edit it there.

main.py
# main.py — Mini eyes (Python) (mini)
#
# A reading copy of one file inside 04-mini-eyes.firmware-3.mobot.
# Editing this copy changes nothing: open the project and edit it there.
# This is the entry point — the board runs it as main.py.
# Written for firmware 3.0; the 2.x version is 04-mini-eyes.main.py.
"""Mini eyes - MoBot Smart Car Mini: a parking sensor, and a pin fight.
The Mini can have TWO kinds of eyes, and it cannot have both at once.
robot.enable_distance_sensor() trigger on pin 27, echo on pin 26
robot.enable_line_sensor() two line eyes: left on pin 26, right on pin 27
Look at those numbers again. They are the SAME TWO PINS, swapped over. There
are only two wires and both sensors want them. Part 2 of this program switches
both on, on purpose, so you can watch the distance sensor stop working.
Neither one is switched on when the Mini boots, so nothing is wasted until you
ask - but you have to ask for the right one.
"""
import time
# ------------------------------------------------------------------ 1 -------
# A parking sensor. Green when you are far away, red when you are close,
# and the beeps get faster the nearer your hand gets.
print("Part 1: parking sensor. Move your hand towards the front of the Mini.")
robot.enable_distance_sensor()
if robot.distance_sensor is None:
print("The distance sensor did not start.")
raise SystemExit
light = robot.rgb_led
def measure():
"""Distance in centimetres, or None if the sensor could not tell.
Careful: when no echo comes back at all, distance_cm() does NOT say so.
It gives back 200, as if something were two metres away. It only gives
back -1 if the reading fails with an error, so a program should check
for that before it believes the number.
"""
cm = robot.distance_sensor.distance_cm()
if cm == -1:
return None
return cm
for _ in range(200): # about 20 seconds
cm = measure()
if cm is None:
print("nothing came back")
light.set_color(20, 20, 20)
time.sleep(0.2)
continue
print(round(cm, 1), "cm")
if cm > 40:
light.set_color(0, 200, 0) # green: plenty of room
gap = 0.6
elif cm > 20:
light.set_color(200, 160, 0) # amber: careful
gap = 0.3
elif cm > 8:
light.set_color(255, 60, 0) # orange: close
gap = 0.15
else:
light.set_color(255, 0, 0) # red: stop!
gap = 0.06
robot.buzzer.play_song("0 C5 1 0")
time.sleep(gap)
robot.buzzer.stop()
light.off()
# ------------------------------------------------------------------ 2 -------
# Now the pin fight. Wake the line sensor up and watch the distance sensor
# fall over, because pin 27 has just stopped being an output.
print("")
print("Part 2: waking the LINE sensor, which wants the same two pins.")
robot.enable_line_sensor()
print("Line eyes say (left, right):", robot.line_sensor.read())
print("And now the distance sensor, five more times:")
for _ in range(5):
cm = measure()
if cm is None:
print(" the reading failed")
else:
print(" ", round(cm, 1), "cm <- if this is stuck at 200, that is the")
print(" sensor giving up, not something two metres away")
time.sleep(0.5)
print("")
print("When this program ends, the Mini gives the pins back. Run it again and")
print("part 1 works, as long as only enable_distance_sensor() is called.")
robot.led1.on()
  • Turn it into a real reversing robot: drive backwards while the distance is more than 15 cm, then stop and beep.
  • Delete the enable_line_sensor() line in part 2 and confirm the distance sensor keeps working — that is the control experiment.
  • Use distance_mm() instead of distance_cm(): whole millimetres, no decimal point, and slightly quicker.
  • robot.distance_sensor.sees_object(15) is True when something is 15 cm away or closer — a shorter way to write part 1’s red test.