
Parking Sensor
Intermediate45 minLike the parking sensor in a car: the HC-SR04 measures the distance to whatever is in front of it, the buzzer starts beeping once something comes within 1 metre and the beeps get faster as it comes closer; below 10 cm the tone is steady. Green, yellow and red LEDs show the far, middle and near zones, and the distance is written to the Serial Monitor. It teaches timing with millis() instead of delay, so measuring, beeping and the lights run without waiting for each other, writing a function that returns a value, splitting into zones with if / else if, and turning the distance into a beep gap with map. No extra library needed.

What you need
1 × Arduino Uno R3
1 × Breadboard 830 Points (MB-102)
1 × HC-SR04 Ultrasonic Distance Sensor
1 × Passive Buzzer (Piezo)
1 × LED - Green (5mm)
1 × LED - Yellow (5mm)
1 × LED - Red (5mm)
3 × Resistor 220 Ω (1/4W)
14 × Jumper Wire - Male/Male
Wiring steps
- 1
Connect the power rails (5V and GND)

Plug a red jumper wire into the 5V pin on the Arduino's power header and connect its other end to the breadboard's bottom red (+) rail. Plug a black jumper wire into one of the GND pins on the same header and connect its other end to the bottom blue (−) rail. The sensor is powered from these two rails, and the buzzer's and the LEDs' ground wires go into the (−) rail too.
- Arduino 5V → the bottom red (+) rail (red wire)
- Arduino GND → the bottom blue (−) rail (black wire)
- 2
Add and wire the HC-SR04

Plug the HC-SR04 upright into the bottom half of the breadboard, its "eyes" facing the way it will measure; its four legs go, from left to right, VCC in column 6, TRIG in 7, ECHO in 8 and GND in 9. Connect column 6 (VCC) to the bottom (+) rail with a short red wire and column 9 (GND) to the bottom (−) rail with a short black wire. Connect the Arduino's pin 7 to column 7 with a blue jumper wire (TRIG_PIN 7) and its pin 8 to column 8 with a yellow jumper wire (ECHO_PIN 8). A 10 µs pulse on TRIG makes the sensor send a 40 kHz sound burst; ECHO stays HIGH until the burst has hit the obstacle and come back.
- HC-SR04 VCC → column 6, bottom half
- HC-SR04 TRIG → column 7, bottom half
- HC-SR04 ECHO → column 8, bottom half
- HC-SR04 GND → column 9, bottom half
- column 6, bottom half → the bottom red (+) rail (red wire)
- column 9, bottom half → the bottom blue (−) rail (black wire)
- Arduino pin 7 → column 7, bottom half (blue wire)
- Arduino pin 8 → column 8, bottom half (yellow wire)
- 3
Add and wire the buzzer

Plug the passive buzzer's − leg into column 24 in the bottom half and its + leg into column 25; the + leg is usually the longer one and the body has a + mark. Connect the Arduino's pin 9 to column 25 with a green jumper wire (BUZZER_PIN 9). Connect column 24 to the bottom (−) rail with a short black wire.
- buzzer − leg → column 24, bottom half
- buzzer + leg → column 25, bottom half
- Arduino pin 9 → column 25, bottom half (green wire)
- column 24, bottom half → the bottom blue (−) rail (black wire)
- 4
Add the green LED and its resistor

Plug the green LED's long leg (+) into column 37 in the top half and its short leg (−) into column 36. Put a 220 Ω resistor in column 37, across the middle gap from the top half to the bottom half. Connect the Arduino's pin 10 to column 37 in the bottom half with an orange jumper wire (GREEN_PIN 10). Connect column 36 in the top half to the bottom (−) rail with a short black wire. The resistor limits the current from the pin before it reaches the LED.
- green LED long leg (+) → column 37, top half
- green LED short leg (−) → column 36, top half
- green LED's resistor (leg 1) → column 37, top half
- green LED's resistor (leg 2) → column 37, bottom half
- Arduino pin 10 → column 37, bottom half (orange wire)
- column 36, top half → the bottom blue (−) rail (black wire)
- 5
Add the yellow LED and its resistor

Plug the yellow LED's long leg (+) into column 41 in the top half and its short leg (−) into column 40. Put the second 220 Ω resistor in column 41, across the gap. Connect the Arduino's pin 11 to column 41 in the bottom half with a brown jumper wire (YELLOW_PIN 11). Connect column 40 in the top half to the bottom (−) rail with a short black wire. Pin 11 cannot do PWM while tone() is playing; that does not matter here, since the LED is only switched on and off.
- yellow LED long leg (+) → column 41, top half
- yellow LED short leg (−) → column 40, top half
- yellow LED's resistor (leg 1) → column 41, top half
- yellow LED's resistor (leg 2) → column 41, bottom half
- Arduino pin 11 → column 41, bottom half (brown wire)
- column 40, top half → the bottom blue (−) rail (black wire)
- 6
Add the red LED and its resistor

Plug the red LED's long leg (+) into column 45 in the top half and its short leg (−) into column 44. Put the third 220 Ω resistor in column 45, across the gap. Connect the Arduino's pin 12 to column 45 in the bottom half with a cyan jumper wire (RED_PIN 12). Connect column 44 in the top half to the bottom (−) rail with a short black wire. That completes the circuit.
- red LED long leg (+) → column 45, top half
- red LED short leg (−) → column 44, top half
- red LED's resistor (leg 1) → column 45, top half
- red LED's resistor (leg 2) → column 45, bottom half
- Arduino pin 12 → column 45, bottom half (cyan wire)
- column 44, top half → the bottom blue (−) rail (black wire)
Arduino sketch
Download the sketch (.ino)Try it on Wokwi
// HC-SR04, buzzer and LED pins
const int TRIG_PIN = 7;
const int ECHO_PIN = 8;
const int BUZZER_PIN = 9;
const int GREEN_PIN = 10;
const int YELLOW_PIN = 11;
const int RED_PIN = 12;
// Zones (cm): beeping starts at 100,
// red at 30, a steady tone at 10
const int FAR_CM = 100;
const int NEAR_CM = 30;
const int STOP_CM = 10;
// The beep's pitch (Hz) and length (ms)
const int BEEP_HZ = 2000;
const int BEEP_MS = 50;
// Time between readings: 60 ms at least
const unsigned long MEASURE_EVERY = 100;
unsigned long lastMeasure = 0;
unsigned long lastBeep = 0;
// -1: nothing in front to measure
int distance = -1;
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(YELLOW_PIN, OUTPUT);
pinMode(RED_PIN, OUTPUT);
Serial.begin(9600);
}
// Distance in cm; -1 when no echo comes
int measureCm() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
// A 10 µs pulse starts one reading
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// No echo in 30 ms (~5 m): give up
unsigned long echo = pulseIn(ECHO_PIN, HIGH, 30000);
if (echo == 0) {
return -1;
}
// There and back: µs / 58 = cm
return echo / 58;
}
// Sets all three LEDs: HIGH is on
void lights(int green, int yellow, int red) {
digitalWrite(GREEN_PIN, green);
digitalWrite(YELLOW_PIN, yellow);
digitalWrite(RED_PIN, red);
}
void loop() {
unsigned long now = millis();
// No delay: measure when it is time
if (now - lastMeasure >= MEASURE_EVERY) {
lastMeasure = now;
distance = measureCm();
if (distance < 0) {
Serial.println("Distance: nothing there");
} else {
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
}
}
if (distance < 0 || distance > FAR_CM) {
// Far away: green on, no sound
lights(HIGH, LOW, LOW);
noTone(BUZZER_PIN);
} else if (distance <= STOP_CM) {
// Very close: red and a steady tone
lights(LOW, LOW, HIGH);
tone(BUZZER_PIN, BEEP_HZ);
} else {
if (distance <= NEAR_CM) {
lights(LOW, LOW, HIGH);
} else {
lights(LOW, HIGH, LOW);
}
// The beep is over: stop the sound
if (now - lastBeep >= BEEP_MS) {
noTone(BUZZER_PIN);
}
// Closer means shorter gaps
unsigned long gap = map(distance, STOP_CM, FAR_CM, 80, 800);
if (now - lastBeep >= gap) {
lastBeep = now;
tone(BUZZER_PIN, BEEP_HZ, BEEP_MS);
}
}
}
What the Serial Monitor shows
Distance: nothing there Distance: 120 cm Distance: 85 cm Distance: 42 cm Distance: 18 cm Distance: 7 cm
What you will see
With nothing within 1 metre, the green LED is on and there is no sound. Move your hand towards the sensor: at 100 cm the yellow LED lights and the buzzer starts beeping slowly; the beeps get faster as you come closer, the red LED lights at 30 cm, and below 10 cm the tone is steady. The Serial Monitor shows the distance ten times a second. If the distance always reads "nothing there" or 0, check that the TRIG and ECHO wires are not swapped (pin 7 → TRIG, pin 8 → ECHO) and the sensor's VCC is in the (+) rail. If the distance is right but there is no sound, check that the buzzer is a passive one and its + leg is in column 25. The sensor cannot measure closer than 2 cm and struggles with soft or slanted surfaces.
Similar projects
Traffic LightBeginner20 minA real traffic light with three LEDs: red, red + yellow, green and yellow light up in the order used in Turkey, and each stage is also written to the Serial Monitor. It teaches driving several outputs in turn, timing with digitalWrite and delay, and gathering a repeated job into its own function. You can change how long each light stays on with the constants in the code. No extra library needed.Open project
Air Quality IndicatorBeginner30 minAn indicator that shows a room's air like a traffic light: the MQ-135 sensor reacts to pollutants such as smoke, ammonia, benzene and the CO₂ that builds up from breathing; green lights while the air is clean, yellow as it starts to get stale and red when it is bad, and the Serial Monitor tells you to air the room. At start the sensor warms up and the clean-air value is measured; every reading after that is judged against it, and if the air gets even cleaner the value is updated. It teaches reading an analog sensor against its own clean-air value, reducing jitter by averaging several readings and leaving a margin at the thresholds so the LEDs do not flicker at the edge. No extra library is needed. The MQ-135 does not tell gases apart and does not give a calibrated CO₂ reading; it shows roughly how polluted the air is.Open project
Smoke and Gas AlarmIntermediate35 minAn alarm that goes off when smoke or a flammable gas (LPG, natural gas, methane) shows up in the kitchen or a room: the MQ-2 sensor's analog output is read, the clean air value is measured at start-up, and once the reading rises well above it the buzzer plays the three-beep pattern (T3) that real smoke alarms use while the red LED flashes with the beeps. The green LED blinks while warming up and stays on once the alarm is armed. It teaches reading an analog sensor against its own clean air value, using two separate thresholds so the alarm does not flicker at the edge, and playing a sound pattern with millis() while still taking readings. No extra library needed. This is a learning project; it does not replace a certified smoke and gas alarm at home.Open project
In the app, tick off every wire, match the parts to your inventory and pick up where you left off.
