Object detection using Ultrasonic Sensor & Arduino
Updated: Sep 10, 2020
#Ultrasonic Sensor is used for the precise detection of objects. The ultrasonic transceivers, that are the #transmitters and #receivers, use the concept of converting electrical energy into ultrasonic sound.
The ultrasonic sensor emits sound waves at a frequency above human hearing and calculates the distance of any object or obstacle in its path. The distance is calculated by counting the time of flight, i.e., the time delay between transmitting the Ultrasonic signal and receiving it.
1. Ultrasonic sensor:
HC-SR04 model with an operating voltage of 5V,
measures distance between 2cm-40cm
accuracy of 3mm.
measuring angle = 15 degrees
operating frequency = 440Hz
Operating current < 15mA
4 pins: VCC, TRIG, ECHO, GND
A #buzzer is an audio signaling device, it can be mechanical, electromechanical, or piezoelectric.
A boundary is set within which the object can be detected when the object is within this boundary, the buzzer starts ringing, else it does not.
3. Arduino Uno R3
The #Arduino consists of digital and analog input/output pins that are interfaced with the ultrasonic sensor (used to send and receive data). the Arduino Uno IDE consists of a Serial Monitor that displays the output.
4. Jumper wires
They are used for making circuit connections.
Circuit connections are made upon this.
The 4 pins of the Ultrasonic Sensor are:
1. Vcc connected to the 5V or Arduino.
2. The trigger is connected to pin 9 of the Arduino- which is the input pin and is kept high for 10 microseconds for the initialization of measurement by sending Ultrasonic waves.
3. The echo is connected to the pin 10 of the Arduino- Output pin which will be high for the time period which is equal to the time taken for the Ultrasonic wave to return to the sensor.
4. The ground is connected to the ground of the breadboard.
1. The positive of the buzzer is connected to pin 11 of the Arduino
2. Ground connected to the ground of breadboard.
Ground: The ground of the breadboard is connected to the ground (GND) of Arduino, thereby making that line (that includes the ground of Ultrasonic Sensor and Buzzer as the common ground).
3 variables of constant integer type are used:
1. trigger pin (connected to pin 9)
2. echo (connected to pin 10)
3. buzzer (connected to pin 11)
2 other variables:
1. duration- of long type
2. distance- of integer type
void setup ()
1. pin mode of trigger pin set as output
2. pin mode of echo pin set as input
3. buzzer set as output
4. serial communication begins.
1. triggering is set as low whenever the loop starts
2. delay of 2 microseconds is given before making the trigger pin high
3. triggering is made high for 10 microseconds
4. triggering is set again as low
5. the pulse (or echo) is counted using the ‘pulseIn’ function and the arguments are echo and high; this calculates the duration from which we can calculate the distance
6. distance = speed of sound x time
7. duration is twice the distance between the object and the sensor; thus we have to divide by 2.
0.034 is the speed of sound in microseconds
8. the distance variable is kept in the if condition to set a boundary of 10 units.
9. Whenever the object is within this boundary, the buzzer gets high and rings
10. If the object is outside the boundary, the buzzer remains low and does not ring.
11. We now print the distance
12. The distance of the object from the sensor is shown on the Serial Monitor
How is the Object distance calculate?
The Ultrasonic Sensor consists of a Transmitter and a Receiver. The Transmitter emits the Ultrasonic waves (signal) and the time is started when the signal is emitted. These waves spread in the air and strike the object/ obstacle in its path, after which they are reflected and returned immediately to the Receiver. once they are being returned, the timing stops. this gives us the total time of flight. We calculate the distance of the object through time.
Ultrasonic Sensor works on the basic formula of Distance = Speed x Time
The time of flight includes the distance traveled back and forth, thus we need to divide the time of flight by 2 while calculating the distance
Thus, D = S X T/2
T = time taken for the waves to hit the object and reflect. So we have divided the Time by 2 as the signal is traveling two times the distance.
S = Speed of Sound = 0.034 m/us (microseconds).
In this code, the boundary distance is set to 10 units. Whenever the object is a distance that is less than or equal to 10, the buzzer gets high and starts ringing.
Circuit Before simulation (OFF)
After simulation (ON)
Here are some applications for Object Detection Using Ultrasonic Sensor:
Monitoring and detecting the fill level.
Vehicle detection for parking lots.
In production lines.
Learn Electronics. (Sept 5, 2020). "Object detection using Ultrasonic sensor & Arduino". https://www.youtube.com/watch?v=HqiOpquTRW4