INTERFACING HC-SR04 ULTRASONIC SENSOR MODULE WITH ARDUINO UNO

INTERFACING HC-SR04 ULTRASONIC SENSOR MODULE WITH ARDUINO UNO

Introduction

An Ultrasonic Sensor is a device that measures the distance to an object using Sound Waves. It works by sending out a sound wave at an ultrasonic frequency and waits for it to bounce back from the object. Then, the time delay between the transmission of sound and receiving of the sound is used to calculate the distance. It is done using the formula 

Distance = (Speed of sound * Time delay) / 2

We divide the distance formula by 2 because the sound waves travel a round trip i.e from the sensor and back to the sensor which doubles the actual distance.

The HC-SR04 is a typical ultrasonic sensor which is used in many projects such as an obstacle detector and electronic distance measurement tapes. The HC-SR04 module provides 2 cm to 400 cm non-contact measurement function. The ranging accuracy can reach to 3mm and effectual angle is < 15°. It can be powered from a 5V power supply. In this tutorial we teach you how to interface the HC-SC04 with an Arduino Uno.

 

 

 

 

 

 

 

 

 

 

 

Pin configuration of the Ultrasonic sensor:

Pin Number

Pin Name

Description

1

Vcc

The Vcc pin powers the sensor, typically with +5V

2

Trigger

pin is an Input pin. This pin has to be kept high for 10us to initialize measurement by sending US wave.

3

Echo

pin is an Output pin. This pin goes high for a period of time which will be equal to the time taken for the US wave to return back to the sensor.

4

Ground

This pin is connected to the Ground of the system.

Instructions for Ultrasonic sensor Usage

·        Using IO trigger for at least 10us high-level signal,

·        The Module automatically sends eight 40 kHz and detect whether there is a

·        Pulse signal back.

·        If the signal back, through high level, time of high output IO duration is

·        The time from sending ultrasonic to returning.

·        Test distance = (high level time × velocity of sound (340M/S) / 2,

 

                                       Download Datasheet PDF:
HC SR04 Ultrasonic Sensor Datasheet

 

 

 

 

 

 

 

Circuit Diagram

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Connections

·       The Vcc pin of the Ultrasonic sensor is connected with the 5V pin of the Arduino board

·        The Trigger pin of the sensor is connected to pin 9 of the Arduino board.

·        The Echo pin of the sensor is connected to pin 10 of the Arduino board.

·        The GND pin of the sensor is connected with the Ground pin of the Arduino board.

 

 

 

Arduino Code

                       // defining the pins

 const int trigPin = 9;

 const int echoPin = 10;

 // defining variables

 long duration;

 float distance;

 void setup()

{

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

 pinMode(echoPin, INPUT); //  Sets the echoPin as an Input

 Serial.begin(9600);     //  Starts the serial communication

}

void loop()

{

 // Clears the trigPin

 digitalWrite(trigPin, LOW);

 delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds

 digitalWrite(trigPin, HIGH);

 delayMicroseconds(10);

 digitalWrite(trigPin, LOW);

 // Reads the echoPin, returns the sound wave travel time in microseconds

 Duration = pulseIn(echoPin, HIGH);

 // calculating the distance

 Distance= duration*0.034/2;

 // Prints the distance on the Serial Monitor

 Serial.print("Distance: ");

 Serial.println(distance);

 delay (300);

}

 

 

 

The result of a project: