HC-SR04 Ultrasonic Sensor Guide with Arduino Interfacing

Overview of ultrasonic sensor

The ultrasonic sensor works on the principle of SONAR and RADAR system which is used to determine the distance to an object.

An ultrasonic sensor generates high-frequency sound (ultrasound) waves. When this ultrasound hits the object, it reflects as echo which is sensed by the receiver as shown in below figure.

The following picture shows How ultrasonic sensor works
Ultrasonic Sensor Working Principle


By measuring the time required for the echo to reach to the receiver, we can calculate the distance. This is the basic working principle of Ultrasonic module to measure distance.


Ultrasonic HC-SR-04 Module

The picture shows the HC-SR04 ultrasonic sensor. Module
Ultrasonic Module


HC-SR-04 has an ultrasonic transmitter, receiver and control circuit.

In the ultrasonic module HCSR04, we have to give trigger pulse, so that it will generate ultrasound of frequency 40 kHz. After generating ultrasound i.e. 8 pulses of 40 kHz, it makes echo pin high. Echo pin remains high until it does not get the echo sound back. So the width of echo pin will be the time for sound to travel to the object and return back. Once we get the time we can calculate distance, as we know the speed of sound.

Ultrasonic Sensor HC-SR04 can measure up to range from 2 cm - 400 cm.


HC-SR04 Pin Description

This picture shows Front and back side of Ultrasonic Module
HC-SR04 Pin Diagram


VCC: +5 V supply

TRIG: Trigger input of sensor. Microcontroller applies 10 us trigger pulse to the HC-SR04 ultrasonic module.

ECHO: Echo output of sensor. Microcontroller reads/monitors this pin to detect the obstacle or to find the distance.

GND: Ground


Ultrasonic Sensor HC-SR04 Working Principle

This picture shows Timing Diagram of HC-SR04 Ultrasonic Module
HC-SR04 Ultrasonic Module Timing Diagram


  1. We need to transmit trigger pulse of at least 10 us to the HC-SR04 Trig Pin.
  2. Then the HC-SR04 automatically sends Eight 40 kHz sound wave and wait for rising edge output at Echo pin.
  3. When the rising edge capture occurs at Echo pin, start the Timer and wait for falling edge on Echo pin.
  4. As soon as the falling edge is captured at the Echo pin, read the count of the Timer. This time count is the time required by the sensor to detect an object and return back from an object.


How to calculate distance for Ultrasonic HC-SR04 Module?

We know that,

\textbf{Distance = Speed x Time}

The speed of sound waves is 343 m/s.


\textbf{Total Distance }= \frac{\textbf{343 x } \textbf{Time of High(Echo) Pulse}}{\textbf{2}}

Total distance is divided by 2 because signal travels from HC-SR04 to object and returns to the module HC-SR-04.


Alternate options for HC-SR04 Ultrasonic Sensor 

  • Maxbotix Ultrasonic Sensors: These sensors offer a wider range of detection and higher accuracy compared to the HC-SR04. They are available in different models with varying ranges and beam patterns.
  • Sharp Infrared Sensors: These sensors use infrared light to measure distances and are available in a range of detection distances. They are suitable for use in low light conditions and have a faster response time compared to ultrasonic sensors.
  • Lidar Sensors: These sensors use laser light to measure distances and provide high accuracy and precision. They are commonly used in robotics and automation applications and are available in different models with varying ranges and resolutions.
  • Time-of-Flight (ToF) Sensors: These sensors use infrared light to measure distances and are suitable for use in applications that require high accuracy and fast response times. They are available in different models with varying ranges and resolutions.
  • Ultrasonic Ranging Module HC-SR05: This is a similar module to the HC-SR04 with minor differences, it has a wider detection range of up to 4.5 meters and a higher accuracy.


HC-SR04 Ultrasonic Sensor interface with Arduino

HC-SR04 Ultrasonic Sensor interface with Arduino
HC-SR04 Ultrasonic Sensor interface with Arduino


HC-SR04 Ultrasonic Sensor code for Arduino

  Ping))) Sensor

  This sketch reads a PING))) ultrasonic rangefinder and returns the distance
  to the closest object in range. To do this, it sends a pulse to the sensor to
  initiate a reading, then listens for a pulse to return. The length of the
  returning pulse is proportional to the distance of the object from the sensor.

  The circuit:
            - +V connection of the PING))) attached to +5V
            - GND connection of the PING))) attached to ground
            - SIG connection of the PING))) attached to digital pin 7

  created 3 Nov 2008
  by David A. Mellis
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.


// this constant won't change. It's the pin number of the sensor's output:
const int pingPin = 7;

void setup() {
  // initialize serial communication:

void loop() {
  // establish variables for duration of the ping, and the distance result
  // in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  digitalWrite(pingPin, HIGH);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): 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(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);

  Serial.print("in, ");


long microsecondsToInches(long microseconds) {
  // According to Parallax's datasheet for the PING))), there are 73.746
  // microseconds per inch (i.e. sound travels at 1130 feet per second).
  // This gives the distance travelled by the ping, outbound and return,
  // so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;

long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the object we
  // take half of the distance travelled.
  return microseconds / 29 / 2;


The output of the code displays the distance of an object from the sensor in both inches and centimeters in the serial monitor. 

To know more about HC-SR04 Ultrasonic Sensor using Arduino refer to this link


Examples of Ultrasonic Module interfacing

  1. HC-SR04 Ultrasonic Module Interfacing with PIC18F4550
  2. HC-SR04 Ultrasonic Module Interfacing with ATmega16
  3. HC-SR04 Ultrasonic Module Interfacing with TI Launchpad
  4. HC-SR04 Ultrasonic Module Interfacing with 8051
  5. HC-SR04 Ultrasonic Module Interfacing with Arduino
  6. HC-SR04 Ultrasonic Module Interfacing with ESP32
  7. HC-SR04 Ultrasonic Module Interfacing with ARM MBED

Components Used

Ultrasonic Module HC-SR04
Ultrasonic module HC-SR04 is generally used for finding distance value and obstacle detection. It can operate in the range 2cm-400cm.