How to Interface HC-SR04 Ultrasonic Sensor With Arduino UNO

4,130 views

Introduction

In modern automation, many times you have noticed that almost every robot changes its direction when observing any hurdles in its way. And, if you’re interested in robotics, you already have comprehended that this is because of the sensor that is wired in the robot, popularly known by the name ‘ultrasonic sensor’. Basically, ultrasonic sensors help to detect the high-pitched pulse signals or waves that are inaudible to humans. As we know that humans can hear the range of 20Hz to 20KHz. Ultrasonic sensors can observe the readings more than this range. To understand this, In this tutorial, we are going to interface ” HC-SR04 Ultrasonic Sensor with Arduino UNO”.

How does Ultrasonic Sensor work?

An ultrasonic sensor estimates the distance of the object by generating ultrasonic waves. Then, it transforms those inaudible sound waves into electrical signals. Hence, the sensor contains two elements, that are transmitter and the receiver. The transmitter emits the waves using a piezoelectric crystal. While the receiver confronts that sound wave. The formula to measure the distance is generally given as D=1/2 T*C.

HC-SR04 Ultrasonic Sensor

HC-SR04 contains the transmitter and the receiver. The operating voltage of the sensor is around 5 Volts. Theoretically, it can measure the distance from 2 cm to 450cm. But, practically it estimates in the range from 2cm to 80 cm. Further, the measuring angle is less than 15 degrees. It can easily operate on a frequency of 40Hz. Also, the operating current range of the sensor is less than 15mA. Usually, the sensor can get interfaced with both microcontrollers like Arduino and microprocessors like PIC. The sensor has four pins. Vcc and ground pins are there to provide the power supply and ground respectively. To initialize and start the measurement the trigger pin must have been high for about 10us and then turned off. And echo pin is the output pin. This pin goes high for the time period equal to the time taken for the wave to return.

hcsr04-ultrasonic-distance-sensor-module

Hardware Required

S.noComponentValueQty
1.ArduinoUNO1
2.USB Cable Type A to B1
3.Jumper Wires1
4.Ultrasonic SensorH-C-SR041

Circuit Diagram

HC-SR04-Ultrasonic-Sensor-Interface-With-Arduino-UNO-Tutorial

Connection Table

ArduinoHC-SR04 Ultrasonic Sensor
GNDGND
D9Trig
D10Echo
5V5V

Arduino Code

// This uses Serial Monitor to display Range Finder distance readings

// Include NewPing Library
#include "NewPing.h"

// Hook up HC-SR04 with Trig to Arduino Pin 9, Echo to Arduino pin 10
#define TRIGGER_PIN 9
#define ECHO_PIN 10

// Maximum distance we want to ping for (in centimeters).
#define MAX_DISTANCE 400	

// NewPing setup of pins and maximum distance.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
float duration, distance;

void setup() 
{
	Serial.begin(9600);
}

void loop() 
{
	// Send ping, get distance in cm
	distance = sonar.ping_cm();
	
	// Send results to Serial Monitor
	Serial.print("Distance = ");
	
	if (distance >= 400 || distance <= 2) 
	{
		Serial.println("Out of range");
	}
	else 
	{
		Serial.print(distance);
		Serial.println(" cm");
	}
	delay(500);
}

Working Explanation

To interface the HC-SR04 ultrasonic sensor with the Arduino, follow the above schematic for connections. After that, open the Arduino IDE and paste the above-mentioned code there. Now, upload that code into your Arduino UNO. Open the serial monitor. Place anything in front of the sensor within 400 centimeters and observe the readings on the serial monitor.

Code Explanation

  • Install the library. Defines the ultrasonic pins that are connected with the pins of Arduino. Use variable MAX_DISTANCE to define the maximum distance in centimeters.
  • In the void setup, initialize the serial monitor by using Serial. begin ( )
  • In the void loop, use sonar. ping( ) to measure the distance. Use the function Serial. print to print that distance. Use the if the distance is greater than 400 and less than 2 centimeters, it will print out of range. Else, it will print the distance in centimeters.

Application and Uses

  • In robots, to avoid obstacles and detect the path.
  • For the devices, that are used to map the objects.
  • in the security devices like alarms, etc.