Arduino Parking Sensor Project: Simple Ultrasonic Distance Detector
An Arduino parking sensor uses an
ultrasonic sensor to measure distance to obstacles and alerts the driver with a buzzer or LED. The Arduino reads the sensor's signals, calculates distance, and triggers warnings when objects are too close.Syntax
The basic syntax for using an ultrasonic sensor with Arduino involves defining trigger and echo pins, sending a pulse, and measuring the time it takes for the echo to return. This time is then converted to distance.
- pinMode(pin, OUTPUT/INPUT): Sets the sensor pins as output or input.
- digitalWrite(pin, HIGH/LOW): Sends a pulse to trigger the sensor.
- pulseIn(pin, HIGH): Measures the duration of the echo pulse.
- delay(ms): Pauses the program for a short time.
arduino
const int trigPin = 9; const int echoPin = 10; void setup() { pinMode(trigPin, OUTPUT); // Trigger pin sends pulse pinMode(echoPin, INPUT); // Echo pin receives pulse Serial.begin(9600); // Start serial communication } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); long duration = pulseIn(echoPin, HIGH); long distance = duration * 0.034 / 2; // Convert to cm Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm"); delay(500); }
Output
Distance: 15 cm
Distance: 14 cm
Distance: 16 cm
...
Example
This example shows a complete Arduino sketch for a parking sensor that uses an ultrasonic sensor to measure distance and lights up an LED and sounds a buzzer when an object is closer than 20 cm.
arduino
const int trigPin = 9; const int echoPin = 10; const int buzzerPin = 11; const int ledPin = 13; void setup() { pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(buzzerPin, OUTPUT); pinMode(ledPin, OUTPUT); Serial.begin(9600); } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); long duration = pulseIn(echoPin, HIGH); long distance = duration * 0.034 / 2; Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm"); if (distance > 0 && distance < 20) { digitalWrite(ledPin, HIGH); // Turn on LED tone(buzzerPin, 1000); // Sound buzzer } else { digitalWrite(ledPin, LOW); // Turn off LED noTone(buzzerPin); // Stop buzzer } delay(200); }
Output
Distance: 18 cm
Distance: 15 cm
Distance: 22 cm
Distance: 19 cm
...
Common Pitfalls
Common mistakes include:
- Not setting the trigger pin LOW before sending the pulse, causing incorrect readings.
- Using
delay()too long, making the sensor slow to respond. - Incorrectly calculating distance by forgetting to divide the duration by 2.
- Not checking if the distance is zero or very large, which can mean no object detected.
arduino
/* Wrong way: Missing LOW before HIGH pulse */ digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); /* Right way: Set LOW first to clear signal */ digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW);
Quick Reference
Tips for building an Arduino parking sensor:
- Use an HC-SR04 ultrasonic sensor for easy distance measurement.
- Connect trigger to a digital output pin and echo to a digital input pin.
- Calculate distance with
distance = duration * 0.034 / 2(cm). - Use a buzzer and LED to alert when distance is below a safe threshold.
- Test sensor readings with
Serial.print()to debug.
Key Takeaways
Use an ultrasonic sensor with Arduino to measure distance by timing echo pulses.
Trigger the sensor with a short HIGH pulse after setting the trigger pin LOW.
Calculate distance by multiplying pulse duration by 0.034 and dividing by 2.
Add buzzer and LED alerts to warn when objects are too close.
Always test sensor readings with serial output to ensure accuracy.