Add the buzzer to make a desk-size parking sensor: slow beeps when something is near, fast beeps when it is very close.
| Part | How many |
|---|---|
| Wiring from Lessons 1-2 | |
| Active buzzer module | 1 |
| Male-to-female jumper wires | 3 |
Nothing extra - just the kit and your computer.
The zones are the same as Lesson 2. Under 20 cm red now beeps slowly (120 ms on, 380 ms off); under 10 cm it beeps fast (100 ms on, 100 ms off).
beep() never waits. Each time round loop() it checks millis(): if it is time to switch, it flips the buzzer and works out when the next switch is due. So the sensor keeps measuring while it beeps.
With no echo, yellow blinks and the buzzer stays silent: a beep would claim the sketch knows the distance when it does not.
Real parking sensors are tested and certified. This one is a desk model for learning - never use it on a vehicle.
Every item is in the kit — nothing extra to buy.
All 9 parts in one box, with this project and 8 more to build from them.
One of each part. Packs (resistor assortment, jumper wires) already hold more than a single build needs. active-buzzer module, red/yellow/green LEDs: included in the kit, ask us for it loose. Delivery is AED 20, free on orders AED 100+.
Unplug USB before you change any wire. Pin names are the labels printed on the board.
| Part / pin | Connect to | Note |
|---|---|---|
| Buzzer I/O (signal) | UNO pin 8 | May be labelled S or IN |
| Buzzer VCC (+) | + rail (5V) | |
| Buzzer GND (−) | − rail (GND) | |
| LEDs and HC-SR04 | As in Lessons 1-2 | Pins 5, 6, 7, 9, 10 |
One thing at a time. Tick nothing off until it matches the diagram.
Written for the board in the kit. Copy it into the Arduino IDE; change the pin numbers if you wire it differently.
/*
DIY KIT - Arduino Ultrasonic Distance & Parking Learning Kit
Lesson 3 of 9: Parking Beeper
WIRING (unplug the USB cable before you wire)
Keep the sensor and LEDs from Lessons 1-2.
Active buzzer module, with 3 male-to-female jumper wires:
I/O (signal, may be labelled S or IN) -> UNO pin 8
VCC (+) -> 5V (+ rail)
GND (-) -> GND (- rail)
WHAT SHOULD HAPPEN
Green 50 cm or more silent
Yellow 20 to 49 cm silent
Red 10 to 19 cm slow beeps
Red under 10 cm fast beeps
No echo: yellow blinks and the buzzer stays silent.
A tabletop model of a parking sensor - never use it on a real vehicle.
*/
const int RED_LED = 5;
const int YELLOW_LED = 6;
const int GREEN_LED = 7;
const int BUZZER_PIN = 8;
const int TRIG_PIN = 9;
const int ECHO_PIN = 10;
const int CLEAR_CM = 50; // this far or more: green
const int NEAR_CM = 20; // closer than this: red + slow beeps
const int CRITICAL_CM = 10; // closer than this: red + fast beeps
const unsigned long SLOW_ON_MS = 120, SLOW_OFF_MS = 380; // slow beep
const unsigned long FAST_ON_MS = 100, FAST_OFF_MS = 100; // fast beep
// Most modules beep when the pin is HIGH. If yours beeps in the gaps or
// all the time, change false to true and upload again.
const bool BUZZER_ACTIVE_LOW = false;
const unsigned long ECHO_TIMEOUT_US = 30000;
const int MIN_CM = 2;
const int MAX_CM = 400;
bool buzzerOn = false;
unsigned long nextBeepChange = 0;
long readOnceCm() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
unsigned long us = pulseIn(ECHO_PIN, HIGH, ECHO_TIMEOUT_US);
if (us == 0) return 0;
long cm = us / 58;
if (cm < MIN_CM || cm > MAX_CM) return 0;
return cm;
}
void showLights(bool red, bool yellow, bool green) {
digitalWrite(RED_LED, red ? HIGH : LOW);
digitalWrite(YELLOW_LED, yellow ? HIGH : LOW);
digitalWrite(GREEN_LED, green ? HIGH : LOW);
}
void buzzer(bool on) {
buzzerOn = on;
if (BUZZER_ACTIVE_LOW) digitalWrite(BUZZER_PIN, on ? LOW : HIGH);
else digitalWrite(BUZZER_PIN, on ? HIGH : LOW);
}
// Beep with the given on and off times without stopping the program:
// millis() tells us when it is time to switch.
void beep(unsigned long onMs, unsigned long offMs) {
unsigned long now = millis();
if (now >= nextBeepChange) {
buzzer(!buzzerOn);
nextBeepChange = now + (buzzerOn ? onMs : offMs);
}
}
void setup() {
pinMode(RED_LED, OUTPUT);
pinMode(YELLOW_LED, OUTPUT);
pinMode(GREEN_LED, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
buzzer(false);
Serial.begin(9600);
Serial.println("Lesson 3: Parking Beeper - learning model only");
Serial.println("cm, zone, buzzer");
}
void loop() {
long cm = readOnceCm();
const char *zone;
if (cm == 0) {
zone = "NO_ECHO";
unsigned long halfSeconds = millis() / 500; // blink yellow, as in Lesson 2
showLights(false, halfSeconds % 2 == 0, false);
buzzer(false); // unknown: never beep "all clear" or "danger"
} else if (cm < CRITICAL_CM) {
zone = "CRITICAL";
showLights(true, false, false);
beep(FAST_ON_MS, FAST_OFF_MS);
} else if (cm < NEAR_CM) {
zone = "NEAR";
showLights(true, false, false);
beep(SLOW_ON_MS, SLOW_OFF_MS);
} else if (cm < CLEAR_CM) {
zone = "CAUTION";
showLights(false, true, false);
buzzer(false);
} else {
zone = "CLEAR";
showLights(false, false, true);
buzzer(false);
}
Serial.print(cm);
Serial.print(", ");
Serial.print(zone);
Serial.print(", ");
Serial.println(buzzerOn ? 1 : 0);
delay(40);
}
| In the code | What it does |
|---|---|
| const int CRITICAL_CM = 10; | The new, closest zone: fast beeps. |
| const bool BUZZER_ACTIVE_LOW = false; | Most modules beep when the pin is HIGH. If yours beeps in the gaps or all the time, change it to true. |
| beep(onMs, offMs) | Our own function. It uses millis() to flip the buzzer at the right moments without stopping the program. |
| buzzer(false) in the NO_ECHO branch | Silence when the distance is unknown. |
| Problem | What to check |
|---|---|
| No sound at all | Unplug USB. Check I/O to pin 8, VCC to the + rail, GND to the − rail, by the labels on the module. |
| It beeps in the gaps, or all the time | Your module beeps when the pin is LOW. Set BUZZER_ACTIVE_LOW to true and upload again. |
| Only a click, no beep | It may be a passive buzzer, which needs tone(). Check the label says active, or ask us. |
| Beeps but the LEDs are wrong | Check the LEDs against Lesson 2: red 5, yellow 6, green 7. |
| It always says no echo | Unplug USB. Check VCC to the + rail, GND to the − rail, TRIG to pin 9 and ECHO to pin 10 - by the labels on the sensor, not their position. Point it at a big flat object 20-50 cm away. |
| The upload fails | See Start here, step 6: "If the upload fails". |
Hold the book at exactly 10 cm. Does the beep keep switching between slow and fast? Why might that happen at a zone edge?
An active buzzer has its own tone maker and beeps when switched on; a passive one needs the UNO to make the tone.
delay() would stop the program, so the sensor could not measure while the buzzer beeps.
It is an untested learning model: the sensor can miss objects, and real parking aids are tested and certified.
Most problems are one wire in the wrong hole. Check these first:
Open the troubleshooting table for this step →
Hi DIY KIT, I'm stuck on the Arduino Ultrasonic Distance & Parking Learning Kit. Lesson: __ Step number: __ What should happen: __ What happens instead: __ Error message (copied exactly): __ I'll attach a photo of my wiring.
All the parts in one sketch: zones on the LEDs, slow and fast beeps, a median filter, the OLED, and commands like test and sound off. Ends with a battery-powered demo.
Wire the HC-SR04 and read distances in the Serial Monitor. Compare them with a ruler to see how accurate the sensor really is. Do Start here first.
Green, yellow and red LEDs show how close an object is: green 50 cm and more, yellow 20-49 cm, red under 20 cm. Yellow blinks when there is no echo.
We use cookies that the shop needs to work — signing in, your cart, form security. We also measure how the shop is used so we can stock the right parts and fix what is broken. Choose “Only essential” and we will not load Google Analytics or the Meta pixel, and will not record your visit. Read more.