INTERFACING OF ARDUINO WITH ULTRASONIC SENSOR
In this session, ARDUINO interfacing with Ultrasonic Sensor is discussed. Ultrasonic Sensor is used to calculate the distances of the obstacle. It works on the principle of SONAR.
Ultrasonic Sensor senses the distance of an object by sending the ultrasonic sound at 40,000 Hz, which travels through the air and if there is an object or obstacle on its path it will reflect or bounce back to the receiver of the module.
- The common name of Ultrasonic Sensor is HC-SR04.
- Operating Voltage of this sensor is +5V.
- It can measure distance from 2cm to 80cm.
- It contains 4 pins – VCC, Ground, Echo, Trigger.
- Trigger pin is the input pin and it sends the ultrasonic waves.
- Echo pin is the output pin and it receives the bounced or reflected waves
FORMULA TO CALCULATE DISTANCE
Speed of sound: v=340 m/s, v=0.034cm/μ s
Time = Distance / Speed:
t = s/v = 10 / 0.034 = 294 μ s
Distance: s = t * 0.034 / 2
CONNECTING THE SENSOR WITH ARDUINO
This sensor connection to the ARDUINO is discussed in the following steps:
- There will be 4 pins present on the ultrasonic sensor.
- Initially the VCC pin of the Ultrasonic Sensor is connected to the VCC(5V) pin of the ARDUINO by using the male to female wire.
- Then the GROUND pin of the Ultrasonic Sensor is connected to the GND of the ARDUINO by using the male to female wire.
- Finally, the TRIGGER and ECHO pins of the Ultrasonic Sensor is connected to the Digital pins of the ARDUINO, excepting the 0 and 1 pins since they are used for transmission and receiving purpose.
-
Now the power supply is given to the ARDUINO
for the functioning of the Ultrasonic Sensor and the ARDUINO Development Board.
The complete interfacing of the Ultrasonic Sensor with ARDUINO is done perfectly, the only left over task is to type the program and dump the program into the ARDUINO Development Board.
ARDUINO PROGRAM OF HC-SR04
// defines pins numbers
const int trigPin = 2;
const int echoPin = 4;
// defines variables
long duration;
int distance;
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
}
void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}
After the complete writing of the above program in ARDUINO IDE , you can dump the code into the ARDUINO board.
FUNCTIONING OF HC-SR04
After the successful dumping of code into the ARDUINO board, the output values of the Ultrasonic Sensor can be visible in the serial monitor in the computer screen. When a obstacle is detected in the path of Ultrasonic Sensor , then the distance of the obstacle from the sensor is displayed on the serial monitor in the ARDUINO IDE software window on the computer screen.
EXPLAINATION OF THE PROGRAM
Initially the TRIGGER pin and the ECHO pin is connected to the pins 2 and 4 respectively in the ARDUINO BOARD , this means the ARDUINO board gets the input and output values from the 2 and 4 pins connection of the TRIGGER AND ECHO pins.
- The Variables duration and distance are declared.
- Sets the TRIGGER Pin as an Output and ECHO Pin as an Input.
- Then establishes the serial communication with baud rate of 9600
- Then in the loop, TRIGGER pin is kept off (low) for 2 microseconds.
- Now the TRIGGER pin is kept on (high) for 10 microseconds and then kept off (low).
- Then the ECHO pin is kept on (high) to read the time taken by the wave to return back and is stored in duration.
- Now finally, the distance is calculated by using the appropriate formulae.
- And the distance is printed on the Serial Monitor.
CONCLUSION
The above discussed information about the Ultrasonic Sensor is all about the interfacing and functioning of the Sensor with ARDUINO Development Board. In this way the HC-SR04 component can be connected to the ARDUINO and can be used for wide range of applications in the Autonomous Robotic Projects.