Ranger 8-Way (TOF8) × 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 8-Way (TOF8) 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.

Ranger 8-Way (TOF8) × Makeblock (mBot) + mBlock 5

Setup

What you need

  • Sensor: Ranger 8-Way (TOF8) (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

  1. Install mBlock 5 for PC from the mBlock website.
  2. Open mBlock 5 and add your board (for example mBot) under "Devices" at the bottom left.
  3. Switch the mode toggle at the bottom right to Upload.

Make the software see the sensor

Add one extension and the MBC blocks appear:

  1. 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.
  2. Drag the .mext file into the mBlock window. Six new categories appear: Pathfinder, Ranger, Controller, IMU gyro, Vision and MBC common.
  3. Start the program with the board's start block (for example "when mBot starts up") and click "Upload" to flash it to the board.
  4. To open an example: "File → Open from your computer" and pick the .mblock file.

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.

Wait 2~4 seconds after power-on

The Ranger 8-Way needs no calibration; plug it in and go. After power-on, wait 2~4 seconds for the 8 lasers to get ready; meanwhile everything reads 120 (cm). Start your program with a short wait before reading distances.

Channel 1 2 3 4 5 6 7 8
Facing left left-front 60° left-front 30° front front right-front 30° right-front 60° right

The table assumes the sensor faces forward with the components up. Put your hand on the robot's left: example 1 should show a negative direction. If it shows a positive one, the sensor is mounted the other way, and "turn left" / "turn right" in the examples must be swapped too.

First test

  1. Start the program with "wait 1 second" so the sensor can boot; reading too early returns -32768.
  2. Check the wiring with the "MBC common → connection OK?" block, then look at the values.
  3. 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 8" category and pick the port the sensor is plugged into.

nearest distance (cm) nearCm

nearest distance (cm)
Range Meaning
0~120 cm smallest of the 8 distances, 0~120 cm (a failed sensor counts as 120); 120 = nothing around
(Ranger 8 port [1 v] nearest distance cm :: sensing)

Block returns nearest distance (cm)(0~120 cm)。Host sends [01] → reply [lo][hi][01 ^ lo ^ hi], value = int16 little-endian.

nearest direction nearDir

nearest direction
Range Meaning
-8~+8 direction of the nearest object: −7 (left) to +7 (right), 0 = straight ahead, negative = left; ±8 = nothing around (keeps the last side)
(Ranger 8 port [1 v] nearest direction :: sensing)

Block returns nearest direction(-8~+8)。Host sends [02] → reply [lo][hi][02 ^ lo ^ hi], value = int16 little-endian.

farthest distance (cm) farCm

farthest distance (cm)
Range Meaning
0~120 cm largest of the 8 distances, 0~120 cm (a failed sensor always reads 120 and looks 'farthest')
(Ranger 8 port [1 v] farthest distance cm :: sensing)

Block returns farthest distance (cm)(0~120 cm)。Host sends [03] → reply [lo][hi][03 ^ lo ^ hi], value = int16 little-endian.

farthest direction farDir

farthest direction
Range Meaning
-7~+7 direction of the most open side: −7 to +7, 0 = straight ahead, negative = left; 0 when all 8 read 120
(Ranger 8 port [1 v] farthest direction :: sensing)

Block returns farthest direction(-7~+7)。Host sends [04] → reply [lo][hi][04 ^ lo ^ hi], value = int16 little-endian.

front distance (cm) frontCm

front distance (cm)
Range Meaning
0~120 cm average of sensors 4 and 5 (both facing forward), 0~120 cm; EV3 mode 3 and SPIKE mode 6 carry the same thing in mm; 0x22 'C' is min(4, 5), not the average (see 10)
(Ranger 8 port [1 v] front distance cm :: sensing)

Block returns front distance (cm)(0~120 cm)。Host sends [05] → reply [lo][hi][05 ^ lo ^ hi], value = int16 little-endian.

nearest distance (mm) nearMm

nearest distance (mm)
Range Meaning
0~1200 mm smallest of the 8 mm readings, 0~1200 (failed sensor counts as 1200); 1200 = nothing around
(Ranger 8 port [1 v] nearest distance mm :: sensing)

Block returns nearest distance (mm)(0~1200 mm)。Host sends [06] → reply [lo][hi][06 ^ lo ^ hi], value = int16 little-endian.

init bitmap initOk

init bitmap
Range Meaning
0~255 bit bit0 = sensor 1 (left) … bit7 = sensor 8; 1 = initialised at power-on (8 sensors take 24 s, no retry); 255 = all OK
(Ranger 8 port [1 v] init bitmap :: sensing)

Block returns init bitmap(0~255 bit)。Host sends [07] → reply [lo][hi][07 ^ lo ^ hi], value = int16 little-endian.

fault bitmap fault

fault bitmap
Range Meaning
0~255 bit bit0 = sensor 1 … bit7 = sensor 8; 1 = currently failing (I2C error or no update for 200 ms); its distance reads 120 / 1200
(Ranger 8 port [1 v] fault bitmap :: sensing)

Block returns fault bitmap(0~255 bit)。Host sends [08] → reply [lo][hi][08 ^ lo ^ hi], value = int16 little-endian.

front distance (mm) frontMm

front distance (mm)
Range Meaning
0~1200 mm average of sensors 4 and 5 (the two facing forward), 0~1200 mm; 1200 = nothing ahead. cm version is 05
(Ranger 8 port [1 v] front distance mm :: sensing)

Block returns front distance (mm)(0~1200 mm)。Host sends [09] → reply [lo][hi][09 ^ lo ^ hi], value = int16 little-endian.

sensor 1~8 distance (cm) distCm

sensor 1~8 distance (cm)
Range Meaning
0~120 cm distance of each sensor 18, 0120 cm (1 = far left, 4 and 5 = front, 8 = far right, 30° apart); 120 = nothing that way / sensor failed; ids 10~17
(Ranger 8 port [1 v] sensor [1 v] distance cm :: sensing)

Block returns sensor 1~8 distance (cm)(0~120 cm)。Host sends [10] → reply [lo][hi][10 ^ lo ^ hi], value = int16 little-endian; channel N uses id 0x10 + (N − 1).

sensor 1~8 distance (mm) distMm

sensor 1~8 distance (mm)
Range Meaning
0~1200 mm distance of each sensor 18, 01200 mm; 1200 = nothing that way / sensor failed; ids 18~1F
(Ranger 8 port [1 v] sensor [1 v] distance mm :: sensing)

Block returns sensor 1~8 distance (mm)(0~1200 mm)。Host sends [18] → reply [lo][hi][18 ^ lo ^ hi], value = int16 little-endian; channel N uses id 0x18 + (N − 1).

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 8 port [1 v] update all values :: sensing

Examples

This page is for people who have never written a program: read the Ranger 8-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

Ranger 8-Way × mBot wiring

Sensor → mBot socket board → RJ25 port on the board (S1 = host TX, S2 = host RX, UART 38400).

  1. Install the extension: get the MBC Pro Sensors v2.0.0 .mext from Makeblock extension and drop it into the mBlock 5 window; add mBot under "Devices" (bottom left) and switch to "Upload" (bottom right).
  2. 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).
  3. Let it boot first: every example starts with "wait 3 seconds" so the sensor can boot; the 8 lasers take 2~4 s to initialise and read 120 meanwhile, which is normal.
  4. The Ranger 8-way needs no calibration. The 8 lasers form a half circle: sensor 1 far left, 4 and 5 straight ahead, 8 far right (30° apart); "nearest direction" negative = left, 0 = ahead, positive = right; nothing around reads 120.
  5. Check left/right with example 2 first: a hand on the left of the robot must give a red LED; blue means the sensor is mounted reversed and left/right in the later examples will be swapped.

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 8 port [3] nearest distance cm 0~120 cm smallest of the 8 distances, 0~120 cm (a failed sensor counts as 120); 120 = nothing around
Ranger 8 port [3] nearest direction -8~+8 direction of the nearest object: −7 (left) to +7 (right), 0 = straight ahead, negative = left; ±8 = nothing around (keeps the last side)
Ranger 8 port [3] farthest direction -7~+7 direction of the most open side: −7 to +7, 0 = straight ahead, negative = left; 0 when all 8 read 120

Examples on this page

  1. Nearest-distance theremin
  2. The LED shows which side the object is on
  3. One wheel points the robot at the nearest object
  4. Obstacle-avoiding drive
  5. Head for open space

Note

The .mblock files 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: Nearest-distance theremin

What this example does: The mBot has no screen, so this example turns the "nearest distance" (the smallest of the 8, 0 to 120 cm) into a pitch: whichever direction something approaches from, the note drops. Learn to "read the number" first.

What you need

  • mBot (mCore board) + mBlock 5 desktop
  • Ranger 8-Way (Makeblock edition) + mBot socket board

Wiring: Ranger → mBot socket board → mBot port 3. Mount it at the very front with sensors 4 and 5 facing forward (sensor 1 far left, 8 far right).

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 (3) seconds
forever
  play tone ((200) + ((Ranger 8 port [3 v] nearest distance cm :: #0A9E8F) * (10))) Hz for (0.1) seconds :: sound
end
🔧 Equivalent Arduino sketch (mBot)
// Ranger 8-Way × mBlock 5  Example 1: Nearest-distance theremin  (equivalent Arduino sketch for mBot)
// Wiring: Ranger → mBot socket board → mBot port 3. Mount it at the very front with sensors 4 and 5 facing forward (sensor 1 far left, 8 far right).
// 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(3000);   // Wait 3 s for the 8 lasers to initialise
  while (1) {   // Repeat forever
    buzzer.tone((200 + (mbcRead(mbc_port3, 0x01) * 10)), 100);   // Pitch = 200 + nearest distance × 10 Hz, for 0.1 s
  }
}

void loop() {}   // nothing here: the blocks above never return

Step by step

  1. Wait 3 s for the 8 lasers to initialise
  2. Repeat forever
    • Pitch = 200 + nearest distance × 10 Hz, for 0.1 s

What you should see: Nothing around: a high 1400 Hz. Approach from the left, front or right and the pitch drops; 300 Hz at 10 cm.

FAQ

  • No sound? → The buzzer is on the board and is not loud; listen closely, or change 0.1 s to 0.3 s.
  • Low note right after power-on? → During the first 3 s the lasers initialise and read 120 (high note). A constant low note means something is near (table, wall); give the robot more room.
  • Left and right are swapped? → The sensor is mounted upside down or rotated 180°. Check with example 2: a hand on the left of the robot must give a red LED.
  • 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: The LED shows which side the object is on

What this example does: "Nearest direction" says where the nearest object is: negative = left, 0 = straight ahead, positive = right. With "if… then… else…": something within 60 cm on the left → red, right → blue, straight ahead → green; nothing → LED off.

What you need

  • mBot (mCore board) + mBlock 5 desktop
  • Ranger 8-Way (Makeblock edition) + mBot socket board

Wiring: Ranger → mBot socket board → mBot port 3. Mount it at the very front with sensors 4 and 5 facing forward (sensor 1 far left, 8 far right).

Download

mBlock 5 project Equivalent Arduino sketch
ex02_led_direction.mblock ex02_led_direction.ino
mBlock blocks (just rebuild them like this)
when mBot starts up :: events hat
wait (3) seconds
forever
  if <(Ranger 8 port [3 v] nearest distance cm :: #0A9E8F) > (60)> then
    LED [all v] colour red (0) green (0) blue (0) :: looks
  else
    if <(Ranger 8 port [3 v] nearest direction :: #0A9E8F) < (0)> then
      LED [all v] colour red (255) green (0) blue (0) :: looks
    else
      if <(Ranger 8 port [3 v] nearest direction :: #0A9E8F) > (0)> then
        LED [all v] colour red (0) green (0) blue (255) :: looks
      else
        LED [all v] colour red (0) green (255) blue (0) :: looks
      end
    end
  end
end
🔧 Equivalent Arduino sketch (mBot)
// Ranger 8-Way × mBlock 5  Example 2: The LED shows which side the object is on  (equivalent Arduino sketch for mBot)
// Wiring: Ranger → mBot socket board → mBot port 3. Mount it at the very front with sensors 4 and 5 facing forward (sensor 1 far left, 8 far right).
// 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(3000);   // Wait 3 s for the 8 lasers to initialise
  while (1) {   // Repeat forever
    if ((mbcRead(mbc_port3, 0x01) > 60)) {   // If the nearest distance is greater than 60
      rgbled.setColor(0, 0, 0, 0); rgbled.show();   // Nothing within 60 cm: LED off
    } else {
      if ((mbcRead(mbc_port3, 0x02) < 0)) {   // Otherwise, if the nearest direction is less than 0 (left)
        rgbled.setColor(0, 255, 0, 0); rgbled.show();   // Nearest object on the left: red
      } else {
        if ((mbcRead(mbc_port3, 0x02) > 0)) {   // Otherwise, if the nearest direction is greater than 0 (right)
          rgbled.setColor(0, 0, 0, 255); rgbled.show();   // On the right: blue
        } else {
          rgbled.setColor(0, 0, 255, 0); rgbled.show();   // Straight ahead: green
        }
      }
    }
  }
}

void loop() {}   // nothing here: the blocks above never return

Step by step

  1. Wait 3 s for the 8 lasers to initialise
  2. Repeat forever
    • If the nearest distance is greater than 60
      • Nothing within 60 cm: LED off
      • otherwise:
        • Otherwise, if the nearest direction is less than 0 (left)
          • Nearest object on the left: red
          • otherwise:
            • Otherwise, if the nearest direction is greater than 0 (right)
              • On the right: blue
              • otherwise:
                • Straight ahead: green

What you should see: Hand on the left of the robot: red; right: blue; straight ahead: green; take it away (beyond 60 cm) and the LED goes off.

FAQ

  • Left and right are swapped? → The sensor is mounted upside down or rotated 180°. Check with example 2: a hand on the left of the robot must give a red LED.
  • 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: One wheel points the robot at the nearest object

What this example does: Left wheel only: power = nearest direction × 6. Object on the right (positive) → left wheel forward, nose turns right; on the left (negative) → wheel reverses, nose turns left; straight ahead (0) → still. Nothing within 60 cm → stop. Put it on the floor and watch the nose follow your hand.

What you need

  • mBot (mCore board) + mBlock 5 desktop
  • mBot two-wheel robot (left wheel on M1, right wheel on M2, as shipped)
  • Ranger 8-Way (Makeblock edition) + mBot socket board
  • A few boxes as obstacles

Wiring: Ranger → mBot socket board → mBot port 3. Mount it at the very front with sensors 4 and 5 facing forward (sensor 1 far left, 8 far right).

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 (3) seconds
forever
  if <(Ranger 8 port [3 v] nearest distance cm :: #0A9E8F) > (60)> then
    stop moving :: motion
  else
    left wheel power ((Ranger 8 port [3 v] nearest direction :: #0A9E8F) * (6)) %, right wheel power (0) % :: motion
  end
end
🔧 Equivalent Arduino sketch (mBot)
// Ranger 8-Way × mBlock 5  Example 3: One wheel points the robot at the nearest object  (equivalent Arduino sketch for mBot)
// Wiring: Ranger → mBot socket board → mBot port 3. Mount it at the very front with sensors 4 and 5 facing forward (sensor 1 far left, 8 far right).
// 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(3000);   // Wait 3 s for the 8 lasers to initialise
  while (1) {   // Repeat forever
    if ((mbcRead(mbc_port3, 0x01) > 60)) {   // If the nearest distance is greater than 60
      wheels(0, 0);   // Nothing within 60 cm: stop
    } else {
      wheels((mbcRead(mbc_port3, 0x02) * 6), 0);   // Left wheel power = nearest direction × 6 (−42 to +42), right wheel 0
    }
  }
}

void loop() {}   // nothing here: the blocks above never return

Step by step

  1. Wait 3 s for the 8 lasers to initialise
  2. Repeat forever
    • If the nearest distance is greater than 60
      • Nothing within 60 cm: stop
      • otherwise:
        • Left wheel power = nearest direction × 6 (−42 to +42), right wheel 0

What you should see: Hand at the front right: the left wheel turns forward and the nose swings toward your hand, stopping when it faces you; move your hand to the left and the wheel reverses.

FAQ

  • 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.
  • Left and right are swapped? → The sensor is mounted upside down or rotated 180°. Check with example 2: a hand on the left of the robot must give a red LED.
  • 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: Obstacle-avoiding drive

What this example does: The robot drives straight; when something is within 20 cm it checks which side: on the left → spin right, on the right (or straight ahead) → spin left, then drives on once the way is clear.

What you need

  • mBot (mCore board) + mBlock 5 desktop
  • mBot two-wheel robot (left wheel on M1, right wheel on M2, as shipped)
  • Ranger 8-Way (Makeblock edition) + mBot socket board
  • A few boxes as obstacles

Wiring: Ranger → mBot socket board → mBot port 3. Mount it at the very front with sensors 4 and 5 facing forward (sensor 1 far left, 8 far right).

Download

mBlock 5 project Equivalent Arduino sketch
ex04_avoid.mblock ex04_avoid.ino
mBlock blocks (just rebuild them like this)
when mBot starts up :: events hat
wait (3) seconds
forever
  if <(Ranger 8 port [3 v] nearest distance cm :: #0A9E8F) < (20)> then
    if <(Ranger 8 port [3 v] nearest direction :: #0A9E8F) < (0)> then
      left wheel power (40) %, right wheel power (-40) % :: motion
    else
      left wheel power (-40) %, right wheel power (40) % :: motion
    end
  else
    left wheel power (40) %, right wheel power (40) % :: motion
  end
end
🔧 Equivalent Arduino sketch (mBot)
// Ranger 8-Way × mBlock 5  Example 4: Obstacle-avoiding drive  (equivalent Arduino sketch for mBot)
// Wiring: Ranger → mBot socket board → mBot port 3. Mount it at the very front with sensors 4 and 5 facing forward (sensor 1 far left, 8 far right).
// 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(3000);   // Wait 3 s for the 8 lasers to initialise
  while (1) {   // Repeat forever
    if ((mbcRead(mbc_port3, 0x01) < 20)) {   // If the nearest distance is less than 20
      if ((mbcRead(mbc_port3, 0x02) < 0)) {   // If the nearest direction is less than 0 (left)
        wheels(40, -40);   // Obstacle on the left: spin right
      } else {
        wheels(-40, 40);   // Obstacle on the right or ahead: spin left
      }
    } else {
      wheels(40, 40);   // Clear: drive straight
    }
  }
}

void loop() {}   // nothing here: the blocks above never return

Step by step

  1. Wait 3 s for the 8 lasers to initialise
  2. Repeat forever
    • If the nearest distance is less than 20
      • If the nearest direction is less than 0 (left)
        • Obstacle on the left: spin right
        • otherwise:
          • Obstacle on the right or ahead: spin left
      • otherwise:
        • Clear: drive straight

What you should see: The robot weaves between the boxes, turning away before it touches anything.

FAQ

  • It scrapes boxes with its side? → The side lasers (1 and 8) count toward "nearest distance"; raise 20 (e.g. to 30) to keep more clearance.
  • 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.
  • Left and right are swapped? → The sensor is mounted upside down or rotated 180°. Check with example 2: a hand on the left of the robot must give a red LED.
  • 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: Head for open space

What this example does: Example 4 only turns when it meets something; this one steers toward the "farthest direction" as it drives: turn = farthest direction × 6, left wheel 35 + turn, right wheel 35 − turn, so the robot drifts around obstacles toward the widest space (green LED). If something is really close (within 10 cm) it backs up with a red LED.

What you need

  • mBot (mCore board) + mBlock 5 desktop
  • mBot two-wheel robot (left wheel on M1, right wheel on M2, as shipped)
  • Ranger 8-Way (Makeblock edition) + mBot socket board
  • A few boxes as obstacles

Wiring: Ranger → mBot socket board → mBot port 3. Mount it at the very front with sensors 4 and 5 facing forward (sensor 1 far left, 8 far right).

Download

mBlock 5 project Equivalent Arduino sketch
ex05_open_space.mblock ex05_open_space.ino
mBlock blocks (just rebuild them like this)
when mBot starts up :: events hat
wait (3) seconds
forever
  if <(Ranger 8 port [3 v] nearest distance cm :: #0A9E8F) < (10)> then
    LED [all v] colour red (255) green (0) blue (0) :: looks
    left wheel power (-30) %, right wheel power (-30) % :: motion
  else
    LED [all v] colour red (0) green (255) blue (0) :: looks
    set [turn v] to ((Ranger 8 port [3 v] farthest direction :: #0A9E8F) * (6))
    left wheel power ((35) + (turn)) %, right wheel power ((35) - (turn)) % :: motion
  end
end
🔧 Equivalent Arduino sketch (mBot)
// Ranger 8-Way × mBlock 5  Example 5: Head for open space  (equivalent Arduino sketch for mBot)
// Wiring: Ranger → mBot socket board → mBot port 3. Mount it at the very front with sensors 4 and 5 facing forward (sensor 1 far left, 8 far right).
// 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
float turn = 0;               // variable:turn

// 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(3000);   // Wait 3 s for the 8 lasers to initialise
  while (1) {   // Repeat forever
    if ((mbcRead(mbc_port3, 0x01) < 10)) {   // If the nearest distance is less than 10
      rgbled.setColor(0, 255, 0, 0); rgbled.show();   // Too close: red LED
      wheels(-30, -30);   // Back up
    } else {
      rgbled.setColor(0, 0, 255, 0); rgbled.show();   // Green LED
      turn = (mbcRead(mbc_port3, 0x04) * 6);   // turn = farthest direction × 6 (−42 to +42)
      wheels((35 + turn), (35 - turn));   // Left = 35 + turn, right = 35 − turn: open space on the left (negative) → left wheel slow, drift left
    }
  }
}

void loop() {}   // nothing here: the blocks above never return

Step by step

  1. Wait 3 s for the 8 lasers to initialise
  2. Repeat forever
    • If the nearest distance is less than 10
      • Too close: red LED
      • Back up
      • otherwise:
        • Green LED
        • turn = farthest direction × 6 (−42 to +42)
        • Left = 35 + turn, right = 35 − turn: open space on the left (negative) → left wheel slow, drift left

What you should see: The robot keeps heading for the most open direction, steering around boxes early; when boxed in it backs up with a red LED.

FAQ

  • The robot keeps swinging left and right? → Lower × 6 (e.g. × 4).
  • 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.
  • Left and right are swapped? → The sensor is mounted upside down or rotated 180°. Check with example 2: a hand on the left of the robot must give a red LED.
  • 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

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

Sources

  • Host and software list:_build/guides.json
  • Values and commands:_build/protocol/tof8.json (single source of truth)
  • Setup:_build/guides/software/mblock-mblock5.en.md、_build/guides/product/tof8/mblock-mblock5.en.md
  • Examples:sensors/tof8/mblock-examples.md
Email for a quoteBuying Guide