MB-12mBotBeginner

mBot Distance Meter: Centimeters with the Ultrasonic Sensor

The ultrasonic sensor at the front of the mBot measures how far away the object in front of it is and prints the result to the Serial port in centimeters. With nothing in front, three dashes are printed. No extra parts are needed. The sensor is plugged into port 3; the measurement repeats about five times a second.

0.0(0)
mBot Distance Meter: Centimeters with the Ultrasonic Sensor
The project that uses the mBot's eyes for the first time. You learn to define the ultrasonic sensor in code, to read the distance in centimeters, to print a decimal number with one digit, to handle the case where the sensor cannot measure, and to watch the results in the Serial Monitor. Reading a distance is the first step of many later projects, from the obstacle-avoiding robot to the parking sensor.

Video

Circuit Diagram

mBot Distance Meter: Centimeters with the Ultrasonic Sensor - Circuit Diagram

Source Code

C++ (Arduino IDE)
1// ─────────────────────────────────────────
2//   BlueGrays · bluegrays.com
3//   mBot Distance Meter: Centimeters with the Ultrasonic Sensor
4// ─────────────────────────────────────────
5// The ultrasonic sensor at the front of the mBot measures how far away the
6// object in front of it is and prints the result to the Serial port in
7// centimeters. With nothing in front, three dashes are printed. No extra
8// parts are needed.
9//
10// Pins:
11//   PORT_3 (A3) -> Ultrasonic sensor
12
13#include <MeMCore.h>
14
15// ============ SETTINGS ============
16const unsigned long READ_MS = 200;        // Time between readings (ms)
17const float MAX_CM = 400;                 // Farthest reading (cm)
18
19MeUltrasonicSensor ultrasonic(PORT_3);
20
21void setup() {
22  Serial.begin(9600);
23}
24
25void loop() {
26  float cm = ultrasonic.distanceCm();     // Distance in centimeters
27  // With nothing in front or the object too far away the sensor returns 400; then dashes are
28  // printed.
29  if (cm >= MAX_CM) {
30    Serial.println("---");
31  } else {
32    Serial.print(cm, 1);
33    Serial.println(" cm");
34  }
35  delay(READ_MS);
36}
37

FAQ

Where do I see the measured value?

With the robot connected to the computer by USB, open the Serial Monitor in the Arduino IDE and set the speed at the bottom right to 9600. Every measurement arrives as a new line, such as 23.5 cm. Move your hand back and forth in front of the sensor and you will see the number change. With nothing in front you see three dashes.

How does the ultrasonic sensor measure distance?

One of the sensor's two eyes sends out a sound too high for us to hear, and the other listens for it to bounce back from an object. The time the sound takes to go and return gives the distance; the library does the math and returns the result in centimeters. The sensor measures from about 3 cm to 400 cm; with nothing in front it returns 400.

Which board do I pick for the mBot in the Arduino IDE?

The mBot's board, the mCore, uses the same processor as the Arduino Uno (ATmega328P). Choose Arduino Uno under Tools > Board and the robot's port under Tools > Port. For the code to compile, the MakeBlock Drive Updated library must be installed from the Library Manager; the code pulls it in under the name MeMCore.h.

Does uploading this code erase the factory program on the robot?

Yes. The board holds one program at a time, so your code replaces the factory program and the ready-made modes, such as driving with the remote or line following, stop working. Bringing them back is easy: open mBlock, connect the mBot over USB and load the factory firmware again from the Firmware Update section.