In this tutorial, we will learn about the working of HC-SR04 ultrasonic sensor, how to use this with Arduino. This tutorial explains details about the working of HC-SRO4 ultrasonic sensor, Schematic diagrams, and sample code.

HC-SRO4 Ultrasonic Sensor

HC-SR04 sensors are commonly used to find the distance with our carrying a physical measurement. HC-SR04 ultrasonic sensor uses SONAR to determine the distance of an object just like the bats do to travel at night. These sensors offer excellent non-contact range detection with high accuracy along with stable readings for easy usage. The operation of the Ultrasonic sensor HC-Sro4 cannot be affected by sunlight or dark.

Specifications of HC-SR04 Ultrasonic sensor

  • Power Supply :+5V DC
  • Quiescent Current : <2mA
  • Working Current: 15mA
  • Effectual Angle: <15°
  • Ranging Distance : 2cm – 400 cm/1″ – 13ft
  • Resolution : 0.3 cm
  • Measuring Angle: 30 degree
  • Trigger Input Pulse width: 10uS
  • Dimension: 45mm x 20mm x 15mm

How Ultrasonic Sensor works ?

HC-SR04 Ultrasonic sensor comes with an ultrasonic transmitter and receiver module. The transmitter part emits an ultrasonic sound wave of 40Khz. Sound waves travel through the air and if there is an object or obstacle on its path It will bounce back and be captured by the receiver.

How Ultrasonic Sensor works ?

The time between the transmission of the Ultrasonic signal and the Receiving of the bounce-backed signal will help find the distance between the object and sensor. This is possible because we know the speed of sound.

To generate the ultrasonic sound you need to set the TrigPin ON a High State for 10 µs. That will send out an 8 cycle of sonic burst which will travel at speed sound and it will be bounced back and received in the Echo pin.

Ultrasonic Sensor  signal clock

For example, if an object is 10 cm away from the ultrasonic sensor, and the speed of the sound is 0.034 cm/µs or 340 m/s. An ultrasonic sound wave will need to travel about 294 µ seconds. But the time you got from the Echo pin will be double that number because the ultrasonic sound wave needs to travel forward and bounce backwards.  So in order to get the distance in cm, we need to multiply the received travel time value from the echo pin by 0.034 and divide it by 2.

 Ultrasonic Sensor  based disatance calulation

HC-SR04 Ultrasonic Sensor Pinout

The HC-SR04 Ultrasonic sensor Module has 4 pins, Ground, VCC, Trig and Echo

HC-SR04 Ultrasonic Sensor Pinout : its has 4 pins VCC,Trig,Echo and GND

HC SR04 Ultrasonic  Sensor Arduino Code

HC-SR04 ultrasonic sensor si the widely used sensor among the maker community. This sensor I easy to connect and program makes it a favourite among makers. In this tutorial, we are going to use an ultrasonic sensor to measure a distance in cm and display it on the serial monitor.

The goal of this tutorial is to understand how the HC-SR04 ultrasonic sensor works, how to connect the Sensor with Arduino, and How to write and upload the code to Arduino. Adopt the tutorial lessons and make your own projects.

Parts Required

  • Arduino Development board ( Here I am using Arduino UNO ): amazon link
  • Ultrasonic distance sensor

Schematics

Follow the below connection diagram to connect Arduino nano with Ultrasonic distance sensor

image is designed in fritzing software

Source Code for the Project

/*
* Ultrasonic Sensor HC-SR04 and Arduino Tutorial
* By Amith G Nair
* 17 May 2020
* electrorules.com
*
*/
// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;

// defines variables
long duration,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);
}

How the Code Works

First, you create two variables trigPin and echoPin to store the pin number, respectively trigPin is connected to digital pin 9 of Arduino nano and echoPin is connected to digital pin 10 of Arduino nano.

const int trigPin = 9;
const int echoPin = 10;

You also need two variables to store the duration, distance, both of them are declared as long. The duration variables store the time between the emission of ultrasonic sound and the reception of the sound signal. The distance variable stores the distance calculated distance value.

// defines variables
long duration,distance;


In setup() first, we need to initialize the serial monitor, for that we choose the baud rate as 9600. Also, we set trigPin as the output pin and echo pin as input.

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


In the loop() function first we need to make sure that trigPin is clear, for that set that pin to LOW state for2 microseconds.

// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

Next for generating the ultrasonic sound, we need to set the trigPIN on the HIGH state for 10microseconds.

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);


Using the pulseIn() function you have to read the travelled time and put that value into the variable duration.pulseIn() function has pass two parameters, first one is the name of echoPin and the second one you can write either HIGH or LOW

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);


In that case, HIGH means pulseIn() function waits for the pin to go high caused by the reflected sound from that object on the propagation path and start timing, then it will wait for the pin to go LOW when the sound wave will end which will stop timing. In the end, the function will return the length of the pulse in microseconds
For getting the distance we need to multiply with value ( 0.0034 /2) gives the distance in cm

// Calculating the distance
distance= duration*0.034/2;

Final code printes the distance value on the serial monitor

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);

Wrapping Up the tutorials

In this tutorial, you learned about how ultrasonic sensor works and connect them with Arduino nano. For example, you can try the smart cane using an ultrasonic sensor.

Related Links

Author

Write A Comment