In this tutorial, we will illustrate the installation and the use of simple, low-cost parking assist car sensors. We will use two HC-SR04 ultrasonic sensors and four Piezo buzzers to progressively alert the driver about the proximity of nearby cars behind the driver’s car while parking with an acoustic beeping sound. We will also solve practical problems like waterproofing and deploying of wires within a car’s cabin.
Hardware
Software
Tools
Let’s start by preparing the electronics shown below so we can test the system before actually installing car sensors in the car.
Figure 1: Required hardware components – Arduino, plastic case, ultrasonic sensors, Piezo buzzers, wires, and a power adapter
In this project, we will only be using two sensors for the rear: one on the left side of the bumper, and the other on the right side of the bumper. Assistive alert systems in most new cars have up to six sensors for more precise positioning.
HC-SR04 sensors show four pins each:
For a more detailed explanation of the functions of these modules please visit this previous tutorial: Using Arduino with Parts and Sensors – Ultrasonic Sensor
Figure 2 below shows a diagram on how to wire the car sensors and buzzers to the Arduino.
Figure 2: Wire diagram of the Arduino, ultrasonic sensors, and Piezo buzzers
Let’s start writing a program. In general, for any microcontroller firmware we can find the following four elements:
Unlike in software development, when programming microcontrollers we normally have some limitations as to how large our code can be. My Arduino Uno comes with an Atmega328, which has 32 KB memory. For this particular application this is more than enough.
1 2 3 4 5 6 |
#define trigPin1 13 #define echoPin1 12 #define buzzerPin1 6 #define trigPin2 11 #define echoPin2 10 #define buzzerPin2 7 |
1 2 3 4 5 6 7 8 9 10 11 |
void setup() { Serial.begin (9600); pinMode(trigPin1, OUTPUT); pinMode(echoPin1, INPUT); pinMode(buzzerPin1, OUTPUT); pinMode(trigPin2, OUTPUT); pinMode(echoPin2, INPUT); pinMode(buzzerPin2, OUTPUT); pinMode(2, OUTPUT); // We will use the pin 2 as ground. We need to make sure it digitalWrite(2,LOW); // is in LOW position so it works as a ground. } |
The functions pinMode() and digitalWrite() are convenient ways of dealing with the microcontroller’s pins. We will use the pinMode() function to set the direction of the specified pin(s). The direction can be either input or output. Once you set the direction, the pin will only work in that direction. We will use digitalWrite() function to set the specified digital pin to HIGH or LOW. Here, I set pin 2 as another extra ground, which I needed in order to connect the GND pin of one of the devices.
1 2 3 4 |
void loop() { calculateDistance(echoPin1, trigPin1,buzzerPin1); //Get the distance for the left calculateDistance(echoPin2, trigPin2,buzzerPin2); //Get the distance for the right } |
For the sake of simplicity, I have defined a function (calculateDistance (echo, trigger, buzzer) that will be used in one of the sensors first, and in the other sensor second.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
//********************************************************************************** //***Function to measure time of return of an ultrasound echo.************ //***Set up for distances shorter than 2 m, which is enough for parking** //***Beyond that HC-SR04 is not reliable *********************************** //********************************************************************************** void calculateDistance(int echo, int trigger, int buzzer){ long duration, distance; digitalWrite(trigger, LOW); // Set the trigger pin of the HC-SR04 to LOW delayMicroseconds(2); // Wait a little to stabilize the sensor digitalWrite(trigger, HIGH); // Set the trigger HIGH and send a pulse delayMicroseconds(10); // Wait for 10us until the wave comes back digitalWrite(trigger, LOW); // Set the trigger pin of the HC-SR04 to LOW again duration = pulseIn(echo, HIGH); // Arduino’s built in function pulseIn reads a pulse // (either HIGH or LOW) on a pin. Returns the length of // the pulse in microseconds or 0 if no complete pulse // was received within the timeout. distance = (duration/2) / 29.1; // The speed of sound is 340 m/s or 29 microseconds // per centimeter. // The ping travels out and back, so to find the distance // if the object we take half of the distance travelled. if (distance >= 200 || distance <= 0){ Serial.println("Out of range"); // Use some feedback in the serial port for // debugging } else { tone(buzzer,2000,25); // Arduino’s built in function tone generates a square // wave of the specified frequency (2000Hz) for the // indication time (25 ms) delay(distance*10); // The delay gives feedback beeping faster the closer // we get to an object Serial.print(distance); // Use some feedback in the serial port for // debugging Serial.println(" cm"); } } |
We first activate a pin (trigger) because it will take a while to send us back information about another pin (echo). With this time interval we can make some assumptions and calculate a distance (the speed of sound is 340 m/s or 29 microseconds per centimeter). This distance will serve as a parameter to establish how often to emit a beep of 2000 Hz (I found this by trial and error, feel free to change it to your preferred pitch).
I used several built-in functions, such as tone (pin, frequency, duration), digitalWrite (pin), delayMicroseconds(duration), and pulseIn(pin, value).
Find a waterproof area in the rear body of your car. Sensors will be installed here. In my car I had several options:
If you were to place the car sensors either inside the tail light covers or on the rear bumpers, you must drill holes to allow ultrasound waves to come in and out. Unfortunately, these holes would also allow water to come in (rain, splashes, etc.) unless they were very well made. Waterproofing them with sealant is not an option because the ultrasound waves will not make it out. In addition, you will have the added difficulty of calibrating the position of the holes with respect to the sensor in order to avoid diffraction effects.
Figure 3: The problem of diffraction when housing ultrasound sensors. / ©bta304
To learn more about diffraction waves, please click here.
So, I chose to place the car sensors near the license plate because the gap underneath the trunk door/handle is large enough, and it spares me the trouble of drilling holes. All I need to do is slightly adjust the orientation of the sensors to aim towards the corners of the car.
Figure 4: Finding the right location for the sensors
Find a place for the Arduino and the Piezo buzzers. For this step we will need to un-mount the car door panels and find a safe, empty place to attach the electronics.
Let the fun begin! The inside panels reveal a whole new dimension of your car. What we have there is a lot of space where we can attach many devices (maybe for future posts!). There are also a number of internal wires that supply power to different elements of the car related to safety. Make sure you don’t touch any important wires.
YOU MUST BE VERY CAREFUL WITH THE INTERNAL PARTS OF THE CAR.
We will connect the ultrasonic sensors from the outside of the car to the Arduino through the holes that hold the license plate. I had to make those holes a little bit larger to have all eight wires pass through and still leave room for the supporting screws.
The Piezo buzzers were arranged with simple two-sided adhesive tape.
Figure 5: Assembly of the HC-SR04 on the right side of the license plate
Figure 6: Assembly of one of the Piezo buzzers with two-sided tape
Figure 7: Locations of the modules on trunk door (inside view)
Figure 8: Locations of the modules (rear view)
Altogether, we will need about 6 meters of wire for the sensors and the buzzers in order for the system to operate comfortably. It is important to color code the wires to prevent confusion in the future.
Once the hardware is in place we need to connect all the wires. The sensors need eight cables (two echoes, two triggers, two grounds and two VCCs) that need to go through the holes behind the license plate. Let’s verify the connections:
The order of the VCCs and the GNDs is irrelevant as long as they are all connected. My Arduino Uno exposes up to three grounds and I am using Vin pin as a power supply for one of the sensors, supplying the other sensor from the 5V pin.
Other Arduino versions are more or less limited in pins (i.e. Arduino Micro only exposes two grounds), so I decided to make an extra ground by setting the pin 2 to LOW.
Figure 9: Connecting the ultrasound modules and the power
Figure 10: Connecting the modules, the power and the buzzers
The power supply in automobiles is tricky. Although the car battery provides 12 DC Volts (the voltage regulator may overheat and damage the Arduino board if you are using more than 12V), it is connected to an alternator. When starting up the engine the alternator can create huge current peaks and fry any electronic device connected to it. It is called load dump. You can read more about it here.
For this reason, it is best to use an intermediate protective circuit between your Arduino and the car’s power supply. One option is the DIY assembly of a voltage regulator, but its design surpasses the scope of this article, so I opted to recycle an old phone adapter.
I used a USB female adapter cable. I had to replace the end of the USB adapter and attach it to the female adapter. Alternatively, I could have removed the Arduino’s female pin headers and attached all wires.
The question here is: Which one of the four wires goes where? Please see the image below.
Figure 11: USB type B. When supplying power through the USB only two wires are needed.
In my Arduino Uno the USB connection is type B. Other models have other types of USB connections, so you will have to check yours and adjust to your needs.
Finally, we need to find a power source for our adapter. There are several options:
I was lucky enough to find a connector in between all the wires that ran behind the plastic panel. I checked the voltage and …Voila!
Figure 12: Finding the power and polarity in a stray connector within my car
In this tutorial, we developed a fairly simple, cost-effective Arduino-based assistive parking system with car sensors that alert a driver when they get too close to the cars behind them (rear side). Using two ultrasonic proximity sensors and two Piezo buzzers, the system warns the driver with acoustic sounds, with the frequency between the sounds indicating the distance from an obstacle.
We created a real-life application using Arduino and explored the boundaries between the proof-of-concept prototype, the user experience prototype and the functional prototype. In a future tutorial, we may expand on the concepts we discussed here today to build an improved version of the assistive parking system with better features and functionalities. What do you think?
If you have any comments or questions, please leave them for us at Google +. Follow us there; we will be posting the next tutorial soon.