How HC-SR04 Ultrasonic Sensor Works & Interface It With Arduino
How HC-SR04 Ultrasonic Sensor Works & Interface It With Arduino
One acts as a transmitter that converts the electrical signal into 40 KHz ultrasonic
sound pulses. The other acts as a receiver and listens for the transmitted pulses.
When the receiver receives these pulses, it produces an output pulse whose
width is proportional to the distance of the object in front.
What is Ultrasound?
Ultrasound is a high-pitched sound wave whose frequency exceeds the audible
range of human hearing.
Humans can hear sound waves that vibrate in the range of about 20 times a
second (a deep rumbling noise) to 20,000 times a second (a high-pitched
whistle). However, ultrasound has a frequency of more than 20,000 Hz and is
therefore inaudible to humans.
HC-SR04 Ultrasonic Sensor Pinout
Let’s take a look at its pinout.
VCC supplies power to the HC-SR04 ultrasonic sensor. You can connect it to the
5V output from your Arduino.
Trig (Trigger) pin is used to trigger ultrasonic sound pulses. By setting this pin to
HIGH for 10µs, the sensor initiates an ultrasonic burst.
Echo pin goes high when the ultrasonic burst is transmitted and remains high until
the sensor receives an echo, after which it goes low. By measuring the time the
Echo pin stays high, the distance can be calculated.
These eight ultrasonic pulses travel through the air away from the transmitter.
Meanwhile the echo pin goes HIGH to initiate the echo-back signal.
If those pulses are not reflected back, the echo signal times out and goes low
after 38ms (38 milliseconds). Thus a pulse of 38ms indicates no obstruction
within the range of the sensor.
If those pulses are reflected back, the echo pin goes low as soon as the signal is
received. This generates a pulse on the echo pin whose width varies from 150 µs
to 25 ms depending on the time taken to receive the signal.
Calculating the Distance
The width of the received pulse is used to calculate the distance from the
reflected object. This can be worked out using the simple distance-speed-time
equation we learned in high school. An easy way to remember the equation is to
put the letters in a triangle.
But we’re not done yet! Remember that the echo pulse indicates the time it takes
for the signal to be sent and reflected back. So to get the distance, you have to
divide your result by two.
Distance = 8.5 cm
Now we know that the object is 8.5 cm away from the sensor.
Connecting the HC-SR04 to Arduino is very easy. Start by placing the sensor on
your breadboard. Connect the VCC pin to the 5V pin on the Arduino and the
GND pin to the ground pin. Now connect the trig and echo pins to digital pins #11
and #12 respectively.
When you are done you should have something that looks similar to the
illustration shown below.
Wiring HC-SR04 Ultrasonic Sensor to Arduino UNO – Normal Mode
void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// The sensor is triggered by a HIGH pulse of 10 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the signal from the sensor: a HIGH pulse whose
// duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(250);
}
Demonstration
Upload the code to your Arduino board. Then, open the Serial Monitor at a
baud rate of 115200.
The distance to the nearest object is printed in the Serial Monitor window.