HC-SR04 ultrasonic sensor interfacing with arduino

An HC-SR04 ultrasonic module is a non-contact distance measuring device consists of an ultrasonic transceiver unit (with transmitter and receiver section of ultrasonic transducers) and a control circuit.

The HC-SR04 module has 4 pins, Vcc, trig, echo and GND

To initiate the HC-SR04, a trigger pulse should be given as the trigger input to the module (here, the trig pin is connected to Arduino pin2).  An input pulse of 10us can trigger the module.

On receiving the trigger pulse, the HC-SR04 module sends eight cycles of 40KHZ pulses (8 x 40KHZ); operation frequency of the module is 40KHZ.

hc-sr04-ultrasonic-sensor-module-front-view,ultrasonic-transducer-pcb       hc-sr04-ultrasonic-module-back-view,hc-sr04-ultrasonic-module-pin-diagram

A pair of identical ultrasonic transducers converts the electrical signals to ultrasonic sound waves and again the received sound signals back to electric signals.

If the transmitted waves get reflected back, it will be detected by the receiver transducer. The module analyses the time taken for the propagation of waves and generates an output signal with a pulse width in proportion to the measured range or distance to the object.

HC-SR04 interface with Arduino

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

hc-sr04-ultrasonic-module-arduino-interfacing-circuit,arduino-ultrasonic-ranging

The output durations at the  Echo pin are converted 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

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.

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.

Connecting Parallax PING))) sensor to Arduino

Arduino pin PING
5V Vcc
Digital pin (eg:- 2) SIG ( I/O )
GND GND
Parallax PING sensor Arduino code
const int sig = 2;
long duration, Distanceincm, Distanceininch;
void setup() {
 Serial.begin(9600);
}
void loop() 
{
 pinMode(sig, OUTPUT);
 digitalWrite(sig, HIGH);
 delayMicroseconds(15);
 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(Distanceincm);
 Serial.print("   Distance in inches = ");
 Serial.println(Distanceininch);
 delay(500);
}

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!
Loading...

You may also like...

Leave a Reply

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