Sensors
Mini distance beeper
The closer something comes, the faster it beeps and the redder its light, like the parking sensor on a car.
- Medium
- about 45 min
- ESP32-C3
- 8 parts
- 10 wires
- 58 lines of code
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
A distance on its own is just a number. Turning it into a rhythm and a colour lets you understand it without looking, which is the whole point of a parking sensor.
What you learn
Mapping one measurement onto a beep rate and a colour blend.
How it works
input
HC-SR04 (3.3 V)
decides
ESP32-C3
output
RGB LED
Buzzer
Powered with 5 V through the board's USB-C port.
A reference design. The sketch is compiled with the real ESP32 toolchain, and its pins are checked against the wiring table automatically. It has not yet been built and tested on real parts by us, so wire it on the bench and run the checks before you build it into a case. Real modules vary: follow the labels printed on yours, not their position in the picture.
Parts
0 of 8 ready
No prices here: they change by shop and by week. Buy each part by its name and the note beside it.
You also need
A computer with Arduino IDE 2 installed (free)
3D wiring
One jumper per step, with the USB cable unplugged. Several wires may share 3V3, 5V or GND: the expansion board or breadboard gives each its own pin.
Step 0 of 10
Lay out the parts
Seat the board on its expansion board or breadboard and lay the modules out round it. Nothing is connected yet.
All connections
mini-sonar.ino
// Mini distance beeper
// The closer something gets, the faster it beeps and the redder the light.
const int PIN_SONAR_TRIG = 0;
const int PIN_SONAR_ECHO = 1;
const int PIN_RGB_R = 5;
const int PIN_RGB_G = 6;
const int PIN_RGB_B = 7;
const int PIN_BUZZER_POS = 10;
const float FAR_CM = 100.0; // beyond this: green, silent
const float NEAR_CM = 10.0; // closer than this: solid red, constant tone
unsigned long nextBeep = 0;
float readCm() {
digitalWrite(PIN_SONAR_TRIG, LOW);
delayMicroseconds(2);
digitalWrite(PIN_SONAR_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_SONAR_TRIG, LOW);
unsigned long us = pulseIn(PIN_SONAR_ECHO, HIGH, 30000);
return us == 0 ? 999.0f : us * 0.0343f / 2.0f;
}
void light(int r, int g, int b) {
analogWrite(PIN_RGB_R, r);
analogWrite(PIN_RGB_G, g);
analogWrite(PIN_RGB_B, b);
}
void setup() {
pinMode(PIN_SONAR_TRIG, OUTPUT);
pinMode(PIN_SONAR_ECHO, INPUT);
pinMode(PIN_RGB_R, OUTPUT);
pinMode(PIN_RGB_G, OUTPUT);
pinMode(PIN_RGB_B, OUTPUT);
pinMode(PIN_BUZZER_POS, OUTPUT);
}
void loop() {
float cm = readCm();
if (cm >= FAR_CM) {
light(0, 120, 0);
noTone(PIN_BUZZER_POS);
} else if (cm <= NEAR_CM) {
light(200, 0, 0);
tone(PIN_BUZZER_POS, 1800);
} else {
// 0.0 when far, 1.0 when near
float closeness = (FAR_CM - cm) / (FAR_CM - NEAR_CM);
light((int)(closeness * 200), (int)((1.0f - closeness) * 120), 0);
if (millis() >= nextBeep) {
tone(PIN_BUZZER_POS, 1800, 40);
nextBeep = millis() + 60 + (unsigned long)((1.0f - closeness) * 600);
}
}
delay(40);
}
Libraries
None. It uses only what the Arduino IDE has built in.
Uploading
First time with an ESP32: in Arduino IDE open Boards Manager, search “esp32” and install “esp32 by Espressif Systems”.
- Open the sketch in Arduino IDE.
- Plug the board in with the USB-C cable.
- Tools > Board > ESP32C3 Dev Module (USB CDC On Boot: Enabled), then Tools > Port and choose the board's port.
- Press Upload (the arrow). Wait for “Done uploading”.