Legacy edition, no longer supported: SharpShot Vision units shipped before 2026-10 (School and WRO editions) are no longer supported; their guides and examples stay here for reference. To upgrade to the current SharpShot Vision, contact the distributor you bought it from and send it back: the firmware update is free, and the buyer pays shipping both ways.

This page has moved into the new guide: SharpShot Vision › mBlock › mBlock 5

mBlock beginner examples: SharpShot (school edition)

Back to the SharpShot (school edition) product page

This page is for people who have never written a program: read the SharpShot (school edition) 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

SharpShot (school edition) × 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: SharpShot → mBot socket board → mBot port 1 (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 1 seconds" so the sensor can boot; reading too early gives -32768 (nothing received yet).
  4. Set the colour on the sensor itself (programs cannot change it): plug the sensor into the mBot, power on and hold the coloured brick in front of the camera. With RUN at the top of the screen, short-press the side button for the menu: SET 1:COLOR sets the sample point for "hue" (example 1: use left/right to move the red and green lines onto the brick, keep the default tolerances); SET 2:TRACK sets the blob to follow (examples 2~5: centre the cross on the brob, tune TOL_HUE / TOL_STA until only your brick is marked, raise MIN AREA to drop small specks). Settings are stored in the sensor.
  5. Leave the flash LED off (bricks reflect and turn white). Back on the RUN screen the top line keeps showing H… (hue) A… (area) X… Y…; those are the numbers the examples read.

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
SharpShot Vision (legacy) port [1] hue -1~100 hue of sample point S1, 0100 (hue ring 0240 scaled); −1 = black/white/grey (saturation < 10); 0 when S1 is not set
SharpShot Vision (legacy) port [1] blob found? true / false 1 = a blob of the target colour is in view (W > 0), 0 = none
SharpShot Vision (legacy) port [1] blob X 0~100 blob centre X 0~100, left = 0; 0 when not found
SharpShot Vision (legacy) port [1] blob area 0~7500 W×H (0~7500); 0 when not found

Examples on this page

  1. Hear the colour
  2. Green when the blob is found
  3. One wheel turns toward the blob
  4. Follow the blob
  5. Follow, stop when close, with lights

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: Hear the colour

What this example does: The mBot has no screen, so this example turns the sample point "hue" (0 to 100: red 0, yellow ≈17, green ≈33, blue ≈67; black/white/grey = −1) into a buzzer pitch. Learn to "read the number" first.

What you need

  • mBot (mCore board) + mBlock 5 desktop
  • SharpShot (school edition) (Makeblock edition) + mBot socket board
  • A brightly coloured brick (red, green or blue)

Wiring: SharpShot → mBot socket board → mBot port 1. Camera facing forward on the front. First set the colour to look for on the sensor itself (see "Before you start").

Download

mBlock 5 project Equivalent Arduino sketch
ex01_hear_colour.mblock ex01_hear_colour.ino
mBlock blocks (just rebuild them like this)
when mBot starts up :: events hat
wait (1) seconds
forever
  play tone ((200) + ((SharpShot Vision \(legacy\) port [1 v] hue :: #C2255C) * (6))) Hz for (0.1) seconds :: sound
end
🔧 Equivalent Arduino sketch (mBot)
// SharpShot (school edition) × mBlock 5  Example 1: Hear the colour  (equivalent Arduino sketch for mBot)
// Wiring: SharpShot → mBot socket board → mBot port 1. Camera facing forward on the front. First set the colour to look for on the sensor itself (see "Before you start").
// 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_port1(1);           // sensor on mBot port 1
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_port1.begin();
  delay(1000);   // Wait 1 s for the sensor to boot
  while (1) {   // Repeat forever
    buzzer.tone((200 + (mbcRead(mbc_port1, 0x01) * 6)), 100);   // Pitch = 200 + hue × 6 Hz, for 0.1 s
  }
}

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

Step by step

  1. Wait 1 s for the sensor to boot
  2. Repeat forever
    • Pitch = 200 + hue × 6 Hz, for 0.1 s

What you should see: Red brick at the sample point: a low 200 Hz; green about 400 Hz; blue about 600 Hz; white paper or black gives the lowest note (194 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.
  • The colour changes but the note does not? → The brick is not at the sample point (the cross on the sensor screen); use the COLOR setting to move the cross to where the brick will be.
  • Always "not found"? → The TRACK setting on the sensor is not done, or the tolerance is too small. Check the sensor screen: a tracking box means found.
  • Nothing happens, or the value stays at -32768? → Wrong port (every example on this page uses port 1), 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: Green when the blob is found

What this example does: With the "blob found?" boolean and "if… then… else…": the set colour in view → green LED, not in view → red. The first decision of every blob-tracking program.

What you need

  • mBot (mCore board) + mBlock 5 desktop
  • SharpShot (school edition) (Makeblock edition) + mBot socket board
  • A brightly coloured brick (red, green or blue)

Wiring: SharpShot → mBot socket board → mBot port 1. Camera facing forward on the front. First set the colour to look for on the sensor itself (see "Before you start").

Download

mBlock 5 project Equivalent Arduino sketch
ex02_found_led.mblock ex02_found_led.ino
mBlock blocks (just rebuild them like this)
when mBot starts up :: events hat
wait (1) seconds
forever
  if <SharpShot Vision \(legacy\) port [1 v] blob found? :: #C2255C> then
    LED [all v] colour red (0) green (255) blue (0) :: looks
  else
    LED [all v] colour red (255) green (0) blue (0) :: looks
  end
end
🔧 Equivalent Arduino sketch (mBot)
// SharpShot (school edition) × mBlock 5  Example 2: Green when the blob is found  (equivalent Arduino sketch for mBot)
// Wiring: SharpShot → mBot socket board → mBot port 1. Camera facing forward on the front. First set the colour to look for on the sensor itself (see "Before you start").
// 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_port1(1);           // sensor on mBot port 1
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_port1.begin();
  delay(1000);   // Wait 1 s for the sensor to boot
  while (1) {   // Repeat forever
    if ((mbcRead(mbc_port1, 0x0A) == 1)) {   // If a blob is found
      rgbled.setColor(0, 0, 255, 0); rgbled.show();   // Blob found: green
    } else {
      rgbled.setColor(0, 255, 0, 0); rgbled.show();   // Not found: red
    }
  }
}

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

Step by step

  1. Wait 1 s for the sensor to boot
  2. Repeat forever
    • If a blob is found
      • Blob found: green
      • otherwise:
        • Not found: red

What you should see: Hold the brick in front of the camera: green (a tracking box also appears on the sensor screen); take it away: red.

FAQ

  • Always "not found"? → The TRACK setting on the sensor is not done, or the tolerance is too small. Check the sensor screen: a tracking box means found.
  • Nothing happens, or the value stays at -32768? → Wrong port (every example on this page uses port 1), 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 turns toward the blob

What this example does: "Blob X" is where the blob is across the picture (0 far left, 50 centre, 100 far right). Left wheel only: power = (blob X − 50) × 0.8; blob on the right → wheel forward, nose turns right; on the left → reverse, nose turns left; centre → still. Not found → stop.

What you need

  • mBot (mCore board) + mBlock 5 desktop
  • mBot two-wheel robot (left wheel on M1, right wheel on M2, as shipped)
  • SharpShot (school edition) (Makeblock edition) + mBot socket board
  • A brightly coloured brick (red, green or blue)

Wiring: SharpShot → mBot socket board → mBot port 1. Camera facing forward on the front. First set the colour to look for on the sensor itself (see "Before you start").

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
  if <SharpShot Vision \(legacy\) port [1 v] blob found? :: #C2255C> then
    left wheel power (((SharpShot Vision \(legacy\) port [1 v] blob X :: #C2255C) - (50)) * (0.8)) %, right wheel power (0) % :: motion
  else
    stop moving :: motion
  end
end
🔧 Equivalent Arduino sketch (mBot)
// SharpShot (school edition) × mBlock 5  Example 3: One wheel turns toward the blob  (equivalent Arduino sketch for mBot)
// Wiring: SharpShot → mBot socket board → mBot port 1. Camera facing forward on the front. First set the colour to look for on the sensor itself (see "Before you start").
// 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_port1(1);           // sensor on mBot port 1
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_port1.begin();
  delay(1000);   // Wait 1 s for the sensor to boot
  while (1) {   // Repeat forever
    if ((mbcRead(mbc_port1, 0x0A) == 1)) {   // If a blob is found
      wheels(((mbcRead(mbc_port1, 0x05) - 50) * 0.8), 0);   // Left wheel power = (blob X − 50) × 0.8 (−40 to +40), right wheel 0
    } else {
      wheels(0, 0);   // Not found: stop
    }
  }
}

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

Step by step

  1. Wait 1 s for the sensor to boot
  2. Repeat forever
    • If a blob is found
      • Left wheel power = (blob X − 50) × 0.8 (−40 to +40), right wheel 0
      • otherwise:
        • Not found: stop

What you should see: Hold the brick at the front right: the left wheel turns forward and the nose swings toward it, stopping when centred; move it 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 mirrored? → H-Mirror is on in the camera settings; turn it off, or change 0.8 to -0.8.
  • Always "not found"? → The TRACK setting on the sensor is not done, or the tolerance is too small. Check the sensor screen: a tracking box means found.
  • Nothing happens, or the value stays at -32768? → Wrong port (every example on this page uses port 1), 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: Follow the blob

What this example does: Example 3 with both wheels: turn = (blob X − 50) × 0.6, left wheel 35 + turn, right wheel 35 − turn, so the robot drives forward while keeping the blob centred, i.e. it follows the brick. Not found → stop.

What you need

  • mBot (mCore board) + mBlock 5 desktop
  • mBot two-wheel robot (left wheel on M1, right wheel on M2, as shipped)
  • SharpShot (school edition) (Makeblock edition) + mBot socket board
  • A brightly coloured brick (red, green or blue)

Wiring: SharpShot → mBot socket board → mBot port 1. Camera facing forward on the front. First set the colour to look for on the sensor itself (see "Before you start").

Download

mBlock 5 project Equivalent Arduino sketch
ex04_follow.mblock ex04_follow.ino
mBlock blocks (just rebuild them like this)
when mBot starts up :: events hat
wait (1) seconds
forever
  if <SharpShot Vision \(legacy\) port [1 v] blob found? :: #C2255C> then
    set [turn v] to (((SharpShot Vision \(legacy\) port [1 v] blob X :: #C2255C) - (50)) * (0.6))
    left wheel power ((35) + (turn)) %, right wheel power ((35) - (turn)) % :: motion
  else
    stop moving :: motion
  end
end
🔧 Equivalent Arduino sketch (mBot)
// SharpShot (school edition) × mBlock 5  Example 4: Follow the blob  (equivalent Arduino sketch for mBot)
// Wiring: SharpShot → mBot socket board → mBot port 1. Camera facing forward on the front. First set the colour to look for on the sensor itself (see "Before you start").
// 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_port1(1);           // sensor on mBot port 1
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_port1.begin();
  delay(1000);   // Wait 1 s for the sensor to boot
  while (1) {   // Repeat forever
    if ((mbcRead(mbc_port1, 0x0A) == 1)) {   // If a blob is found
      turn = ((mbcRead(mbc_port1, 0x05) - 50) * 0.6);   // turn = (blob X − 50) × 0.6 (−30 to +30)
      wheels((35 + turn), (35 - turn));   // Left = 35 + turn, right = 35 − turn: blob on the right (positive) → left wheel fast, turn right
    } else {
      wheels(0, 0);   // Not found: stop
    }
  }
}

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

Step by step

  1. Wait 1 s for the sensor to boot
  2. Repeat forever
    • If a blob is found
      • turn = (blob X − 50) × 0.6 (−30 to +30)
      • Left = 35 + turn, right = 35 − turn: blob on the right (positive) → left wheel fast, turn right
      • otherwise:
        • Not found: stop

What you should see: Move the brick slowly in front of the robot and it follows; hide the brick and it stops.

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.
  • The robot swings left and right? → Lower 0.6 (e.g. 0.4).
  • Always "not found"? → The TRACK setting on the sensor is not done, or the tolerance is too small. Check the sensor screen: a tracking box means found.
  • Nothing happens, or the value stays at -32768? → Wrong port (every example on this page uses port 1), 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: Follow, stop when close, with lights

What this example does: Examples 2 and 4 combined, plus "blob area": not found → stop with red; area over 2500 (the brick is close) → stop with blue and a beep; otherwise follow with green. The full "find the target → drive to it → stop there" routine.

What you need

  • mBot (mCore board) + mBlock 5 desktop
  • mBot two-wheel robot (left wheel on M1, right wheel on M2, as shipped)
  • SharpShot (school edition) (Makeblock edition) + mBot socket board
  • A brightly coloured brick (red, green or blue)

Wiring: SharpShot → mBot socket board → mBot port 1. Camera facing forward on the front. First set the colour to look for on the sensor itself (see "Before you start").

Download

mBlock 5 project Equivalent Arduino sketch
ex05_follow_stop.mblock ex05_follow_stop.ino
mBlock blocks (just rebuild them like this)
when mBot starts up :: events hat
wait (1) seconds
forever
  if <SharpShot Vision \(legacy\) port [1 v] blob found? :: #C2255C> then
    if <(SharpShot Vision \(legacy\) port [1 v] blob area :: #C2255C) > (2500)> then
      stop moving :: motion
      LED [all v] colour red (0) green (0) blue (255) :: looks
      play tone (880) Hz for (0.3) seconds :: sound
    else
      LED [all v] colour red (0) green (255) blue (0) :: looks
      set [turn v] to (((SharpShot Vision \(legacy\) port [1 v] blob X :: #C2255C) - (50)) * (0.6))
      left wheel power ((35) + (turn)) %, right wheel power ((35) - (turn)) % :: motion
    end
  else
    stop moving :: motion
    LED [all v] colour red (255) green (0) blue (0) :: looks
  end
end
🔧 Equivalent Arduino sketch (mBot)
// SharpShot (school edition) × mBlock 5  Example 5: Follow, stop when close, with lights  (equivalent Arduino sketch for mBot)
// Wiring: SharpShot → mBot socket board → mBot port 1. Camera facing forward on the front. First set the colour to look for on the sensor itself (see "Before you start").
// 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_port1(1);           // sensor on mBot port 1
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_port1.begin();
  delay(1000);   // Wait 1 s for the sensor to boot
  while (1) {   // Repeat forever
    if ((mbcRead(mbc_port1, 0x0A) == 1)) {   // If a blob is found
      if ((mbcRead(mbc_port1, 0x09) > 2500)) {   // If the blob area is greater than 2500
        wheels(0, 0);   // Blob is large (close): stop
        rgbled.setColor(0, 0, 0, 255); rgbled.show();   // Blue LED
        buzzer.tone(880, 300);   // Beep
      } else {
        rgbled.setColor(0, 0, 255, 0); rgbled.show();   // Green LED
        turn = ((mbcRead(mbc_port1, 0x05) - 50) * 0.6);   // turn = (blob X − 50) × 0.6
        wheels((35 + turn), (35 - turn));   // Left = 35 + turn, right = 35 − turn
      }
    } else {
      wheels(0, 0);   // Not found: stop
      rgbled.setColor(0, 255, 0, 0); rgbled.show();   // Red LED
    }
  }
}

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

Step by step

  1. Wait 1 s for the sensor to boot
  2. Repeat forever
    • If a blob is found
      • If the blob area is greater than 2500
        • Blob is large (close): stop
        • Blue LED
        • Beep
        • otherwise:
          • Green LED
          • turn = (blob X − 50) × 0.6
          • Left = 35 + turn, right = 35 − turn
      • otherwise:
        • Not found: stop
        • Red LED

What you should see: Brick 1 m ahead: the robot drives to it with a green LED, turns blue and beeps as it stops in front; remove the brick and it goes red.

FAQ

  • It stops too early, or only after bumping the brick? → Area depends on brick size: read A (area) on the sensor screen at the distance where you want it to stop, then change 2500.
  • 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.
  • Always "not found"? → The TRACK setting on the sensor is not done, or the tolerance is too small. Check the sensor screen: a tracking box means found.
  • Nothing happens, or the value stays at -32768? → Wrong port (every example on this page uses port 1), 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

Email for a quoteBuying Guide