Ranger 2-Way (TOF2) × mBlock × mBlock 5
Interactive tutorialmBlock
First time? Try the interactive tutorial in your browser first, then follow the steps below with your hub.
This page covers mBlock 5 (MBC Pro Sensors extension) only: how to set it up for the Ranger 2-Way (TOF2) on Makeblock (mBot) + mBlock 5, how to read every value, the commands it supports and examples. Use the switcher above to change product, host or software.
Which edition and how to connect
Get the MakeBlock edition (RJ25 plug) and plug it into an RJ25 port of mBot / Ranger / Orion / MegaPi; usable ports per board are listed in MakeBlock extension.
Setup
What you need
- Sensor: Ranger 2-Way (TOF2) (MakeBlock edition, different factory firmware from the other editions), plus an mBot adapter (to plug into an RJ25 port). SPIKE, EV3 and I2C editions cannot connect to a Makeblock board directly.
- Board: mBot (mCore), mBot Ranger (Auriga), Orion, Ultimate 2.0 (MegaPi), MegaPi Pro. mBot2 (CyberPi) has no RJ25 port and is not supported.
- Software: mBlock 5 for PC (Upload mode: the program is flashed to the board).
- Extension: MBC Pro Sensors v2.0.0 (for protocol v2 firmware shipped from 2026-10).
Install the software
- Install mBlock 5 for PC from the mBlock website.
- Open mBlock 5 and add your board (for example mBot) under "Devices" at the bottom left.
- Switch the mode toggle at the bottom right to Upload.
Make the software see the sensor
Add one extension and the MBC blocks appear:
- Get the v2.0.0 extension: once it is listed, search "MBC" in the mBlock 5 "Extension Center" and add it. Before it is listed (under review), advanced users can sign in to ext.mblock.cc with their own Makeblock account → My extensions → Import data, upload mbc_pro_sensors_2.0.0_import.mext (about 1 minute), then click "Download" to get a
.mext. - Drag the
.mextfile into the mBlock window. Six new categories appear: Pathfinder, Ranger, Controller, IMU gyro, Vision and MBC common. - Start the program with the board's start block (for example "when mBot starts up") and click "Upload" to flash it to the board.
- To open an example: "File → Open from your computer" and pick the
.mblockfile.
Important
The new extension (v2) only works with new firmware, and the legacy extension (v1.0.0) only with firmware from before 2026-10; their blocks are not compatible. Not sure which firmware you have? Drag in the v2 extension and read the "MBC common → protocol version" block: 2 means new firmware. The legacy extension is in the Downloads.
Wiring
Sensor → mBot adapter → the board's RJ25 port (UART 38400). The blocks ask for a port number; the extension maps it to the right pins for your board.
| Board | Usable ports | Notes |
|---|---|---|
| mBot (mCore) | 1, 2, 3, 4 | all four ports |
| mBot Ranger (Auriga) | 6 ~ 10 | 1 ~ 4 are red motor ports, 5 is the serial port |
| Orion | 3, 4, 6 | 1, 2 are red motor ports; 7, 8 are analog only |
| Ultimate 2.0 (MegaPi) | 6, 7, 8 | needs the RJ25 shield; 5 is the serial port |
| MegaPi Pro | 7, 8, 9, 10 | needs the RJ25 shield |
The examples on this page use mBot port 1; to use another port, change the port number on the blocks. On another board, replace "when mBot starts up" with that board's start block.
Which side is left
Left and right are as seen from behind the sensor, looking the way it faces (left = J5, right = J3). Facing the sensor, they are the other way round, so when you test with your hand, stand behind the sensor and look the same way it does. "Left is closer" and "turn left" in the examples follow this direction.
First test
- Start the program with "wait 1 second" so the sensor can boot; reading too early returns
-32768. - Check the wiring with the "MBC common → connection OK?" block, then look at the values.
- Upload example 1 on this page. If the number changes with the sensor, everything is connected.
FAQ
- "connection OK?" is false? Wrong port, the sensor is not the MakeBlock edition, or the firmware is legacy (use the v1 extension with old firmware).
- Reading
-32768? Nothing has been read since power-on: check the wiring; or this product has no such value. - The value never changes? Make sure the adapter is firmly in and the sensor light is on; "protocol version" reading 2 means communication works.
- Still stuck? Contact us on LINE with the board model, the port and the value of the "device ID" block.
Reading values
After adding the MBC Pro Sensors extension, find the blocks in the "Ranger 2" category and pick the port the sensor is plugged into.
left distance (cm) leftCm
| Range | Meaning |
|---|---|
0~120 cm |
distance from the left sensor, 0~120 cm (left = sensor 0 seen from behind the robot); 120 = nothing in range / sensor failed |
(Ranger 2 port [1 v] left distance cm :: sensing)
Block returns left distance (cm)(0~120 cm)。Host sends [01] → reply [lo][hi][01 ^ lo ^ hi], value = int16 little-endian.
right distance (cm) rightCm
| Range | Meaning |
|---|---|
0~120 cm |
distance from the right sensor, 0~120 cm (right = sensor 1); 120 = nothing in range / sensor failed |
(Ranger 2 port [1 v] right distance cm :: sensing)
Block returns right distance (cm)(0~120 cm)。Host sends [02] → reply [lo][hi][02 ^ lo ^ hi], value = int16 little-endian.
nearer distance (cm) nearCm
| Range | Meaning |
|---|---|
0~120 cm |
the smaller of left and right, 0~120 cm |
(Ranger 2 port [1 v] nearer distance cm :: sensing)
Block returns nearer distance (cm)(0~120 cm)。Host sends [03] → reply [lo][hi][03 ^ lo ^ hi], value = int16 little-endian.
left distance (mm) leftMm
| Range | Meaning |
|---|---|
0~1200 mm |
left sensor distance 0~1200 mm; 1200 = nothing in range / sensor failed |
(Ranger 2 port [1 v] left distance mm :: sensing)
Block returns left distance (mm)(0~1200 mm)。Host sends [04] → reply [lo][hi][04 ^ lo ^ hi], value = int16 little-endian.
right distance (mm) rightMm
| Range | Meaning |
|---|---|
0~1200 mm |
right sensor distance 0~1200 mm; 1200 = nothing in range / sensor failed |
(Ranger 2 port [1 v] right distance mm :: sensing)
Block returns right distance (mm)(0~1200 mm)。Host sends [05] → reply [lo][hi][05 ^ lo ^ hi], value = int16 little-endian.
init bitmap initOk
| Range | Meaning |
|---|---|
0~3 bit |
bit0 = left, bit1 = right; 1 = that sensor initialised at power-on (no retry); 3 = both OK |
(Ranger 2 port [1 v] init bitmap :: sensing)
Block returns init bitmap(0~3 bit)。Host sends [06] → reply [lo][hi][06 ^ lo ^ hi], value = int16 little-endian.
fault bitmap fault
| Range | Meaning |
|---|---|
0~3 bit |
bit0 = left, bit1 = right; 1 = that sensor is currently failing (I2C error or no update for 200 ms); its distance reads 120 / 1200 |
(Ranger 2 port [1 v] fault bitmap :: sensing)
Block returns fault bitmap(0~3 bit)。Host sends [07] → reply [lo][hi][07 ^ lo ^ hi], value = int16 little-endian.
average distance (mm) avgMm
| Range | Meaning |
|---|---|
0~1200 mm |
(left mm + right mm) / 2, 0~1200; same number as EV3 mode 3 and the 3rd value of SPIKE mode 6 |
(Ranger 2 port [1 v] average distance mm :: sensing)
Block returns average distance (mm)(0~1200 mm)。Host sends [08] → reply [lo][hi][08 ^ lo ^ hi], value = int16 little-endian.
Commands
update all values readAll
reads the whole value table into a cache (FD pack-all); reporter blocks within the next 10 ms use the cache instead of asking one by one
Ranger 2 port [1 v] update all values :: sensing
Examples
This page is for people who have never written a program: read the Ranger 2-Way on an mBot with mBlock 5 blocks. The 5 examples start with "turn the number into a sound" and work up to a robot that drives itself. Every example has a block picture, a downloadable project file and an Arduino sketch that does the same thing.
| Software | Who it suits | How to open the example file |
|---|---|---|
| 🟧 mBlock 5 desktop (blocks, Upload mode) + MBC Pro Sensors extension v2 | Schools, first time with an mBot | Download the .mblock → open mBlock 5 → "File → Open from your computer" → switch to "Upload" at the bottom right → "Upload" |
| 🔧 Arduino IDE (text code) | People who want to see what is behind the blocks, or write their own code | Download the .ino → install the Makeblock and MBC_Sensors libraries (see Makeblock extension) → board Arduino Uno → upload |
Important
- The sensor must be the Makeblock edition with the 2026-10 protocol v2 firmware, connected through the mBot socket board to an RJ25 port; use extension v2.0.0 (old firmware goes with the old v1 extension; they cannot be mixed).
- The board is the mBot (mCore). mBot Ranger, Orion and MegaPi work too: swap "when mBot starts up" for that board's hat block and use a port that board allows (see supported boards and ports); the mBot2 (CyberPi) has no RJ25 port and is not supported.
- mBlock 5 is shown here with its English block text; your copy may show the same blocks in another language.
Before you start
Sensor → mBot socket board → RJ25 port on the board (S1 = host TX, S2 = host RX, UART 38400).
- Install the extension: get the MBC Pro Sensors v2.0.0
.mextfrom Makeblock extension and drop it into the mBlock 5 window; add mBot under "Devices" (bottom left) and switch to "Upload" (bottom right). - Wiring: Ranger → mBot socket board → mBot port 3 (every example on this page uses this port; to use another, change the port number on the blocks).
- Let it boot first: every example starts with "wait 1 seconds" so the sensor can boot; reading too early gives -32768 (nothing received yet).
- The Ranger 2-way needs no calibration. Each laser looks straight ahead; seen from behind the robot, the left one is "left distance"; anything beyond 120 cm reads 120.
Blocks used on this page
Each reporter block is one "value id" of the sensor's value table (the same ids as the Arduino I2C registers); the full block list is in Makeblock extension.
| Block | Returns | Meaning |
|---|---|---|
| Ranger 2 port [3] nearer distance cm | 0~120 cm | the smaller of left and right, 0~120 cm |
| Ranger 2 port [3] left distance cm | 0~120 cm | distance from the left sensor, 0~120 cm (left = sensor 0 seen from behind the robot); 120 = nothing in range / sensor failed |
| Ranger 2 port [3] right distance cm | 0~120 cm | distance from the right sensor, 0~120 cm (right = sensor 1); 120 = nothing in range / sensor failed |
Examples on this page
- Distance theremin
- Traffic-light distance warning
- Distance drives one wheel
- Drive and stop at the wall
- Turn away from the nearer side
Note
The
.mblockfiles are generated in the mBlock 5 save format and have not yet been opened in mBlock 5 for verification (pending the extension v2 release). If one will not open or shows blank blocks, check that extension v2 is loaded and the device is mBot, or rebuild it from the block picture below (under 5 minutes); you can also tell us on LINE.
Example 1: Distance theremin
What this example does: The mBot has no screen, so this example turns the "nearer distance" (0 to 120 cm) into a pitch: the closer your hand, the lower the note; the further, the higher. Learn to "read the number" first.
What you need
- mBot (mCore board) + mBlock 5 desktop
- Ranger 2-Way (Makeblock edition) + mBot socket board
Wiring: Ranger → mBot socket board → mBot port 3. Mount it on the front with both lasers facing forward.
Download
| mBlock 5 project | Equivalent Arduino sketch |
|---|---|
| ex01_theremin.mblock | ex01_theremin.ino |
mBlock blocks (just rebuild them like this)
when mBot starts up :: events hat wait (1) seconds forever play tone ((200) + ((Ranger 2 port [3 v] nearer distance cm :: #0A9E8F) * (10))) Hz for (0.1) seconds :: sound end
🔧 Equivalent Arduino sketch (mBot)
// Ranger 2-Way × mBlock 5 Example 1: Distance theremin (equivalent Arduino sketch for mBot)
// Wiring: Ranger → mBot socket board → mBot port 3. Mount it on the front with both lasers facing forward.
// Does exactly what the mBlock block example does, for people who want to see "what is behind the blocks" or use the Arduino IDE.
// Libraries needed:
// 1. Makeblock (Arduino IDE Library Manager, search "Makeblock", or the copy inside the mBlock 5 install folder)
// 2. MBC_Sensors v2: the four files in src/ of the extension .mext (MBC_Sensors.h/.cpp, mbc_proto.h, mbc_ids.h),
// put them in Documents/Arduino/libraries/MBC_Sensors/ and #include <MBC_Sensors.h> (https://mbc-pro-sensors.github.io/en/makeblock.html)
// Board: Arduino Uno (the mBot mCore is an Uno). After uploading, unplug USB and run on battery.
#include <MeMCore.h> // mBot (mCore): motors, LED, buzzer
#include <SoftwareSerial.h>
#include <MBC_Sensors.h> // MBC sensors v2 (the same library the mBlock extension uses)
MBCPort mbc_port3(3); // sensor on mBot port 3
MeDCMotor motorL(9); // left wheel (M1)
MeDCMotor motorR(10); // right wheel (M2)
MeRGBLed rgbled(7, 2); // on-board RGB LED (2 pixels)
MeBuzzer buzzer; // on-board buzzer
// left / right wheel power -100~100 (positive = forward): the mBot left wheel is mounted mirrored, so M1 gets the negative value; % becomes PWM 0~255
void wheels(int l, int r) {
l = constrain(l, -100, 100); r = constrain(r, -100, 100);
motorL.run(-l * 255 / 100);
motorR.run(r * 255 / 100);
}
void setup() { // everything runs inside setup(), just like the program mBlock generates
mbc_port3.begin();
delay(1000); // Wait 1 s for the sensor to boot
while (1) { // Repeat forever
buzzer.tone((200 + (mbcRead(mbc_port3, 0x03) * 10)), 100); // Pitch = 200 + nearer distance × 10 Hz, for 0.1 s
}
}
void loop() {} // nothing here: the blocks above never returnStep by step
- Wait 1 s for the sensor to boot
- Repeat forever
- Pitch = 200 + nearer distance × 10 Hz, for 0.1 s
What you should see: Hand 10 cm in front: a low 300 Hz; move it away and the pitch rises; nothing in front (120) is a high 1400 Hz.
FAQ
- No sound? → The buzzer is on the board and is not loud; listen closely, or change 0.1 s to 0.3 s.
- Nothing ahead but the value is not 120? → The laser sees the floor or table: mount it higher or tilt it slightly up. Anything beyond 120 cm reads 120.
- Nothing happens, or the value stays at -32768? → Wrong port (every example on this page uses port 3), socket board not seated, or the sensor is not the Makeblock edition. Check with the "MBC common → connected?" block first; "protocol version" must read 2 for the new firmware (old firmware needs extension v1).
- Upload fails, or the blocks are blank? → Make sure the switch at the bottom right is in "Upload" mode, extension v2 has been dropped into mBlock, and the USB cable goes to the mBot board.
Example 2: Traffic-light distance warning
What this example does: Use "if… then… else…" to split the distance into three bands: within 15 cm red, within 40 cm yellow, further green. This is the decision skeleton for "stop at the wall" later.
What you need
- mBot (mCore board) + mBlock 5 desktop
- Ranger 2-Way (Makeblock edition) + mBot socket board
Wiring: Ranger → mBot socket board → mBot port 3. Mount it on the front with both lasers facing forward.
Download
| mBlock 5 project | Equivalent Arduino sketch |
|---|---|
| ex02_led_warning.mblock | ex02_led_warning.ino |
mBlock blocks (just rebuild them like this)
when mBot starts up :: events hat
wait (1) seconds
forever
if <(Ranger 2 port [3 v] nearer distance cm :: #0A9E8F) < (15)> then
LED [all v] colour red (255) green (0) blue (0) :: looks
else
if <(Ranger 2 port [3 v] nearer distance cm :: #0A9E8F) < (40)> then
LED [all v] colour red (255) green (255) blue (0) :: looks
else
LED [all v] colour red (0) green (255) blue (0) :: looks
end
end
end
🔧 Equivalent Arduino sketch (mBot)
// Ranger 2-Way × mBlock 5 Example 2: Traffic-light distance warning (equivalent Arduino sketch for mBot)
// Wiring: Ranger → mBot socket board → mBot port 3. Mount it on the front with both lasers facing forward.
// Does exactly what the mBlock block example does, for people who want to see "what is behind the blocks" or use the Arduino IDE.
// Libraries needed:
// 1. Makeblock (Arduino IDE Library Manager, search "Makeblock", or the copy inside the mBlock 5 install folder)
// 2. MBC_Sensors v2: the four files in src/ of the extension .mext (MBC_Sensors.h/.cpp, mbc_proto.h, mbc_ids.h),
// put them in Documents/Arduino/libraries/MBC_Sensors/ and #include <MBC_Sensors.h> (https://mbc-pro-sensors.github.io/en/makeblock.html)
// Board: Arduino Uno (the mBot mCore is an Uno). After uploading, unplug USB and run on battery.
#include <MeMCore.h> // mBot (mCore): motors, LED, buzzer
#include <SoftwareSerial.h>
#include <MBC_Sensors.h> // MBC sensors v2 (the same library the mBlock extension uses)
MBCPort mbc_port3(3); // sensor on mBot port 3
MeDCMotor motorL(9); // left wheel (M1)
MeDCMotor motorR(10); // right wheel (M2)
MeRGBLed rgbled(7, 2); // on-board RGB LED (2 pixels)
MeBuzzer buzzer; // on-board buzzer
// left / right wheel power -100~100 (positive = forward): the mBot left wheel is mounted mirrored, so M1 gets the negative value; % becomes PWM 0~255
void wheels(int l, int r) {
l = constrain(l, -100, 100); r = constrain(r, -100, 100);
motorL.run(-l * 255 / 100);
motorR.run(r * 255 / 100);
}
void setup() { // everything runs inside setup(), just like the program mBlock generates
mbc_port3.begin();
delay(1000); // Wait 1 s for the sensor to boot
while (1) { // Repeat forever
if ((mbcRead(mbc_port3, 0x03) < 15)) { // If the nearer distance is less than 15
rgbled.setColor(0, 255, 0, 0); rgbled.show(); // Within 15 cm: red
} else {
if ((mbcRead(mbc_port3, 0x03) < 40)) { // Otherwise, if the nearer distance is less than 40
rgbled.setColor(0, 255, 255, 0); rgbled.show(); // Within 40 cm: yellow
} else {
rgbled.setColor(0, 0, 255, 0); rgbled.show(); // Further: green
}
}
}
}
void loop() {} // nothing here: the blocks above never returnStep by step
- Wait 1 s for the sensor to boot
- Repeat forever
- If the nearer distance is less than 15
- Within 15 cm: red
- otherwise:
- Otherwise, if the nearer distance is less than 40
- Within 40 cm: yellow
- otherwise:
- Further: green
- Otherwise, if the nearer distance is less than 40
- If the nearer distance is less than 15
What you should see: Bring your hand slowly closer: green → yellow (40 cm) → red (15 cm).
FAQ
- Nothing ahead but the value is not 120? → The laser sees the floor or table: mount it higher or tilt it slightly up. Anything beyond 120 cm reads 120.
- Nothing happens, or the value stays at -32768? → Wrong port (every example on this page uses port 3), socket board not seated, or the sensor is not the Makeblock edition. Check with the "MBC common → connected?" block first; "protocol version" must read 2 for the new firmware (old firmware needs extension v1).
- Upload fails, or the blocks are blank? → Make sure the switch at the bottom right is in "Upload" mode, extension v2 has been dropped into mBlock, and the USB cable goes to the mBot board.
Example 3: Distance drives one wheel
What this example does: Use "nearer distance × 0.8" directly as the left wheel power: the more open the front, the faster the wheel; the closer an object, the slower; right in front it stops. Prop the robot up to watch one wheel react, then drive it in the next example.
What you need
- mBot (mCore board) + mBlock 5 desktop
- mBot two-wheel robot (left wheel on M1, right wheel on M2, as shipped)
- Ranger 2-Way (Makeblock edition) + mBot socket board
- A wall or a box as an obstacle
Wiring: Ranger → mBot socket board → mBot port 3. Mount it on the front with both lasers facing forward.
Download
| mBlock 5 project | Equivalent Arduino sketch |
|---|---|
| ex03_one_wheel.mblock | ex03_one_wheel.ino |
mBlock blocks (just rebuild them like this)
when mBot starts up :: events hat wait (1) seconds forever left wheel power ((Ranger 2 port [3 v] nearer distance cm :: #0A9E8F) * (0.8)) %, right wheel power (0) % :: motion end
🔧 Equivalent Arduino sketch (mBot)
// Ranger 2-Way × mBlock 5 Example 3: Distance drives one wheel (equivalent Arduino sketch for mBot)
// Wiring: Ranger → mBot socket board → mBot port 3. Mount it on the front with both lasers facing forward.
// Does exactly what the mBlock block example does, for people who want to see "what is behind the blocks" or use the Arduino IDE.
// Libraries needed:
// 1. Makeblock (Arduino IDE Library Manager, search "Makeblock", or the copy inside the mBlock 5 install folder)
// 2. MBC_Sensors v2: the four files in src/ of the extension .mext (MBC_Sensors.h/.cpp, mbc_proto.h, mbc_ids.h),
// put them in Documents/Arduino/libraries/MBC_Sensors/ and #include <MBC_Sensors.h> (https://mbc-pro-sensors.github.io/en/makeblock.html)
// Board: Arduino Uno (the mBot mCore is an Uno). After uploading, unplug USB and run on battery.
#include <MeMCore.h> // mBot (mCore): motors, LED, buzzer
#include <SoftwareSerial.h>
#include <MBC_Sensors.h> // MBC sensors v2 (the same library the mBlock extension uses)
MBCPort mbc_port3(3); // sensor on mBot port 3
MeDCMotor motorL(9); // left wheel (M1)
MeDCMotor motorR(10); // right wheel (M2)
MeRGBLed rgbled(7, 2); // on-board RGB LED (2 pixels)
MeBuzzer buzzer; // on-board buzzer
// left / right wheel power -100~100 (positive = forward): the mBot left wheel is mounted mirrored, so M1 gets the negative value; % becomes PWM 0~255
void wheels(int l, int r) {
l = constrain(l, -100, 100); r = constrain(r, -100, 100);
motorL.run(-l * 255 / 100);
motorR.run(r * 255 / 100);
}
void setup() { // everything runs inside setup(), just like the program mBlock generates
mbc_port3.begin();
delay(1000); // Wait 1 s for the sensor to boot
while (1) { // Repeat forever
wheels((mbcRead(mbc_port3, 0x03) * 0.8), 0); // Left wheel power = nearer distance × 0.8 (0 to 96), right wheel 0
}
}
void loop() {} // nothing here: the blocks above never returnStep by step
- Wait 1 s for the sensor to boot
- Repeat forever
- Left wheel power = nearer distance × 0.8 (0 to 96), right wheel 0
What you should see: Nothing ahead: the left wheel runs at full speed. Bring your hand closer and it slows; right in front it stops.
FAQ
- Nothing ahead but the value is not 120? → The laser sees the floor or table: mount it higher or tilt it slightly up. Anything beyond 120 cm reads 120.
- Nothing happens, or the value stays at -32768? → Wrong port (every example on this page uses port 3), socket board not seated, or the sensor is not the Makeblock edition. Check with the "MBC common → connected?" block first; "protocol version" must read 2 for the new firmware (old firmware needs extension v1).
- Upload fails, or the blocks are blank? → Make sure the switch at the bottom right is in "Upload" mode, extension v2 has been dropped into mBlock, and the USB cable goes to the mBot board.
Example 4: Drive and stop at the wall
What this example does: The robot drives forward and stops when the nearer distance is below 20 cm; remove the obstacle and it drives on. The most basic "sensor + if + motor" combination.
What you need
- mBot (mCore board) + mBlock 5 desktop
- mBot two-wheel robot (left wheel on M1, right wheel on M2, as shipped)
- Ranger 2-Way (Makeblock edition) + mBot socket board
- A wall or a box as an obstacle
Wiring: Ranger → mBot socket board → mBot port 3. Mount it on the front with both lasers facing forward.
Download
| mBlock 5 project | Equivalent Arduino sketch |
|---|---|
| ex04_stop_wall.mblock | ex04_stop_wall.ino |
mBlock blocks (just rebuild them like this)
when mBot starts up :: events hat
wait (1) seconds
forever
if <(Ranger 2 port [3 v] nearer distance cm :: #0A9E8F) > (20)> then
left wheel power (40) %, right wheel power (40) % :: motion
else
stop moving :: motion
end
end
🔧 Equivalent Arduino sketch (mBot)
// Ranger 2-Way × mBlock 5 Example 4: Drive and stop at the wall (equivalent Arduino sketch for mBot)
// Wiring: Ranger → mBot socket board → mBot port 3. Mount it on the front with both lasers facing forward.
// Does exactly what the mBlock block example does, for people who want to see "what is behind the blocks" or use the Arduino IDE.
// Libraries needed:
// 1. Makeblock (Arduino IDE Library Manager, search "Makeblock", or the copy inside the mBlock 5 install folder)
// 2. MBC_Sensors v2: the four files in src/ of the extension .mext (MBC_Sensors.h/.cpp, mbc_proto.h, mbc_ids.h),
// put them in Documents/Arduino/libraries/MBC_Sensors/ and #include <MBC_Sensors.h> (https://mbc-pro-sensors.github.io/en/makeblock.html)
// Board: Arduino Uno (the mBot mCore is an Uno). After uploading, unplug USB and run on battery.
#include <MeMCore.h> // mBot (mCore): motors, LED, buzzer
#include <SoftwareSerial.h>
#include <MBC_Sensors.h> // MBC sensors v2 (the same library the mBlock extension uses)
MBCPort mbc_port3(3); // sensor on mBot port 3
MeDCMotor motorL(9); // left wheel (M1)
MeDCMotor motorR(10); // right wheel (M2)
MeRGBLed rgbled(7, 2); // on-board RGB LED (2 pixels)
MeBuzzer buzzer; // on-board buzzer
// left / right wheel power -100~100 (positive = forward): the mBot left wheel is mounted mirrored, so M1 gets the negative value; % becomes PWM 0~255
void wheels(int l, int r) {
l = constrain(l, -100, 100); r = constrain(r, -100, 100);
motorL.run(-l * 255 / 100);
motorR.run(r * 255 / 100);
}
void setup() { // everything runs inside setup(), just like the program mBlock generates
mbc_port3.begin();
delay(1000); // Wait 1 s for the sensor to boot
while (1) { // Repeat forever
if ((mbcRead(mbc_port3, 0x03) > 20)) { // If the nearer distance is greater than 20
wheels(40, 40); // Clear ahead: both wheels 40, forward
} else {
wheels(0, 0); // Too close: stop
}
}
}
void loop() {} // nothing here: the blocks above never returnStep by step
- Wait 1 s for the sensor to boot
- Repeat forever
- If the nearer distance is greater than 20
- Clear ahead: both wheels 40, forward
- otherwise:
- Too close: stop
- If the nearer distance is greater than 20
What you should see: The robot drives forward and stops about 20 cm from the wall; it also stops for your hand and continues when you remove it.
FAQ
- It bumps the wall before stopping? → Too fast to brake: lower 40 to 30, or raise 20 to 30.
- The robot turns the wrong way and gets worse? → Swap the "+" and "−" (or swap the left and right wheel powers). The mBot ships with the left wheel on M1 and the right on M2; check your own wiring.
- Nothing ahead but the value is not 120? → The laser sees the floor or table: mount it higher or tilt it slightly up. Anything beyond 120 cm reads 120.
- Nothing happens, or the value stays at -32768? → Wrong port (every example on this page uses port 3), socket board not seated, or the sensor is not the Makeblock edition. Check with the "MBC common → connected?" block first; "protocol version" must read 2 for the new firmware (old firmware needs extension v1).
- Upload fails, or the blocks are blank? → Make sure the switch at the bottom right is in "Upload" mode, extension v2 has been dropped into mBlock, and the USB cable goes to the mBot board.
Example 5: Turn away from the nearer side
What this example does: Two lasers tell you whether the obstacle is on the left or the right. When something is within 20 cm, compare left and right: nearer on the left → turn right, nearer on the right → turn left (red LED); nothing ahead → drive straight (green). A self-driving obstacle avoider.
What you need
- mBot (mCore board) + mBlock 5 desktop
- mBot two-wheel robot (left wheel on M1, right wheel on M2, as shipped)
- Ranger 2-Way (Makeblock edition) + mBot socket board
- A wall or a box as an obstacle
Wiring: Ranger → mBot socket board → mBot port 3. Mount it on the front with both lasers facing forward.
Download
| mBlock 5 project | Equivalent Arduino sketch |
|---|---|
| ex05_avoid.mblock | ex05_avoid.ino |
mBlock blocks (just rebuild them like this)
when mBot starts up :: events hat
wait (1) seconds
forever
if <(Ranger 2 port [3 v] nearer distance cm :: #0A9E8F) < (20)> then
LED [all v] colour red (255) green (0) blue (0) :: looks
if <(Ranger 2 port [3 v] left distance cm :: #0A9E8F) < (Ranger 2 port [3 v] right distance cm :: #0A9E8F)> then
left wheel power (40) %, right wheel power (-40) % :: motion
else
left wheel power (-40) %, right wheel power (40) % :: motion
end
else
LED [all v] colour red (0) green (255) blue (0) :: looks
left wheel power (40) %, right wheel power (40) % :: motion
end
end
🔧 Equivalent Arduino sketch (mBot)
// Ranger 2-Way × mBlock 5 Example 5: Turn away from the nearer side (equivalent Arduino sketch for mBot)
// Wiring: Ranger → mBot socket board → mBot port 3. Mount it on the front with both lasers facing forward.
// Does exactly what the mBlock block example does, for people who want to see "what is behind the blocks" or use the Arduino IDE.
// Libraries needed:
// 1. Makeblock (Arduino IDE Library Manager, search "Makeblock", or the copy inside the mBlock 5 install folder)
// 2. MBC_Sensors v2: the four files in src/ of the extension .mext (MBC_Sensors.h/.cpp, mbc_proto.h, mbc_ids.h),
// put them in Documents/Arduino/libraries/MBC_Sensors/ and #include <MBC_Sensors.h> (https://mbc-pro-sensors.github.io/en/makeblock.html)
// Board: Arduino Uno (the mBot mCore is an Uno). After uploading, unplug USB and run on battery.
#include <MeMCore.h> // mBot (mCore): motors, LED, buzzer
#include <SoftwareSerial.h>
#include <MBC_Sensors.h> // MBC sensors v2 (the same library the mBlock extension uses)
MBCPort mbc_port3(3); // sensor on mBot port 3
MeDCMotor motorL(9); // left wheel (M1)
MeDCMotor motorR(10); // right wheel (M2)
MeRGBLed rgbled(7, 2); // on-board RGB LED (2 pixels)
MeBuzzer buzzer; // on-board buzzer
// left / right wheel power -100~100 (positive = forward): the mBot left wheel is mounted mirrored, so M1 gets the negative value; % becomes PWM 0~255
void wheels(int l, int r) {
l = constrain(l, -100, 100); r = constrain(r, -100, 100);
motorL.run(-l * 255 / 100);
motorR.run(r * 255 / 100);
}
void setup() { // everything runs inside setup(), just like the program mBlock generates
mbc_port3.begin();
delay(1000); // Wait 1 s for the sensor to boot
while (1) { // Repeat forever
if ((mbcRead(mbc_port3, 0x03) < 20)) { // If the nearer distance is less than 20
rgbled.setColor(0, 255, 0, 0); rgbled.show(); // Obstacle: red LED
if ((mbcRead(mbc_port3, 0x01) < mbcRead(mbc_port3, 0x02))) { // If the left distance is less than the right distance
wheels(40, -40); // Nearer on the left: spin right
} else {
wheels(-40, 40); // Nearer on the right: spin left
}
} else {
rgbled.setColor(0, 0, 255, 0); rgbled.show(); // Clear ahead: green LED
wheels(40, 40); // Drive straight
}
}
}
void loop() {} // nothing here: the blocks above never returnStep by step
- Wait 1 s for the sensor to boot
- Repeat forever
- If the nearer distance is less than 20
- Obstacle: red LED
- If the left distance is less than the right distance
- Nearer on the left: spin right
- otherwise:
- Nearer on the right: spin left
- otherwise:
- Clear ahead: green LED
- Drive straight
- If the nearer distance is less than 20
What you should see: The robot drives straight with a green LED; at an obstacle it turns red and spins toward the more open side, then drives on once the way is clear.
FAQ
- Stuck spinning in a corner? → When both sides are close it keeps flipping. Add "if the nearer distance is below 8, reverse for 0.5 s first".
- The robot turns the wrong way and gets worse? → Swap the "+" and "−" (or swap the left and right wheel powers). The mBot ships with the left wheel on M1 and the right on M2; check your own wiring.
- Nothing ahead but the value is not 120? → The laser sees the floor or table: mount it higher or tilt it slightly up. Anything beyond 120 cm reads 120.
- Nothing happens, or the value stays at -32768? → Wrong port (every example on this page uses port 3), socket board not seated, or the sensor is not the Makeblock edition. Check with the "MBC common → connected?" block first; "protocol version" must read 2 for the new firmware (old firmware needs extension v1).
- Upload fails, or the blocks are blank? → Make sure the switch at the bottom right is in "Upload" mode, extension v2 has been dropped into mBlock, and the USB cable goes to the mBot board.
Next steps
- All Ranger blocks and the usable ports per board: Makeblock extension; this product's block table: Ranger 2-Way × mBlock 5
- The same ideas on LEGO: EV3 beginner examples
- Arduino straight to the sensor (I2C edition, no mBot): Arduino examples
More questions
- Blocks keep returning 0? Check the port in the block matches the real port and that the program was uploaded to the board.
- Does the old extension still work? Sensors shipped before 2026-10 use the legacy v1.0.0 extension; old and new blocks are not compatible.
Still stuck? Contact us with your software and a screenshot of the program.
Downloads
- MBC_Pro_Sensors_mBlock5_v2.2.0_import.mext
- MBC_Pro_Sensors_mBlock5_v2.1.0_import.mext
- MBC_Pro_Sensors_mBlock5_v2.0.0_import.mext
- MBC_Pro_Sensors_mBlock5_v1.0.0.mext (legacy, units shipped before 2026-10)
- Download centre (libraries, firmware, catalogue for every product)
Sources
- Host and software list:
_build/guides.json - Values and commands:
_build/protocol/tof2.json(single source of truth) - Setup:
_build/guides/software/mblock-mblock5.en.md、_build/guides/product/tof2/mblock-mblock5.en.md - Examples:
sensors/tof2/mblock-examples.md