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.
⚠ A fresh Mini ignores your program
Section titled “⚠ A fresh Mini ignores your program”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.
What you need
Section titled “What you need”- 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.
How to run it
Section titled “How to run it”Open 04-mini-eyes.firmware-3.mobot, press Save, and move your hand slowly towards the front of it.
What you should see and hear
Section titled “What you should see and hear”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 trap this example exists to show
Section titled “The trap this example exists to show”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.
The program
Section titled “The program”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 — 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. Thereare only two wires and both sensors want them. Part 2 of this program switchesboth 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 youask - 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()Try changing it
Section titled “Try changing it”- 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 ofdistance_cm(): whole millimetres, no decimal point, and slightly quicker. robot.distance_sensor.sees_object(15)isTruewhen something is 15 cm away or closer — a shorter way to write part 1’s red test.
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.
Project for firmware 2.x: 04-mini-eyes.mobot. Open it in MoBot.
⚠ A fresh Mini ignores your program
Section titled “⚠ A fresh Mini ignores your program”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.
What you need
Section titled “What you need”- 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.
How to run it
Section titled “How to run it”Open 04-mini-eyes.mobot, press Save, and move your hand slowly towards the front of it.
What you should see and hear
Section titled “What you should see and hear”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 sensors, 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.
Reset the Mini and part 1 works again.
The trap this example exists to show
Section titled “The trap this example exists to show”The Mini has two pairs of eyes and only one pair of wires:
| ask for | takes pin 26 | takes pin 27 |
|---|---|---|
initUltrasonic() |
echo (listens) | trigger (shouts) |
initIRs() |
left line sensor | right line sensor |
initIRs() 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 — Init Ultrasonic and Init IRs — 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: the Mini reports “far away” wrongly
Section titled “The other trap: the Mini reports “far away” wrongly”When the Mini’s 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.
Also: if a reading fails with an error, the Mini’s sensor raises it
(OSError), while the big Smart Car’s returns -1 instead. Same sensor, two
different manners — which is why measure() in this file has a try around it.
Hearing nothing is not an error, though: it is 200.
The program
Section titled “The program”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 — Mini eyes (Python) (mini)## A reading copy of one file inside 04-mini-eyes.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.
"""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.
initUltrasonic() the distance sensor: trigger on pin 27, echo on pin 26 initIRs() two line sensors: left on pin 26, right on pin 27
Look at those numbers again. They are the SAME TWO PINS, swapped over. Thereare only two wires and both sensors want them. Part 2 of this program callsboth 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 youask - 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.")mobotMINI.initUltrasonic()
if mobotMINI.ultrasonic is None: print("The distance sensor did not start.") raise SystemExit
def measure(): """Distance in centimetres, or None if the sensor could not tell.
Careful: when no echo comes back at all, the Mini's distance sensor does NOT say so. It gives back 200, as if something were two metres away. If the reading itself fails, it RAISES an error - it does not quietly hand you -1 the way the big Smart Car's does. So a Mini program has to be ready to catch that. """ try: return mobotMINI.ultrasonic.distance_cm() except OSError: return None
for _ in range(200): # about 20 seconds cm = measure()
if cm is None: print("nothing came back") mobotMINI.rgb.set_color(20, 20, 20) time.sleep(0.2) continue
print(round(cm, 1), "cm")
if cm > 40: mobotMINI.rgb.set_color(0, 200, 0) # green: plenty of room gap = 0.6 elif cm > 20: mobotMINI.rgb.set_color(200, 160, 0) # amber: careful gap = 0.3 elif cm > 8: mobotMINI.rgb.set_color(255, 60, 0) # orange: close gap = 0.15 else: mobotMINI.rgb.set_color(255, 0, 0) # red: stop! gap = 0.06
mobotMINI.buzzer.play("0 C5 1 0", looping=False) time.sleep(gap)
mobotMINI.buzzer.stop()mobotMINI.rgb.off()
# ------------------------------------------------------------------ 2 -------# Now the pin fight. Wake the line sensors up and watch the distance# sensor fall over, because pin 27 has just stopped being an output.
print("")print("Part 2: waking the LINE sensors, which want the same two pins.")mobotMINI.initIRs()
print("Line sensors say:", mobotMINI.left_ir.value(), mobotMINI.right_ir.value())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("Reset the Mini and call ONLY initUltrasonic() to get it back.")mobotMINI.led1.on()Try changing it
Section titled “Try changing it”- Turn it into a real reversing robot: drive backwards while the distance is more than 15 cm, then stop and beep.
- Delete the
initIRs()line in part 2 and confirm the distance sensor keeps working — that is the control experiment. - Use
distance_mm()instead ofdistance_cm(): whole millimetres, no decimal point, and slightly quicker.