mBot activity: measure distances (ultrasonic sensor)

Beginner mBlock tutorial: read the mBot ultrasonic sensor in Live mode, show distance on the LED matrix with join blocks. Upload steps, checks, troubleshooting.

Some links on this page are affiliate links (Amazon.ca is the default shop for this /en/ guide; Amazon.ca is listed below for US readers). We may earn a commission on qualifying purchases, at no extra cost to you.

Direct answer (October 2026) — In mBlock 5 (Makeblock’s block software, similar to Scratch), read the mBot ultrasonic sensor into a variable in Live mode (real time with the PC, without uploading on every click), then show the same distance on the LED matrix by turning the number into text (join + " cm") and uploading the program for a cable-free robot.

This two-part activity guides you to:

  1. Read the distance from the Makeblock mBot ultrasonic sensor and see it on the computer (variable + Live mode);
  2. Display that distance on the LED matrix without the cable (program stored on the robot), handling number → text.

Exact block labels may vary with the mBlock version and interface language; the logical sequence stays the same.

Front view of the Makeblock mBot with ultrasonic sensor and LED matrix on the mCore board.
On the classic mBot the ultrasonic sensor is usually at the front; the LED matrix shows messages or values. Photo: Emmanuel Sauvage, Educational Robotics — October 2026.
Criterion Detail
Level Beginner (variables, sensor); optional C part: intermediate
Time About 45–75 minutes (Live + matrix + tests)
Hardware Charged mBot, PC, USB cable (Bluetooth optional)
Software mBlock 5, mBot blocks enabled
Success check Distance in cm on the PC, then about 42 cm scrolling on the matrix

Video — mBot activity: measuring distances (French voice-over): YouTube — step-by-step demo. Turn on auto subtitles (YouTube → “CC”) if you prefer reading steps during the demo.

Hardware and software

1. Prerequisites

2. The ultrasonic sensor: useful reminder

The ultrasonic module measures distance by emitting a sound wave and measuring the return time after reflection on an obstacle. On the classic mBot it is usually at the front; point the robot at what you want to “see”.

  • Usual range (manufacturer docs / blocks): often about 3 cm to 400 cm — too close or too far, the reading becomes unreliable or missing.
  • Uses: obstacle avoidance, wall following, intrusion detector, “stop” game at a given distance, etc.

The speed of sound is used in the board-side calculation; for the activity, the key point is that the block value is in centimetres (cm) (check the unit shown in mBlock).

3. Part one: distance on screen (Live mode)

Goal: a variable (e.g. data or distance) takes the sensor value in a loop; you see it in the sprite area on the PC.

  1. Create a variable and tick display on the stage if mBlock offers it.
  2. Connect the mBot (USB or Bluetooth — wireless link; see mBlock Bluetooth help if the link drops) and switch to Live mode — real-time mode where blocks talk to the robot without uploading everything on every click.
  3. In a forever loop, set the variable to the ultrasonic sensor reading (block like “distance / ultrasonic sensor” in mBot or Sensors).
  4. A short wait (a few tens of ms) avoids saturating the link and stabilises the display.

When you run the script (green flag or Live button depending on version), the variable updates: you read the distance in cm (as long as the obstacle stays in the valid range).

mBlock screenshot: loop script that reads the mBot ultrasonic sensor and assigns the value to a variable shown in the sprite area.
Example script: loop → read distance → set variable. mBlock 5 screenshot — Emmanuel Sauvage, Educational Robotics — October 2026.

You can also open the sample project distance_sans_affichage.mblock to compare with your script.

Check without the matrix: place a sheet about 40 cm in front of the sensor; the stage variable should show a value near 40. Say “forty-two centimetres” aloud when it shows 42 — that is the same string you will scroll on the matrix.

4. Part two: distance on the LED matrix (standalone)

Goal: the robot alone shows the distance on the LED matrix (often 5×8), ideally with scrolling if the number is too long — as in scroll text on the LED matrix.

Photo of the mBot showing a word on the LED matrix.
Same LED matrix as for scrolling text: you will display a string like “42 cm”. Photo: Emmanuel Sauvage, Educational Robotics — October 2026.

4.1 The problem: number vs text

The text scroll block usually expects a string (text), for example "42 cm".
The sensor returns a number (integer or decimal). These are not the same “type”: in Scratch / mBlock you must convert or join text.

Two common approaches:

Stay in the block palette — no Arduino (text code) tab required.

  1. Read the ultrasonic value into a variable (e.g. distance).
  2. Build a string with join / combine: join(distance, " cm") (exact block name depends on the UI language).
  3. Pass that string to the same show / scroll text pattern used in scroll text.
  4. Switch to Upload mode so the robot keeps scrolling when the USB cable is removed; use when mBot starts as the start event if mBlock offers it.

Success criterion: standing about 42 cm from a wall, the matrix scrolls something like 42 cm (slight sensor noise is normal).

  • Advantage: everything stays in blocks for learners.
  • Drawback: two procedures if you mix free text and distance — name them (scroll_distance, scroll_message).

Save the project as distance_matrix.mblock before trying Approach B.

4.3 Approach B — Via Arduino / C code (optional, secondary+)

Use this only after Approach A works.

mBlock can generate code from blocks, then let you edit the Arduino C part (button or Code tab / </> symbol depending on version).

Typical steps:

  1. Build the program in blocks (loop, sensor read, call to scroll).
  2. Open the code view (</> icon or equivalent menu), select all and copy.
  3. Switch to the Arduino tab, paste then adapt the scroll function call to concatenate number and unit into a string.
// scroll_text_… (may appear as defilement_texte_… if mBlock UI was French)
scroll_text_N_N(String(distance) + String(" cm"), 1);

Here, distance holds the centimetre reading; you form a string like "37 cm" before the call.

Safety: editing C code means re-uploading to the board and checking cable / batteries. Make a copy of the .mblock project before changing generated code.

5. Extension ideas

6. Checks and troubleshooting

Issue Common cause What to do
Variable frozen or missing Live mode off or mBot disconnected Reconnect USB, select mBot, restart Live
Reading 0 or huge Obstacle out of range or angled surface Flat surface at 10–200 cm; avoid absorbent fabric
Unstable Bluetooth Range or low battery Prefer USB in class; see mBlock Bluetooth help
Blank matrix after unplugging Program not uploaded Use Upload again; when mBot starts event if offered
Sensor blocks missing mBot library off Install mBot blocks
Type error on scroll Number instead of string Join block + " cm" before matrix block
Value jumps by several cm Angled obstacle or hand in front of sensor Flat surface; keep hands out of the beam

FAQ

How do I read the ultrasonic distance in mBlock?

Variable + loop + ultrasonic block in Live mode with the variable shown on the stage.

Why join " cm" before the LED matrix?

Scrolling expects text; the sensor sends a number.

Live or Upload?

Live to test with the PC; Upload for a cable-free demo.

7. Going further

Continue with the intrusion detector (same sensor family), blink the LEDs, your first mBot program, and mBlock guides in the “Also read” column.

For class: our 10 free mBot classroom projects offer a full 10-session progression with pupil worksheets and PDF answer keys.

Hardware: Amazon.ca — check today’s price before you order.

Amazon partner — commission possible on qualifying purchases, at no extra cost to you.

Emmanuel Sauvage — updated 31 October 2026.

Further reading

Was this page helpful?

Have a question about this guide or need advice? Ask us here

Torn between two models or stuck on a build? Drop us a line and we will get back to you by email.