Ultrasonic parking sensor with buzzer and LEDs on Arduino: design worksheet and code

A ₹806 design worksheet for a garage parking aid: an HC-SR04 reads the distance, three LEDs show the zone and a buzzer beeps faster as the car gets closer, with the timing maths worked out.

About the author · Editorial & verification standards

  • Parts₹806
  • Bench time2–3 h
  • LevelBeginner
  • ReviewedNov 2026

A ₹806 parking aid that beeps faster as something gets closer. An HC-SR04 ultrasonic sensor measures the distance to a wall or bumper, three LEDs show green, yellow or red, and a buzzer beeps with a gap that shrinks with the distance. The page works out the timing so you can see what the numbers mean before you build it.

Scope: beginner. This is a design worksheet with illustrative costs. It has not been bench-tested by this site. The distance-to-beep table below was computed by script. It is a hobby aid, not a safety device: do not rely on it to prevent a collision.

How it works

The sensor sends a burst of 40 kHz sound and times the echo. At about 343 m/s (20 °C) sound travels 1 cm out and back in about 58.3 microseconds, so the distance in centimetres is the echo time divided by 58.3. A car 100 cm away returns an echo after about 5,830 microseconds. Many listings give the usable range as roughly 2 to 400 cm; the sketch only cares about the first metre.

Beeping is done without delay() so the sensor keeps being read. Each time the gap since the last beep reaches the interval for the current distance, the buzzer is switched on for 60 ms. Below 20 cm the buzzer stays on. Above 100 cm it is silent.

Bill of materials

PartQtyUnitTotal
Arduino Uno R3 (or compatible)1₹550₹550
HC-SR04 ultrasonic sensor1₹110₹110
Active buzzer module1₹15₹15
LEDs (green, yellow, red)3₹5₹15
220 ohm resistors3₹2₹6
Breadboard1₹60₹60
Jumper wires1₹50₹50
Parts total₹806

Scroll the table sideways to see every column.

Prices checked Nov 2026. Shipping and tools are not included.

Prices are illustrative figures for Indian sellers in November 2026, before shipping, and have not been checked against current listings.

The numbers

DistanceEcho timeBeep gapLED
100 cm5,830 µs800 msGreen
60 cm3,500 µs480 msGreen
50 cm2,915 µs400 msYellow
30 cm1,750 µs240 msYellow
25 cm1,458 µs200 msRed
20 cm1,166 µsContinuousRed
Gap = distance in cm × 8 ms between 20 and 100 cm. Echo time = distance × 58.3 µs. Zones: above 60 cm green, 30 to 60 yellow, below 30 red.

Temperature changes the speed of sound by about 0.6 m/s per degree, so on a hot day the reading is a few percent off. That is fine for a parking aid; see the temperature-compensated ultrasonic distance page if you want it corrected.

Power and wiring

PartConnects toNote
HC-SR04 VCC and GND5V and GNDSensor is a 5 V part
HC-SR04 TRIGD9
HC-SR04 ECHOD10Echo is 5 V; fine for an Uno
Buzzer signalD8Active buzzer, high = on
LEDs through 220 ohmD2, D3, D4Green, yellow, red
Mount the sensor facing straight out, a little above the lowest part of the bumper, so it sees the wall and not the floor.

The sketch

// Ultrasonic parking sensor: Uno + HC-SR04 + buzzer + 3 LEDs
// Design worksheet: distances and gaps are starting values.

const int TRIG = 9, ECHO = 10, BUZ = 8;
const int LED_G = 2, LED_Y = 3, LED_R = 4;

unsigned long lastBeep = 0, beepUntil = 0;

float readCm() {
  digitalWrite(TRIG, LOW);  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  unsigned long t = pulseIn(ECHO, HIGH, 25000UL);   // 25 ms timeout, about 4 m
  if (t == 0) return 999;                            // no echo
  return t / 58.3;
}

void setup() {
  pinMode(TRIG, OUTPUT); pinMode(ECHO, INPUT); pinMode(BUZ, OUTPUT);
  pinMode(LED_G, OUTPUT); pinMode(LED_Y, OUTPUT); pinMode(LED_R, OUTPUT);
}

void loop() {
  float d = readCm();
  digitalWrite(LED_G, d > 60);
  digitalWrite(LED_Y, d <= 60 && d >= 30);
  digitalWrite(LED_R, d < 30);

  unsigned long now = millis();
  if (d > 100) {
    digitalWrite(BUZ, LOW);
  } else if (d < 20) {
    digitalWrite(BUZ, HIGH);                         // continuous
  } else {
    unsigned long gap = (unsigned long)(d * 8);
    if (now - lastBeep >= gap) { lastBeep = now; beepUntil = now + 60; }
    digitalWrite(BUZ, now < beepUntil);
  }
  delay(40);                                         // let echoes die away
}

The 40 ms pause between readings stops one echo being mistaken for the next. A reading of 999 means no echo came back; a soft surface such as a cloth can absorb the sound and look like open space. The sketch has been proofread but not compiled on this site.

Test plan

  1. Bench check: hold a book at 100, 60, 30 and 20 cm from a ruler and note which LED lights and how the beeping changes.
  2. Accuracy: compare the reading (print it with Serial) against the ruler at five distances and record the error.
  3. Surface test: try a wall, a metal panel, a cardboard box and a cloth. Soft or sloped surfaces return weaker echoes.
  4. Angle test: approach a wall at an angle and see where the reading stops being reliable.
  5. In place: mount it in the garage and roll a toy or slow-moving object toward the wall.

What usually goes wrong

SymptomLikely causeFix
Always reads 999Echo or trigger wires swapped, or sensor not poweredCheck TRIG on D9, ECHO on D10 and the 5 V supply
Reading jumps aroundSoft or angled surface, or electrical noiseAverage three readings and aim at a flat surface
Beeps never stopDistance reads under 20 cmCheck for the sensor seeing its own mounting
Buzzer is faintPassive buzzer usedUse an active buzzer, or drive a passive one with tone()
LED zone flickers at the edgeNo margin between zonesAdd a 3 cm margin before switching zone

Upgrades

  • Display: show the distance on a small OLED or a 7-segment display.
  • Two sensors: add a second sensor for the side of the car.
  • Temperature correction: add a DS18B20 and correct the speed of sound.
  • Stop line: flash all LEDs at the exact stop distance you choose.

Cost and parts notes

The ₹806 total is an illustration and does not include shipping. Use the cost calculator to compare sellers.

Common questions

Listings commonly give 2 to 400 cm, and the sketch here only uses the first metre. Range depends on the surface and angle.

Sound covers about 0.0343 cm per microsecond at 20 °C, and the echo travels there and back, so each centimetre of distance adds about 58.3 µs.

No. It is a hobby aid and can miss soft, thin or sloped objects.

Yes, through the Uno USB socket. The sensor draws little current.

Last reviewed: November 2026. This is a design worksheet with illustrative costs. It has not been bench-tested by this site; library names, pins and limits must be checked against your board and the current library versions, and measurements from your build are welcome.

More builds

Next builds to try

Free: the final-year project pack

A shortlist with BOMs, a report outline that matches the usual chapter scheme, a costing sheet and 40 viva questions. Built for the February–April submission season.

The Build Note

One short email when something is worth your bench time.

A new build sheet, a part price that moved, or a mistake worth avoiding. No daily drip, no recycled news.

One short email when a build or a price is worth your time. Unsubscribe in one click.