ad space

How to measure distance with HC-SR04 and Arduino

Here ultrasonic sensor module HC-SR04 is used with Arduino to measure the distance of an object. HC-SR04 module has acoustic transducer which can be controlled with microcontroller or microcontroller boards like Arduino or Raspberry Pie. Here Arduino is used to control HC-SR04 module. The HC-SR04 module consist of transmitter, receiver and control unit. The HC-SR04 has acoustic transmitter that sends out acoustic sound wave when it receives electrical transmit sigal and has receiver which converts reflected received acoustic sound wave into electrical signal. That is HC-SR04 module contains transducer which converts electrical signal to acoustic sound wave and vice versa. 

HC-SR04 Arduino Interfacing

The following shows Arduino Uno connected to HC-SR04 module.

Ultrasonic with Arduino Uno

The HC-SR04 module has four pins- VCC, GND, TRIGGER pin and ECHO pin. These pins can be connected directly to Arduino. The following shows the circuit diagram of interconnecting Arduino Uno and HC-SR04 module.

circuit diagram of Arduino uno and hc-sr04
 The Vcc and GND of HC-SR04 is connected to the Arduino +5V and GND pins. The echo pin and trigger pin of the HC-SR04 module is connected to pin 12 and pin 11 of the Arduino Uno. 

How the ultrasonic sensor works?

Arduino sends 10us pulse to the trigger pin of the HC-SR04. When HC-SR04 receives this trigger pulse it sends out 40KHz burst of 8 acoustic wave from its transmitter. At the same time it sends out the acoustic wave a counter is started. The maximum theoretical distance over which the HC-SR04 works is in the measurement range of 2cm to 400cm but practically it works in the measurement range about 2cm to 80cm. The angle covered for measurement is 15 degree. 

When HC-SR04 receives reflected acoustic wave, it sends out the echo signal to the Arduino which will be high according to the time it took to start transmission and reflection detection time. This high pulse time of the echo signal is processed in Arduino to calculate the distance of the object. The following formula is used to calculate the distance in cm and inch.

\[distance(cm) = \frac{Echo \; Pulse \; Width(uS)}{58}\]

\[distance(inch) = \frac{Echo \; Pulse \; Width(uS)}{148}\]

In case of measuring the distance of the object we have to taken into account that the duration of the pulse sent and received takes two times the measured time so we have to divide the distance by 2. Thus the formula to calculate the distance is as follows.

 \[distance = \frac{duration \times speed \; of \; sound}{2}\]

Arduino program 

The following Arduino code sends out trigger signal, receives echo signal, calculates the distance of an object with HC-SR04 and print the distance on serial monitor.


//simple program to calculate distance with HC-SR04

#define echoPin 11 // Echo pin connected to Arduino 11
#define trigPin 12 // Trig pin connected to Arduino 12

void setup() {
   Serial.begin (115200);
   pinMode(echoPin, INPUT);
   pinMode(trigPin, OUTPUT);
}

void loop() {
   //define some constants
   int duration, distance;
   
   //send trigger signal
   digitalWrite(trigPin, LOW);
   delayMicroseconds(2);
   digitalWrite(trigPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(trigPin, LOW);

   //calculate distance
   duration = pulseIn(echoPin, HIGH);
   distance = (duration / 2) / 29.1;

   //print it
   Serial.print("distance:");
   Serial.print(distance);
   Serial.println(" cm");

   //add some delay
   delay(500);
}

Results

After uploading this sketch we will see the distance of an object in front of the HC-SR04 displayed on the serial monitor.

serial monitor

The following video demonstrates how the above distance measurement system works.

 Application of ultrasonic sensors

The ultrasonic sensor like the HC-SR04 can be used to make intruder detection system with Arduino. Other application of ultrasonic sensor includes anemometers in weather station, to make tide gauge which is used to monitor sea level, to measue fluid level in a tank, to measure distance and used for guidance system in Unmanned aerial vehicles (UAV) or drones, robots, industrial robotics. 

The following shows simulation of HC-SR04 ultrasonic sensor with Arduino in Proteus electronics design software.


Post a Comment

Previous Post Next Post