Parking Sensor – The Most Affordable Solution
Components and Supplies Needed:
- Jumper Wires (Generic)
- Ultrasonic Sensor – HC-SR04 (Generic)
- Arduino UNO
- Buzzer
- Breadboard (Generic)
Project Description
In this tutorial, you will learn how to create a simple parking sensor using the HC-SR04 ultrasonic proximity sensor and a buzzer. This budget-friendly sensor can help detect nearby obstacles with ease.
How Does a Proximity Sensor Work?
Imagine a bat searching for food in the dark. Since it cannot see well at night, it uses ultrasound to navigate. When the sound waves emitted by the bat hit an object (like an insect), they bounce back as an echo. By calculating the time taken for the echo to return, the bat can determine the object’s distance and direction.
The HC-SR04 ultrasonic sensor works similarly:
- It emits sound waves and measures the time it takes for the sound to bounce back after hitting an obstacle.
- Using the speed of sound (approximately 343 m/s), the distance to the object is calculated.
Key Details:
- Sound Speed Conversion:
To work with centimeters and microseconds, the speed of sound is converted to 0.0343 cm/μs. This value is defined in the code for accurate calculations. - Sensor Range:
- The HC-SR04 detects objects up to 4 meters away and within a 15° cone from its viewpoint.
- When the obstacle is closer than a specific distance (e.g., 100 cm), the buzzer starts beeping.
- Beep Pattern:
- At 1 meter, the buzzer beeps every 400 milliseconds.
- At 0.5 meters, the beeping interval reduces to 200 milliseconds.
- If the distance is 5 cm or less, the buzzer emits a continuous sound.
Why Build This Project?
This project is ideal for beginners to understand basic electronics and coding with Arduino. It also demonstrates the practical use of ultrasonic sensors in real-life scenarios, like parking assistance systems in vehicles.
Code:
<pre><code>
/**************************************************************
2 * PARKING SENSOR WITH HC-SR04 *
3 * *
4 * This code receives data from the HC-SR04 proximity *
5 * sensor, analyses them, sends them to the serial monitor *
6 * and produces intermittent sounds to warn of an obstacle. *
7 **************************************************************/
8
9// Definition of trigger, echo, beep pins and other constants
10#define trigger 2
11#define echo 3
12#define beep 11
13#define beep_start 100
14#define min_distance 5
15
16// Definition of sound speed (centimetres / microsecond)
17#define c 0.0343
18
19// Definition of the variables
20long tempo;
21float space;
22
23void setup() {
24 // Definition of input and output
25 pinMode(trigger, OUTPUT);
26 pinMode(echo, INPUT);
27 pinMode(beep, OUTPUT);
28
29 // Serial communication initialisation (optional)
30 Serial.begin(9600);
31}
32
33void loop() {
34 // Before measurement, the trigger is set to low level
35 digitalWrite(trigger, LOW);
36 delayMicroseconds(5);
37
38 // Send one pulse (trigger goes high level for 10 microseconds)
39 digitalWrite(trigger, HIGH);
40 delayMicroseconds(10);
41 digitalWrite(trigger, LOW);
42
43 // Reading echo, via pulseIn, which returns the duration of the impuse (in microseconds)
44 // The acquired data is then divided by 2 (forward and backward)
45 tempo = pulseIn(echo, HIGH) / 2;
46 // Computation of distance in centimetres
47 space = tempo * c;
48
49 // space is displayed in the serial monitor ([Ctrl] + [Shift] + M)
50 // approximated to the first decimal place
51 Serial.println("Distanza = " + String(space, 1) + " cm");
52
53 // If the distance is less than one metre
54 if (space < beep_start) {
55 // Emits sounds at intervals proportional to distance (1 m = 400 ms)
56 tone(beep, 1000);
57 delay(40);
58 // Below min_distance cm it emits a continuous sound
59 if (space > min_distance) {
60 noTone(beep);
61 delay(space * 4);
62 }
63 }
64 // Waits 50 milliseconds before another measurement
65 delay(50);
66}
<pre><code>