Skip to content

Parking Sensor

Light a warning LED when an ultrasonic target is closer than 30 cm.

Parking Sensor: the matching Arduino Uno circuit rendered in Circuitera’s simulator
Matching Circuitera simulator circuit. Real firmware capture; modeled components are idealized.

Wire it up

Disconnect power before changing connections. Follow terminal names rather than wire colors.

FromToWhy it matters
HC-SR04 (sensor1) · VCCUno · 5VPower connection
HC-SR04 (sensor1) · GNDUno · GND.2Ground reference
HC-SR04 (sensor1) · TRIGUno · 7Match this terminal to the sketch
HC-SR04 (sensor1) · ECHOUno · 8Match this terminal to the sketch
Uno · 9Resistor (r-led1) · 1Match this terminal to the sketch
Resistor (r-led1) · 2LED (led1) · AMatch this terminal to the sketch
LED (led1) · CUno · GND.1Ground reference

The complete sketch

parking-sensor.ino
float readDistance() {
  digitalWrite(7, LOW); delayMicroseconds(2);
  digitalWrite(7, HIGH); delayMicroseconds(10); digitalWrite(7, LOW);
  unsigned long us = pulseIn(8, HIGH, 30000);
  return us ? us * 0.0343 / 2.0 : -1;
}
void setup() {
  Serial.begin(9600); pinMode(7, OUTPUT); pinMode(8, INPUT); pinMode(9, OUTPUT);
}
void loop() {
  float cm = readDistance();
  digitalWrite(9, cm > 0 && cm < 30);
  if (cm < 0) Serial.println("No echo");
  else { Serial.print("distance:"); Serial.println(cm); }
  delay(100);
}

The launch buttons use this same code and a matching saved circuit.

Build it step by step

  1. Open a complete project

    Launch in Simulator creates a separate Uno sketch and its matching wired circuit. Existing sketches are preserved. Press Run to compile and execute.

  2. Explore the live circuit

    Set the sensor to 20 cm and then 100 cm. The orange LED should switch on and off while serial output reports distance.

  3. Try it on hardware

    Disconnect power while wiring. Match terminal names in the table, select the exact board, Verify, then connect and upload. Check component ratings and power requirements first.

Understand the sketch

A threshold with an invalid state

Only a valid positive reading below 30 cm activates the LED. Missing echoes are reported separately. This is an educational indicator, not a vehicle safety system.

If something is different

Warning is reversed

The comparison uses distance below 30, not above. Check LED polarity and its series resistor.

Expected output and tested scope

The warning LED lights when the modeled target is closer than the sketch threshold (30 cm).

Version 1 · Last source- and build-matched test: 2026-10-25. Actual shipped AVR-GCC WASM compilation and Uno AVR engine execution; no physical hardware test.

Supported tutorial target: Arduino Uno R3 / ATmega328P. Compile: PASS · Modeled execution: PASS. Other boards require a separate review of pins, voltage, libraries and the exact build profile.

Keep exploring.