0% found this document useful (0 votes)
2 views

How HC-SR04 Ultrasonic Sensor Works & Interface It With Arduino

about ultasonic sensor
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

How HC-SR04 Ultrasonic Sensor Works & Interface It With Arduino

about ultasonic sensor
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

How HC-SR04 Ultrasonic

Sensor Works & Interface


It With Arduino

The HC-SR04 ultrasonic sensor uses sonar to determine the distance to an


object. This sensor reads from 2cm to 400cm (0.8inch to 157inch) with an
accuracy of 0.3cm (0.1inches), which is good for most hobbyist projects. In
addition, this particular module comes with ultrasonic transmitter and receiver
modules.

HC-SR04 Hardware Overview


An HC-SR04 ultrasonic distance sensor actually consists of two ultrasonic
transducers.

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.

This sensor provides excellent non-contact range detection between 2 cm to 400


cm (~13 feet) with an accuracy of 3 mm.

Since it operates on 5 volts, it can be connected directly to an Arduino or any


other 5V logic microcontroller.
Features
Here’s a list of some of the HC-SR04 ultrasonic sensor features and specs—
for more information, you should consult the sensor’s datasheet:

■ Power Supply :+5V DC


■ Quiescent Current : <2mA
■ Working Current: 15mA
■ Effectual Angle: <15°
■ Ranging Distance : 2cm – 400 cm/1″ – 13ft
■ Resolution : 0.3 cm
■ Measuring Angle: 30 degree
■ Trigger Input Pulse width: 10uS TTL pulse
■ Echo Output Signal: TTL pulse proportional to the distance range
■ Dimension: 45mm x 20mm x 15mm

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.

GND is the ground pin. Connect it to the ground of the Arduino.

How Does HC-SR04 Ultrasonic Distance


Sensor Work?
It all starts when the trigger pin is set HIGH for 10µs. In response, the sensor
transmits an ultrasonic burst of eight pulses at 40 kHz. This 8-pulse pattern is
specially designed so that the receiver can distinguish the transmitted pulses
from ambient ultrasonic noise.

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.

Let us take an example to make it more clear. Suppose we have an object in


front of the sensor at an unknown distance and we receive a pulse of 500µs
width on the echo pin. Now let’s calculate how far the object is from the sensor.
For this we will use the below equation.

Distance = Speed x Time


Here we have the value of time i.e. 500 µs and we know the speed. Of course it’s
the speed of sound! It is 340 m/s. To calculate the distance we need to convert
the speed of sound into cm/µs. It is 0.034 cm/μs. With that information we can
now calculate the distance!

Distance = 0.034 cm/µs x 500 µs

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 = (0.034 cm/µs x 500 µs) / 2

Distance = 8.5 cm

Now we know that the object is 8.5 cm away from the sensor.

HC-SR04 Sensor with Arduino UNO


Now that we have a complete understanding of how the HC-SR04 ultrasonic
sensor works we can start connecting it to our Arduino!

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

Upload the following code to your Arduino IDE.

int trigPin = 11; // Trigger


int echoPin = 12; // Echo
long duration, cm, inches;

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);

// Convert the time into a distance


cm = (duration/2) / 29.1; // Divide by 29.1 or multiply by 0.0343
inches = (duration/2) / 74; // Divide by 74 or multiply by 0.0135

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.

You might also like