How the HC-SR04 Sensor Works and Example Programs with Arduino

Spread the love


HC-SR04 is an ultrasonic sensor module that is usually used for distance measuring devices. In this article we will learn how to work from the HC-SR04 sensor and the following example program using Arduino.

In HC-SR04 there is a pair of ultrasonic transducers, one of which functions as a transmitter whose duty is to convert an electrical signal to an ultrasonic sound wave pulse signal with a frequency of 40KHz, and one serves as a receiver whose job is to receive ultrasonic sound wave signals.

HC-SR04 Sensor Module

What is ultrasonic waves? Ultrasonic sound waves are sound waves with frequencies that are above the limits of human hearing. As we know that the limit of human hearing is in the frequency range of 20Hz – 20KHz.

How HC-SR04 Works

A pulse signal with a duration of at least 10 μS (10 microseconds) is applied to the trigger pin. After that, the sensor transmits eight pulses of ultrasonic waves at a frequency of 40 KHz. This 8-pulse pattern is used for an ultrasonic signal marker of this module, which allows the receiver to distinguish the transmitted pattern from the surrounding ultrasonic noise.

Eight ultrasonic pulses move through the air away from the transmitter / transmitter leading to the object or object in front of it. Meanwhile the Echo pin becomes HIGH to start forming the beginning of the echo signal.

If no ultrasonic signal is reflected or received by the receiver during the range of 38 mS (milliseconds), which means there are no objects or objects then the Echo signal will timeout and return to LOW.

Meanwhile, if there is an ultrasonic signal that is reflected or received by the receiver, then the Echo signal will immediately change to LOW. Well, the width of the time span of the ECHO signal is used to measure the distance between the sensor and the object or object.

By using the distance – speed – time equation of sound waves that propagate in the air, it can be described as follows :

Distance = Speed x Time

where the speed of sound waves in the air is = 340 m/s = 0.034 cm/μS. Because the distance of the ultrasonic sound waves was alternating from the sensor (transmitter) to the object and back to the sensor (receiver), the formula becomes :

Distance (cm) = Time (μS) * 0.034 / 2

Thus is the theory and workings of the HC-SR04 sensor which can be used to measure the distance of an object.


HC-SR04 sensor has the following specifications :

  • Supply Voltage : 5V DC
  • Static Current : < 2mA
  • Output Level : 0v – 5V
  • Angle of Sensor : < 15 derajat
  • The distance that can be measured : 2cm – 450cm (4.5m)
  • Accuracy : up to 0.3cm (3mm)

Material Required

The materials or hardware used in the examples in this program include :

  • Arduino UNO or others
  • HC-SR04 Sensor
  • Some of Jumper Cabel M-F atau F-F

Wiring Diagram

The circuit between Arduino and the HC-SR04 sensor module used in this example can be seen in the following table :


Code Program

 int Trig_Pin = 13;
 int echo_Pin = 12;

 void setup()
    pinMode(Trig_Pin, OUTPUT);
    pinMode(echo_Pin, INPUT);
 void loop()
    int pulse, inches, cm;
    digitalWrite(Trig_Pin, HIGH);
    digitalWrite(Trig_Pin, LOW);
    pulse = pulseIn(echo_Pin, HIGH);
    cm = pulse * 0.034 / 2;
    inches = cm * 2.54;
    Serial.print("Jarak (cm) : ");
    Serial.print("Jarak (inch) : ");


This is an illustration of how the HC-SR04 sensor works and an example program for measuring distances.

++++++++ Hope This Useful ++++++++

Be the first to comment

Leave a Reply

Your email address will not be published.