This article “Interfacing of Ultrasonic Sensor with Arduino” is updated in 2020.

Today, In this tutorial we will learn about the Ultrasonic Sensor Arduino Code and How you can Interface Ultrasonic Sensor (HCSR04) with Arduino. A few days ago, I have posted a complete tutorial on What is Arduino and How you can learn Arduino, and later I have posted different projects on Arduino For Example Line Following Robot, Arduino Based Calculator, Arduino Based Radar System. Those posts were very good and I got the good response, so I thought today let’s interface Ultrasonic Sensor to the Arduino.

So, today I am gonna show you the complete guide on How to Interface Ultrasonic Sensor (HCSR04) with Arduino in a few easy steps.

How to Interface Ultrasonic Sensor with Arduino:

What is Ultrasonic Sensor

An Ultrasonic Sensor is an Electronic device which is used to measure a distance of an object using Sound Waves. Its working principle is very simple; it sends out a sound wave at very high-frequency range (Ultrasonic range of > 40KHz) and waits for it to bounce back from the object. As shown in the figure below. Then, the time duration between transmission of the sound wave and receiving of the sound wave is used to calculate the distance.

The formula for the Distance is = (Speed of sound wave * Time delay) / 2

Here you can see we have divided the distance formula by 2 this is because the sound waves travel from the transmitter and back to the receiver which doubles the actual distance.

Ultrasonic Sensor HCSR04 is Consist of:

Ultrasonic Sensor HCSR04 is a simple electronic sensor which basically is used for measuring the distance between the sensor itself and any obstacle in front of it. The sensor has a sender and a receiver on it.



This sensor consists of four pins, which are:

Vcc (+5V) : Ultrasonic Sensor requires +5V to work perfectly.

Ultrasonic Sensor requires +5V to work perfectly. Trig (Trigger): This is the important pin because we have to provide trigger Pin after which this ultrasonic sensor emits ultrasonic sound waves.

This is the important pin because we have to provide trigger Pin after which this ultrasonic sensor emits ultrasonic sound waves. Echo: This is also very important pin, it is used When Ultrasonic waves emitted from the transmitter, hit some object then they get bounced back and then those waves are received by the receiver and at that moment this echo Pin goes HIGH.

This is also very important pin, it is used When Ultrasonic waves emitted from the transmitter, hit some object then they get bounced back and then those waves are received by the receiver and at that moment this echo Pin goes HIGH. GND: The GND is used to complete the circuit, we need to provide ground to HC-SR04 Ultrasonic Sensor.

Interfacing of Ultrasonic Sensor With Arduino:

Now As we have Covered and understood the working of the Ultrasonic sensor, so we have some knowledge about what we need to do in order to get the values from the Ultrasonic sensor. Let’s now have a look at Interfacing of Ultrasonic Sensor with Arduino.

First of all, we need to generate an electronic signal of 10Us (microsecond) and then send it over to trigger pin of Ultrasonic Sensor. After sending the trigger pin we then need to read the echo pin and wait for it to get HIGH.

For Example: if the object is 6 cm away from the sensor, and the speed of the sound is 0.034 cm/µs the sound wave will need to travel about 176 microseconds. But what you will get from the Echo pin will be double that number because the sound wave needs to travel forward and bounce backward.

So in order to get the distance in cm, we need to multiply the received travel time value from the echo pin by 0.034 and divide it by 2. This is illustrated in the figure below.

Once the ECHO pin goes HIGH then we need to count the time for how long it remained HIGH. On the basis of this time, we are going to calculate the distance of the object from the ultrasonic sensor.

Components required for the Connection of Ultrasonic Sensor with Arduino:

Ultrasonic Sensor HCSR04

Arduino UNO Board

Breadboard and Jump Wires

Circuit Diagram of Connection of Ultrasonic Sensor with Arduino:

The connection is very easy, do not get confused and if you feel any query feel free to comment below. I will make sure to explain it as soon as possible.

Ultrasonic Sensor Arduino Code:

/* * Ultrasonic Sensor HC-SR04 and Arduino Tutorial * * Crated by Sohail Anwar, * www.studentsheart.com * */ // defines pins numbers const int trigPin = 12; const int echoPin = 11; // 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); */ visit:www.Studentsheart.com/ }

Note: If you want to display the results from the HCSR04 Ultrasonic Sensor on an LCD you can use the following source code:

Also Read: How to Interface IR Sensor with Arduino Step by Step GUIDE

Circuit Diagram for Interfacing Ultrasonic Sensor with LCD:

Ultrasonic Sensor Arduino Code with LCD:

/* * Ultrasonic Sensor HC-SR04 and Arduino Tutorial * * Crated by Sohail Anwar, * www.studentsheart.com * */ // defines pins numbers #define trigPin 12 #define echoPin 13 #include <LiquidCrystal.h> // initialize the library with the numbers of the interface pins LiquidCrystal lcd(7, 6, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); lcd.setCursor(1,0); lcd.print("Distance="); } void loop() { long duration, distance; digitalWrite(trigPin, LOW); // Added this line delayMicroseconds(2); // Added this line digitalWrite(trigPin, HIGH); delayMicroseconds(10); // Added this line digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; // conversion in cm lcd.setCursor(11,0); lcd.print(distance); //output on lcd in centimeter lcd.setCursor(14,0); lcd.print("cm"); }

Once you’ve uploaded the code, and if you do not know how to upload the code visit my this post Learn Arduino, the board will begin to transmit data to the computer. You will see Tx LED on the Arduino blinking each time it transmits a data it means the data is transmitted from the Ultrasonic Sensor.

Now click on Serial Monitor and see the values of Distance measured by Ultrasonic Sensor HCSR04, you’ll see the distance is displayed. This is all about How to Interface Ultrasonic Sensor (HCSR04) with Arduino Easy Steps.

If you need any further help please feel free to leave a comment below. And do not forget to share this post on your Facebook let your friends know your work.