মূল বিষয়বস্তুতে যান
Back to Projects
IntermediateArduino Uno R3
35 minutes build

Parking sensor

An ultrasonic sensor with beeps that speed up as an obstacle approaches.

Interactive Simulation

Parking sensorarduino-uno
Open in Lab
UNOATmega328P: The 8-bit AVR microcontroller that runs your sketch (16 MHz, 32 KB flash, 2 KB RAM).ATmega328PUSB-B: USB connection for uploading sketches and for the Serial Monitor. Also powers the board (5 V).DC jack: Barrel jack for 7–12 V from a battery pack or adapter (centre positive).ATmega16U2: Converts USB to serial for uploads and Serial.print().RESET: Restarts your sketch from the beginning (setup() runs again).L (D13): Built-in LED connected to pin 13 (LED_BUILTIN).ON: Power indicator.5 V regulator: Turns 7–12 V from VIN/jack into a steady 5 V.16 MHz: Clock source: the microcontroller runs 16 million cycles per second.ICSP: In-circuit programming header (SPI). Used to burn a bootloader; not needed for normal uploads.60 cm220 Ω+

Required Components Checklist

Assembly Steps

1

Connect the sensor

VCC to 5V, GND to GND, TRIG to pin 7, ECHO to pin 6.

2

Measure

Send a 10 µs pulse, time the echo with pulseIn() and divide by 58 for centimetres.

3

Give feedback

Map the distance to the pause between beeps; light the LED when closer than 20 cm.

Arduino Sketch

const int TRIG = 7;
const int ECHO = 6;
const int BUZZER = 8;
const int LED = 12;

long readCm() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);   // a 10 µs pulse starts a measurement
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  long us = pulseIn(ECHO, HIGH, 30000); // echo time in µs (0 = nothing in range)
  return us / 58;                       // sound: ~58 µs per cm, there and back
}

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(LED, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  long cm = readCm();
  Serial.print(cm);
  Serial.println(" cm");
  if (cm > 0 && cm < 100) {
    digitalWrite(LED, cm < 20 ? HIGH : LOW);
    tone(BUZZER, 1000, 60);
    delay(map(cm, 0, 100, 60, 600)); // closer = faster beeps
  } else {
    digitalWrite(LED, LOW);
    delay(300);
  }
}

Challenges & Extensions

Ready to take this project further? Try these challenges:

  • •Make a continuous tone below 10 cm.
  • •Average 5 readings to smooth out jumps.
  • •Add a second LED for a green/red indicator.