HC-SR04 ultrasonic sensor interfacing with arduino

HC-SR04 ultrasonic sensor

An hc sr04 ultrasonic module is a non-contact range measuring device consist of an ultrasonic transceiver unit (transmitter and receiver section) and a control circuit.

The transmitter and receiver section has a similar ultrasonic transducer. The transmitter section transmits ultrasonic sound waves corresponding to the electrical signals given to the transducers.

The hc sr04 module has 4 pins, Vcc, trig, echo and GND. To initiate the HC-SR04, a trigger pulse of 10us should be given to the trigger input of the module (here, the trig pin is connected to Arduino pin2). An active high state input greater than 10us  will initiate the module. On receiving the trigger pulse, the HC-SR04 module sends eight cycle burst of 40KHZ ultrasound pulses (8 x 40KHZ); operation frequency of the ultrasonic module is 40KHZ.

ultrasonic sensor hc-sr04   hc sr04 ultrasonic sensor

The ultrasonic signals reflected by objects are received at the receiver transducer and it is converted back to electric signals. From this signal, the module analyses the time taken for the propagation of waves.

The module generates a pulse width output at the Echo pin in proportion to the distance to the reflecting surface.

HC-SR04 ultrasonic sensor Arduino interface

Arduino pin HC- SR04
5V Vcc
Digital pin (eg:- 2) Trig  ( Trigger Pulse Input  )
Digital pin (eg:- 3) Echo(  Echo Pulse Output )

hc-sr04 ultrasonic sensor interfacing with arduino

The Arduino code converts the output durations at the Echo pin to the corresponding unit of the measured distance.

Conversion method of the pulse width to distance values,

Distance in centimetre = Duration in uS / 58

Distance in inch =  Duration in us /148

The total travel time of waves is the value of duration (High-level time of echo output) or the pulse width. So the travel time required to reach the object is duration/2.

Distance = Velocity x  time

The velocity of sound = 340m/s

Distance =  340 x duration x 10 -6 /2  (duration in micro seconds is converted to seconds by , multiplying  10 -6 ).

Distance in centimetre  = duration * 340 * 10-4  / 2

=  duration / 58

Distance in inch             =  duration * 340 * 10-4  / 2   *  2.54

=  duration / 148

An hc-sr04 ultrasonic module can measure the distance up to a range of 2cm to 400cm, with a measuring angle of 30 degrees.

»  4 pin ultrasonic sensor arduino code

Connect HC SR04 as a 3 pin ultrasonic sensor

3 pin PING))) sensor

A Parallax PING))) is a 3 pin header Ultrasonic Distance ranging Sensor. It has a single shared I/O signal pin “SIG”, used to trigger and read the return echo pulse from the Ping sensor. Whereas an HC- SR04 uses separate trig and echo pins for the same operations.

Parallax PING))) sensor connection to Arduino

Arduino pin PING
5V Vcc
Digital pin (eg:- 2) SIG ( I/O )
Parallax PING sensor Arduino code
const int sig = 2;
long duration, Distanceincm, Distanceininch;
void setup() {
void loop() 
 pinMode(sig, OUTPUT);
 digitalWrite(sig, HIGH);
 digitalWrite(sig, LOW);
 pinMode(sig, INPUT);
 duration = pulseIn(sig, HIGH);
 Distanceincm = duration / 58;  // Pulse width to centimetres
 Distanceininch = duration / 148; // Pulse width to inches
 Serial.print("Distance in cm = ");
 Serial.print("   Distance in inches = ");

Here we are interfacing a 4 pin HC SR04 to a 3 Pin Sensor module to use as a 3 wire connection, which shares the trigger and echo pins to operate in a single line. An HC SR04 module can operate through a single line as like a PING sensor. So it can run with the same Arduino code used for a PING))) sensor.

For that, connect both echo and trigger header pins to a single digital pin of the Arduino board and upload the same code used for PING sensor. In the program, the specified pin first configures as output and give the high state trig pulse. Then it Configures as an input pin and reads the echo. That is for each measurement the pin switches to output and input alternatively, performs read-write operations through a single wire.

1 Star2 Stars3 Stars4 Stars5 Stars Rate Us!

You may also like...

Leave a Reply

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