Interfacing Ultrasonic sensor with Arduino UNO

HC-SR04
As show in the figure HC-SR04 is an ultrasonic sensor. It is an 4pin module consists of Vcc, Trig, Echo and GND. Module had a transmitter and receiver which transmits and receive the ultrasonic wave signal.

Pins Explanation:

VCC – +5v dc

TRIG – To activate sensor we should give the HIGH pulse of 20 microseconds of time to the trig pin then module transmits the ultrasonic wave.

ECHO – This pin will get high whenever the module receives objected wave. otherwise pin will be always low

GND– ground

Working:

This module will have two objects(look like eye) that is transmitter and receiver. The Ultrasonic transmitter transmits an ultrasonic wave, this wave travels in air and when it gets objected by any material it gets reflected back toward the sensor this reflected wave is observed by the Ultrasonic receiver.

This module is used to measure a distance and object finding. To find distance of a object from the sensor, we use a simple formula

Distance = time * speed

Here,
Distance gives the objected distance
time represents wave transmission and reception period
speed represents wave transmission speed of ultrasonic wave (300m/s)

Example Code:

  1. #define trig 2
  2. #define echo  3
  3. void setup()
  4. {
  5.   Serial.begin (9600);
  6.   pinMode (trig, OUTPUT);
  7.   pinMode (echo, INPUT);
  8. }
  9. void loop()
  10. {
  11.   unsigned long response;
  12.   int distance;
  13.   digitalWrite (trigger, HIGH);
  14.   delayMicroseconds (10);
  15.  digitalWrite (trigger, LOW);
  16.   response = pulseIn (echo, HIGH);
  17.   distance = (response * 0.034) / 2;
  18.   Serial.print (“Distance= “);
  19.   Serial.println (distance);
  20.   delay(1000);
  21. }

Code Explanation:

In the first two lines declared macros for trig and echo pins respectively.
In setup function first instruction is to configure serial data transmission from arduino to PC. This instruction is mandatory to send values to the PC. pinMode is to initialize the pin mode as input and output respectively. To know more information of predefined function go to my previous blog click here.
In loop function declared two local variables. Firstly, to activate the module we have to give the high pulse of 20us of time period to the trig pin. After objected wave is detected by the module, module makes the high pulse on the echo pin. So, here pulseIn method is used to calculate the time period of wave transmission and reception until specified pin is gets the value HIGH/LOW. pulseIn returns the duration of wave travel.
In 18th line we converting the time travel by the wave to centimeters. response represents the time taken by the echo pin go to HIGH(i.e., objected wave), 0.034 is the speed of the wave and divided to 2 because response is the time duration for travel in both direction, so we divided by 2 to get the wave travel of one direction.

OUTPUT:

Distance= 80
Distance= 10
Distance= 8

Output values be in unit of centimeter.

Leave a Reply

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