Beginners Guide to Ultrasonic sensor and how to use it with Arduino

Hey guys! Today we are going to learn how to use or how to interface an Ultrasonic sensor with Arduino. So here is your Beginners Guide To Ultrasonic sensor.

These all are the same kind of ultrasonic sensor called HCSR04 and distance up-to 400 cm can be measured using this sensor.

HCSRO4 Ultrasonic Sensor
HCSRO4 Ultrasonic Sensor

Learn More about HCSR04: LINK

So an ultrasonic sensor, also known as ping sensor is commonly used to measure the distance of an object from its surface by emitting and receiving sound waves. The transmitter transmits the sound waves whereas the Receiver receives these transmitted sound waves reflected back from the object. It kinda works like a radar about which you must have read in your high school books.


Ultrasonic sensor
Ultrasonic sensor


Watch the video given below for step by step explanation:


Pinouts of HCSRO4 ultrasonic sensor:

Pinouts of HCSRO4 Ultrasonic Sensor
Pinouts of HCSRO4 Ultrasonic Sensor



1) VCC 




VCC and GND: Sensor is powered through VCC and Ground.

TRIGGER Pin: Trig pin is used to initiate the transmission of the sound wave whereas-

ECHO Pin: Echo pin is used to get the total time taken by the wave to get back to the sensor.


Read Also: Beginners Guide to IR Sensors before this Beginners guide to Ultrasonic Sensor.



Working of HCSR04 Ultrasonic Sensor:

Now let’s see how the distance is calculated using HCSR04 Ultrasonic Sensor.

How ultrasonic Sensor works
How ultrasonic Sensor works


As soon as Trig pin is set High it sends pulses of sound waves and at the same time Echo pin becomes high. And as soon as these sound waves are received by the receiver, Echo pin becomes low. Now we have to just measure the time interval for which echo pin was high. Now multiply this time by the speed of sound and then divide by 2 to get the oneway distance.

i.e, Time= For which Echo pin remains high

      Distance = Time * Speed of Sound/2  

Let’s understand this once again by an example:
Let’s suppose there is an object in front of the sensor at 10 cm distance. Wave is transmitted by the transmitter, hits the object and then received by the receiver.


This slideshow requires JavaScript.


The total time between these two events is calculated using echo pin as mentioned earlier. Now using this formula oneway distance is calculated.

Formula to calculate oneway distance
Formula to calculate oneway distance


How to use Ultrasonic Sensor with Arduino:

Let’s interface ultrasonic sensor with Arduino now. I am using Arduino UNO here but you can use any Arduino board. Make connections as shown in this circuit.

HCSR04 Ultrsonic sensor connections with Arduino
HCSR04 Ultrasonic sensor connections with Arduino

1) Connect Vcc and Gnd of the ultrasonic sensor to 5 v and GND pin of Arduino respectively.

2) Connect trig and echo pin to any digital input op pins. I am connecting trig pin to 6 digital pin and echo to 7 digital pin respectively.


Program to measure distance using HCSR04 Ultrasonic sensor:

const int trig = 6;
const int echo = 7;

long totaltime;
int distance;

void setup() {
pinMode(trig, OUTPUT); 
pinMode(echo, INPUT);


void loop() {

digitalWrite(trig, LOW);

digitalWrite(trig, HIGH);
digitalWrite(trig, LOW);

totaltime = pulseIn(echo, HIGH);

distance= totaltime*0.034/2;

Serial.print("Distance: ");



Code Explanation:

The code for measuring distance using ultrasonic sensor is very simple. First trig pin is set high for 10 us than time for which echo pin remains high is calculated using puls-in function and finally, distance in centimeters is calculated using this formula:


distance= totaltime*0.034/2;


Now upload the code to Arduino and then Open serial monitor. Here you can see the calculated distance of the object from the surface of the ultrasonic sensor. Let’s move the object and see how accurate is the measurement.


Projects using HCSR04 Ultrasonic Sensor

Now apart from measuring the distance you can make many cool projects using an ultrasonic sensor like:

1)Arduino based blind stick made by Circuit digest:

2) Arduino Radar system by How to Mechatronics

3) Smart Dustbin by Indian life hacker


Note: Please remember that none of these project videos are made by me and they belong to the creators only.

So this was your Beginners Guide To Ultrasonic sensor. Thank you for reading!

No Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: