Interfacing Ultrasonic Sensor with Arduino UNO

An ultrasonic sensor measures the distance to the object using ultrasonic sound waves. These sound waves are capable to generate and sense ultrasound energy. They are divided into three categories,

  1. Transmitters
  2. Receivers
  3. Transceivers

Transmitters convert the electrical signals to ultrasound and receivers convert ultrasound signals to the electrical signals, whereas transceivers can both transmit and receive signals.  The Ultrasonic sensor is also known as HC-SR04

What is Ultrasound?

Ultrasound is high pitch sound waves with frequencies higher than the audible limit of human hearing. Ultrasound has a frequency over 20,000Hz and is therefore inaudible to human beings. Human ears are capable of hearing the sound waves in between the range of  20Hz to 20,000Hz. 


In order to understand the principles of precision and accuracy, the ultrasonic sensor is the best device to take help from. The functionality it is eligible to perform is to measure the distance from the point where the obstacle comes in-front of it. The measured distance will then be displayed on the serial monitor.   


The industrial applications of HC-SR04 are advancing day by day. They are being used as parking sensors in cars, water level sensors for water tank management etc.  

HC-SR04 Pin Out

Pinout diagram

Vcc labelled as 5v is the power supply for HC-SR04 ultrasonic sensor.

Trigger pin triggers the Ultrasonic sound pulses.

Echo pin generates a pulse as the reflected signal is received. The length of the pulse is in proportion to the time required to detect the transmitted signal.

Connect Gnd of HC-SR04 to the Gnd of Arduino UNO.


Requirements for interfacing ultrasonic sensor with Arduino UNO are are as follows,

  1. HC-SR04
  2. Arduino UNO
  3. Jumper wires


Interfacing HC-SR04 with Arduino
					/ defining the pins
const int trigPin = 2;
const int echoPin = 3;
// defining 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);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
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: ");

NOTE: When HC-SR04 is interfaced with the Arduino UNO, it may not function properly. If you run into such a problem, then the issues is with the voltage connectivity. Connect Vcc to 5v because the ultrasonic sensor does not function properly when connected to 3.3v and hence it is required to connect it to 5v for accurate results. 



Upload the code and open serial monitor to view results. To check for accurate results, use a centimeter scale and observe the readings. Put an obstacle in-front of the sensor after which then observe and measure the readings. 


Leave a Reply

Your email address will not be published.