Exercise 30 - Making a parking sensor
1. INTRODUCTION
A parking sensor is a system that some vehicles have. When you are parking the sensor measure the distance between the vehicle and obstacles, and send a sound warning if too small. On the other hand, if the distance decreases the beep frequency increases its value.
Most of real vehicles have several sensors, which are activated when reverse gear is selected. This exercise only uses one ultrasonic sensor, the same that in CONNECTING_A_ULTRASONIC_SENSOR.
2. HOW TO CONNECT?
Connect the buzzer to digital pin11. The ultrasonic sensor terminals (Vcc, trig, echo and gnd) are connected to 5V, Pin4, Pin5 and GND respectively.
EXERCISE_A (PARKING SENSOR) |
![]()
|
Materials:
· Arduino UNO board
· Protoboard
· Some wire Jumpers
· Buzzer
· Ultrasonic sensor
3. PROGRAMMING
Code based on Ignacio Miró Orozco (Professor at Polytechnic University of Valencia) code.
This program simulates parking system of a car turning on and turning off a sound with more or less frequency in function of distance
#define echoPin 5 // Echo Pin
#define trigPin 4 // Trigger Pin
#define buzzer 11 // Buzzer
long distance;
long times;
int del;
On setup is set trigger pin as output and echo pin as input.
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
On loop, the value of distance is printed in serial monitor. This value also compare with model values to determine the frequency of beep.
void loop() {
digitalWrite(trigPin, LOW); /*This line stabilize the sensor*/
delayMicroseconds(5);
digitalWrite(trigPin, HIGH); /*Send ultrasonic pulse*/
delayMicroseconds(10);
times = pulseIn(echoPin, HIGH); /*How much time wastes the pulse to arrive to sensor second time*/
distance = int(0.017 * times);
Serial.print("Distancia ");
Serial.print(distance);
Serial.println(" cm");
del ,that Depends of the distance, will have a determined value, which will oscillate between 100 and 800.
if (distance <= 20) {
if (distance >= 0 && distance <= 3) del = 100;
if (distance > 3 && distance <= 6) del = 200;
if (distance > 6 && distance <= 9) del = 300;
if (distance > 9 && distance <= 12) del = 400;
if (distance > 12 && distance <= 15) del = 600;
if (distance > 15) del = 800;
tone(buzzer, 349);
delay(del);
noTone(buzzer);
if (distance >= 3) delay(200);
}
else
{
delay(200);
noTone(buzzer);
}
}