Connect the ideas · 16 / 16

Parking Sensor: Distance & Three LEDs

Build a parking indicator with distance thresholds. Test 15 cm and 30 cm boundaries and explain when the red, amber or green light appears.

10 minBeginner
What you’ll learn

Use distance thresholds to select three signal states.

Understand it

A distance at or below 15 cm selects red. Above 15 and at or below 30 selects amber. Above 30 selects green. Mutually exclusive conditions mean only one output is selected at a time.

Your experiment

Run at 40 cm, then test 30, 16 and 15 cm. Record which light appears at each threshold, including equality.

Connections

  • D9 / D10 — Trigger / echo
  • D4 / D3 / D2 — Red / amber / green
  • GND — Common return

Sketch

This sketch illustrates the hardware logic. The workbench runs its corresponding teaching model, not a C++ compiler.

void setup() {
  pinMode(9, OUTPUT);
  pinMode(10, INPUT);
  Serial.begin(9600);
  for (int pin = 2; pin <= 4; pin++) {
    pinMode(pin, OUTPUT);
  }
}

void loop() {
  digitalWrite(9, LOW);
  delayMicroseconds(2);
  digitalWrite(9, HIGH);
  delayMicroseconds(10);
  digitalWrite(9, LOW);
  long cm = pulseIn(10, HIGH) / 58;
  Serial.println(cm);
  digitalWrite(4, cm <= 15);
  digitalWrite(3, cm > 15 && cm <= 30);
  digitalWrite(2, cm > 30);
}

Quick check

Which light appears at exactly 15 cm and exactly 30 cm?

Show explanation

Red at 15 cm and amber at 30 cm. The model uses red for 15 cm or less, amber above 15 through 30, and green above 30.

Go beyond the experiment

Connect the experiment with the fundamentals · Help & FAQ