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.
Getting started
Home Assistant
Buying
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.com is the default shop for this /en/ guide; Amazon.com 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:
Exact block labels may vary with the mBlock version and interface language; the logical sequence stays the same.
| 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.
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”.
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).
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.
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).
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.
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.
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.
distance).join(distance, " cm") (exact block name depends on the UI language).Success criterion: standing about 42 cm from a wall, the matrix scrolls something like 42 cm (slight sensor noise is normal).
scroll_distance, scroll_message).Save the project as distance_matrix.mblock before trying Approach B.
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:
</> icon or equivalent menu), select all and copy.// 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.
| 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 |
Variable + loop + ultrasonic block in Live mode with the variable shown on the stage.
" cm" before the LED matrix?Scrolling expects text; the sensor sends a number.
Live to test with the PC; Upload for a cable-free demo.
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.com — 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.
Was this page helpful?
Thank you!
What should we improve?
Torn between two models or stuck on a build? Drop us a line and we will get back to you by email.
Cookies and analytics
We use Google Analytics 4 to understand how the site is used (page views, navigation). It is only switched on if you accept. Declining does not stop you using the site. More: cookie settings and privacy policy (in French).