Stop before you bump — Mini
The Mini’s ultrasonic sensor is a pair of little silver cylinders that look like eyes. They are really a mouth and an ear: the Mini shouts a sound too high for you to hear, listens for the echo, and works out how far away things are.
This program drives forward, and stops all by itself when something gets closer than 15 cm. Put your hand in front of it and it stops. Take your hand away and it goes again.
This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Mini.
The program
Section titled “The program”Read the program as text
- MoBot Mini Start
- Print to serial
"Obstacle test — wave your hand in front of the sensor." - Initialize ultrasonic sensor
- wait 1 seconds
- repeat forever
- Print to serial (create text with
"cm: "Read ultrasonic distance in cm" mm: "Read ultrasonic distance in mm) - if (Read ultrasonic distance in cm < 15)
- set Motor 1 to Direction 1 with speed 0 %
- set Motor 2 to Direction 1 with speed 0 %
- Set LED 1 ON
- else
- set Motor 1 to Direction 1 with speed 45 %
- set Motor 2 to Direction 1 with speed 45 %
- Set LED 1 OFF
- wait 0.2 seconds
- Print to serial (create text with
What you need
Section titled “What you need”- A MoBot Mini, charged and switched on.
- A USB cable.
- The ultrasonic sensor plugged in — trigger to GPIO 27, echo to GPIO 26.
- A book to stand the Mini on the first time, so it cannot drive away while you are reading the numbers. Once you trust it, put it on the floor and walk in front of it.
⛔ Do not plug the two IR sensors in at the same time. On the Mini the ultrasonic sensor and the IR sensors use the same two pins (26 and 27). Only one of them can be connected at a time. Use this example or 06-ir-line-follow, never both at once.
⚠️ First time? Read the “Before you send it” section of Blink and fade.
How to run it
Section titled “How to run it”- Plug the ultrasonic sensor in and stand the Mini on a book.
- Open 05-ultrasonic-stop.mobot in MoBot.
- Press Send to Device.
- Open the serial monitor, then move your hand towards the sensor.
What you should see
Section titled “What you should see”[MOBOT] Obstacle test — wave your hand in front of the sensor.once.Ultrasonic initialized— printed by the robot itself, not by this program. If you seeUltrasonic init errorinstead, the sensor is not plugged in properly and nothing below will work.- Then a new line about five times a second:
[MOBOT] cm: 23.45 mm: 234 - Move your hand closer and the numbers get smaller. Move it away and they get bigger. With nothing in front of the Mini at all, expect a steady 200 cm: that is what the Mini says when no echo comes back.
- Closer than 15 cm: the wheels stop and LED 1 comes on.
- Further than 15 cm: the wheels turn and LED 1 goes out.
The light is the quickest test of all — you can see it from across the room without reading a single number.
The blocks
Section titled “The blocks”| block | what it does |
|---|---|
mobot_init_ultrasonic |
Wakes the sensor up. Nothing works without it — it must come before the loop. |
mobot_ultrasonic_distance |
How far away, in cm or in mm. This example prints both so you can check one against the other. |
controls_if / logic_compare |
“If closer than 15, then… otherwise…” |
mobot_motordirspeed |
Drives at 45 %, or stops with speed 0. |
mobot_ledon |
The warning light. |
Check the two numbers agree: mm should be about ten times cm. If they
do not match, the sensor is being read twice too quickly.
The Python code
Section titled “The Python code”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.
# Stop before you bump — mini (firmware 3.0)## This is what MoBot puts on a robot running firmware 3.0 as main.py when you open# 05-ultrasonic-stop.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 05-ultrasonic-stop.main.py.
from machine import Pinimport time
print("[MOBOT]", 'Obstacle test — wave your hand in front of the sensor.')robot.enable_distance_sensor()time.sleep(1)while True: print("[MOBOT]", (''.join([str(x) for x in ['cm: ', robot.distance_sensor.distance_cm(), ' mm: ', robot.distance_sensor.distance_mm()]]))) if robot.distance_sensor.distance_cm() < 15: robot.left_motor.forward(0) robot.right_motor.forward(0) robot.led1.set(1) else: robot.left_motor.forward(45) robot.right_motor.forward(45) robot.led1.set(0) time.sleep(0.2)# Stop before you bump — mini## This is what MoBot puts on the board as main.py when you open# 05-ultrasonic-stop.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.
from machine import Pinimport time
print("[MOBOT]", 'Obstacle test — wave your hand in front of the sensor.')mobotMINI.initUltrasonic()time.sleep(1)while True: print("[MOBOT]", (''.join([str(x) for x in ['cm: ', mobotMINI.ultrasonic.distance_cm(), ' mm: ', mobotMINI.ultrasonic.distance_mm()]]))) if mobotMINI.ultrasonic.distance_cm() < 15: mobotMINI.left_motor.set_direction_and_speed(0, 0) mobotMINI.right_motor.set_direction_and_speed(0, 0) mobotMINI.led1.setState(1) else: mobotMINI.left_motor.set_direction_and_speed(0, 45) mobotMINI.right_motor.set_direction_and_speed(0, 45) mobotMINI.led1.setState(0) time.sleep(0.2)Try changing it
Section titled “Try changing it”- Change
15to40— the Mini becomes very shy and stops much earlier. - Change it to
5— very brave. Do this one on the floor with a soft wall. - Swap the wheels to speed
0and45when it is too close: instead of stopping, the Mini turns away from the wall.