mBot Parking Sensor: Beeps That Speed Up as You Get Closer
The mBot works like a car's parking sensor: as the object in front gets closer the beeps come faster, very close the sound becomes continuous, and far away the robot stays quiet. No extra parts are needed. The sound starts at 60 centimeters and becomes continuous closer than 8 centimeters.

Video
Circuit Diagram

Source Code
1// ─────────────────────────────────────────
2// BlueGrays · bluegrays.com
3// mBot Parking Sensor: Beeps That Speed Up as You Get Closer
4// ─────────────────────────────────────────
5// The mBot works like a car's parking sensor: as the object in front gets
6// closer the beeps come faster, very close the sound becomes continuous,
7// and far away the robot stays quiet. No extra parts are needed.
8//
9// Pins:
10// D8 -> Onboard buzzer
11// PORT_3 (A3) -> Ultrasonic sensor
12
13#include <MeMCore.h>
14
15// ============ SETTINGS ============
16const float NEAR_CM = 8; // Continuous sound limit (cm)
17const float FAR_CM = 60; // Silence limit (cm)
18const uint16_t BEEP_HZ = 1000; // Beep frequency (Hz)
19const unsigned long BEEP_MS = 60; // Length of one beep (ms)
20const unsigned long MIN_GAP_MS = 40; // Wait between beeps when closest (ms)
21const unsigned long MAX_GAP_MS = 600; // Wait between beeps when farthest (ms)
22const unsigned long READ_MS = 100; // Time between readings (ms)
23
24MeBuzzer buzzer;
25MeUltrasonicSensor ultrasonic(PORT_3);
26
27void setup() {
28}
29
30void loop() {
31 float cm = ultrasonic.distanceCm(); // Distance in centimeters
32 if (cm > FAR_CM) {
33 delay(READ_MS);
34 return;
35 }
36 buzzer.tone(BEEP_HZ, BEEP_MS);
37 if (cm > NEAR_CM) {
38 // map turns the distance into a waiting time: the closer the object, the shorter the wait.
39 delay(map((long)cm, (long)NEAR_CM, (long)FAR_CM, MIN_GAP_MS, MAX_GAP_MS));
40 }
41}
42FAQ
How do I change the distance where the sound starts?
`FAR_CM` is the distance where the robot starts beeping and `NEAR_CM` is where the sound becomes continuous. To try it on a small desk, set `FAR_CM` to 30. The longest and shortest wait between beeps are set with `MAX_GAP_MS` and `MIN_GAP_MS`. `NEAR_CM` must always stay smaller than `FAR_CM`.
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.
