Build an Arduino parking assistant that shows distance on an I²C LCD, fades an RGB LED, and beeps faster as your car approaches an obstacle.
What Is the HC-SR04 Ultrasonic Sensor?

The HC-SR04 is a low-cost ultrasonic rangefinder that emits a 40 kHz pulse and measures the echo’s round-trip time to calculate distance from roughly 2 cm to 4 m. It offers ~3 mm resolution, needs only one trigger and one echo pin, and runs happily from 5 V—perfect for quick proximity projects.

How the HC-SR04 Works

  1. Trigger — a 10 µs HIGH pulse starts a burst of ultrasonic “pings.”
  2. Echo — the module sets its ECHO pin HIGH while waiting for the echo.
  3. Timing — the pulse width equals the sound’s flight time; divide by 58.2 to convert microseconds to centimetres.
    Because it is self-calibrated, you just pulse, measure, and convert—no external components required.

     

Wiring Diagram

HC-SR04 TRIGD12

HC-SR04 ECHOD11

RGB LED (common-anode)

Red → D7 through 220 Ω

Green → D4 through 220 Ω

Blue → D2 through 220 Ω (kept OFF in code)

Piezo buzzer +D8 (– to GND)

I²C LCD: SDA → A4, SCL → A5, VCC → 5 V, GND → GND

 

 

Complete Code:
 

/* DIY Arduino Parking Assistant
   HC-SR04 + I2C LCD + RGB LED + Buzzer
   Author: SOSLAB (soslab.net)
*/

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

// ---------- Pins ----------
#define PIN_RED     7
#define PIN_GREEN   4
#define PIN_BLUE    2
#define PIN_TRIG   12
#define PIN_ECHO   11
#define PIN_BUZZER  8

// ---------- Thresholds (cm) ----------
const uint16_t DIST_FAR  = 100;   // ≥ 100 cm → solid green
const uint16_t DIST_NEAR =   5;   // ≤ 5 cm  → solid red
//--------------------------------------

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  pinMode(PIN_TRIG, OUTPUT);
  pinMode(PIN_ECHO, INPUT);
  pinMode(PIN_RED,   OUTPUT);
  pinMode(PIN_GREEN, OUTPUT);
  pinMode(PIN_BLUE,  OUTPUT);
  pinMode(PIN_BUZZER, OUTPUT);

  lcd.init();
  lcd.backlight();
  lcd.print("Parking Assist");
}

void loop() {
  long dist = readDistanceCm();

  // ----- LCD -----
  lcd.setCursor(0, 1);
  lcd.print("Dist: ");
  lcd.print(dist);
  lcd.print(" cm   ");

  // ----- Outputs -----
  fadeLed(dist);
  beep(dist);

  delay(50);                  // 20 Hz refresh
}

// ---------- Helpers ----------
long readDistanceCm() {
  digitalWrite(PIN_TRIG, LOW);  delayMicroseconds(2);
  digitalWrite(PIN_TRIG, HIGH); delayMicroseconds(10);
  digitalWrite(PIN_TRIG, LOW);

  return pulseIn(PIN_ECHO, HIGH) / 58.2;  // µs → cm
}

void fadeLed(long d) {
  d = constrain(d, DIST_NEAR, DIST_FAR);
  uint8_t g = map(d, DIST_NEAR, DIST_FAR,   0, 255);
  uint8_t r = map(d, DIST_NEAR, DIST_FAR, 255,   0);
  analogWrite(PIN_RED,   r);
  analogWrite(PIN_GREEN, g);
  analogWrite(PIN_BLUE,  0);    // blue unused
}

void beep(long d) {
  static unsigned long t0 = 0;
  unsigned long now = millis();
  uint16_t period =
      (d > 50) ? 1000 :
      (d > 20) ?  500 :
      (d > 10) ?  200 :
      (d >  5) ?  100 : 0;

  if (period == 0) { tone(PIN_BUZZER, 2000); return; }  // steady tone
  if (now - t0 >= period) { tone(PIN_BUZZER, 2000, 50); t0 = now; }
}

 

 

 

 

 

Conclusion

You now have a compact parking aid that delivers visual, numeric, and audible cues, making tight manoeuvres safer and stress-free. Tweak the distance thresholds, add multiple sensors for wider coverage, or 3D-print an enclosure—let your creativity steer the next upgrade!