Robot Maker Toolkit › Projects › Parking sensor

Arduino parking sensor

The classic first "real" build: a car-style distance sensor. Green means clear, yellow means you're getting close (with beeps speeding up as you do), red plus a continuous tone means stop. One sensor, three LEDs, a buzzer, and no libraries.

Beginner · ~30 min Arduino UNO No libraries
✔ Wiring datasheet-verified ✔ Code compiles ✔ Simulation-tested (all zones) ◻ Bench-tested — be the first, get credited

Verification: the exact sketch and circuit below were compiled and run in the Wokwi simulator. All four zones were checked against the spec (green >100 cm · yellow 100–30 cm · red <30 cm · continuous tone <10 cm). It has not yet been run on physical hardware — build it, tell us it works via support@infinitxlabs.com, and we'll credit you here.

Parts

Wiring

FromTo (UNO)Notes
HC-SR04 VCC5V5 V sensor — happy on the UNO
HC-SR04 GNDGND
HC-SR04 TRIGD9
HC-SR04 ECHOD10Direct is fine on a 5 V board. On 3.3 V boards (ESP32/Pico) use a voltage divider.
Green LED (+)D4via 220 Ω, cathode to GND
Yellow LED (+)D5via 220 Ω, cathode to GND
Red LED (+)D6via 220 Ω, cathode to GND
Buzzer (+)D3passive buzzer, driven with tone()
Buzzer (−)GND

The HC-SR04 wiring (TRIG→D9, ECHO→D10) matches the audited wiring table in the Robot Maker Toolkit app, so the app's diagram and this project agree.

▶ Run it in your browser first

Try the whole build before you buy a single part — no login, nothing to install:

▶ Open the live simulation

  1. Press the green ▶ play button
  2. Click the HC-SR04 sensor and drag its distance slider
  3. Watch the LEDs switch zones and hear the buzzer speed up as you "reverse"

Prefer your own copy? Download sketch.ino · diagram.json and paste them into a new project at wokwi.com/projects/new/arduino-uno.

How it works

The sketch

sketch.ino (copy everything)
// Parking Sensor — Arduino UNO + HC-SR04 + 3 LEDs + passive buzzer
// Robot Maker Toolkit starter project. Wiring matches the app's audited
// HC-SR04 table: TRIG->D9, ECHO->D10, VCC->5V, GND->GND.

const int trigPin   = 9;
const int echoPin   = 10;
const int greenLed  = 4;   // clear (> 100 cm)
const int yellowLed = 5;   // getting close (30-100 cm)
const int redLed    = 6;   // close (< 30 cm)
const int buzzer    = 3;   // passive buzzer via tone()

const int ZONE_SAFE = 100; // cm
const int ZONE_NEAR = 30;  // cm
const int ZONE_STOP = 10;  // cm - continuous tone below this

void setup() {
  Serial.begin(115200);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(greenLed, OUTPUT);
  pinMode(yellowLed, OUTPUT);
  pinMode(redLed, OUTPUT);
  pinMode(buzzer, OUTPUT);
}

float readDistanceCm() {
  digitalWrite(trigPin, LOW);  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH); delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  long duration = pulseIn(echoPin, HIGH, 30000); // 30 ms timeout (~5 m)
  if (duration == 0) return -1.0;                // no echo = out of range
  return duration * 0.0343f / 2.0f;
}

void loop() {
  float d = readDistanceCm();
  bool valid = d > 0;

  Serial.print("Distance: ");
  if (valid) { Serial.print(d); Serial.println(" cm"); }
  else       { Serial.println("out of range"); }

  digitalWrite(greenLed,  valid && d > ZONE_SAFE  ? HIGH : LOW);
  digitalWrite(yellowLed, valid && d <= ZONE_SAFE && d > ZONE_NEAR ? HIGH : LOW);
  digitalWrite(redLed,    valid && d <= ZONE_NEAR ? HIGH : LOW);

  if (!valid || d > ZONE_SAFE) {
    noTone(buzzer);                 // clear: silence
    delay(150);
  } else if (d > ZONE_STOP) {
    tone(buzzer, 2000, 60);         // beep, faster as you close in
    int gap = map((int)d, ZONE_STOP, ZONE_SAFE, 60, 400);
    delay(constrain(gap, 60, 400));
  } else {
    tone(buzzer, 2000);             // STOP: continuous tone
    delay(60);
  }
}
diagram.json (for the Wokwi simulation)
{
  "version": 1,
  "author": "Infinitx Labs — Robot Maker Toolkit",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-uno", "id": "uno", "top": 60, "left": 0 },
    { "type": "wokwi-hc-sr04", "id": "dist1", "top": -140, "left": 40 },
    { "type": "wokwi-resistor", "id": "r1", "attrs": { "value": "220" }, "top": -90, "left": 320 },
    { "type": "wokwi-resistor", "id": "r2", "attrs": { "value": "220" }, "top": -30, "left": 320 },
    { "type": "wokwi-resistor", "id": "r3", "attrs": { "value": "220" }, "top": 30, "left": 320 },
    { "type": "wokwi-led", "id": "led_g", "attrs": { "color": "green" }, "top": -100, "left": 420 },
    { "type": "wokwi-led", "id": "led_y", "attrs": { "color": "yellow" }, "top": -40, "left": 420 },
    { "type": "wokwi-led", "id": "led_r", "attrs": { "color": "red" }, "top": 20, "left": 420 },
    { "type": "wokwi-buzzer", "id": "bz1", "attrs": { "volume": "0.1" }, "top": 100, "left": 420 }
  ],
  "connections": [
    [ "dist1:VCC", "uno:5V", "red", [] ],
    [ "dist1:GND", "uno:GND.1", "black", [] ],
    [ "dist1:TRIG", "uno:9", "blue", [] ],
    [ "dist1:ECHO", "uno:10", "purple", [] ],
    [ "uno:4", "r1:1", "green", [] ],
    [ "r1:2", "led_g:A", "green", [] ],
    [ "led_g:C", "uno:GND.2", "black", [] ],
    [ "uno:5", "r2:1", "gold", [] ],
    [ "r2:2", "led_y:A", "gold", [] ],
    [ "led_y:C", "uno:GND.2", "black", [] ],
    [ "uno:6", "r3:1", "orange", [] ],
    [ "r3:2", "led_r:A", "orange", [] ],
    [ "led_r:C", "uno:GND.2", "black", [] ],
    [ "uno:3", "bz1:2", "violet", [] ],
    [ "bz1:1", "uno:GND.3", "black", [] ]
  ]
}

Going further

Want the full reference on your bench? 55+ sensor wiring diagrams, 9 board pinouts, shield maps and maker calculators — all offline.

Get it onGoogle Play
Board pinout library →