Skip to content

Search the documentation

Kit
Firmware

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 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 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
  • 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.

  1. Plug the ultrasonic sensor in and stand the Mini on a book.
  2. Open 05-ultrasonic-stop.mobot in MoBot.
  3. Press Send to Device.
  4. Open the serial monitor, then move your hand towards the sensor.
  • [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 see Ultrasonic init error instead, 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.

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 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.

05-ultrasonic-stop.firmware-3.main.py
# 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 Pin
import 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)
  • Change 15 to 40 — 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 0 and 45 when it is too close: instead of stopping, the Mini turns away from the wall.