How To Use HC-SR04 Ultrasonic Sensor With Arduino Uno | DIY Project

1,293 views

Ultrasonic sensors calculate the distance of a target object by the use of ultrasonic waves and transduce the reflected sound waves into an electrical signal. They are extremely precise and accurate since ultrasonic waves travel faster than the speed of audible sound. Ultrasonic sensors provide a unique advantage over conventional sensors in terms of increased detection sensitivity and sensing range. Ultrasonic sensors are becoming widely accepted as an industry-standard across the globe due to their cost-effectiveness, low maintenance, and high accuracy. In today’s tutorial, we will go over a step by step procedure on How To interface an HC-SR04 Ultrasonic Sensor With Arduino Uno.

HC-SR04 Ultrasonic Sensor

HC-SR04 Ultrasonic Distance Sensor is used for determining the distance to an object utilizing sonar. The sensor uses non-contact ultrasound sonar (housed within a speaker config) to measure the distance to an object and consists of two ultrasonic transmitters, a receiver, and a control circuit. The transmitters emit a high frequency ultrasonic sound, that bounces off the target object, and are collected by the receiver as a return echo. That echo is then processed by the control circuit to calculate the time difference between the signal being transmitted and received. Then the distance to the target object can be calculated by using the following relation:

S = (V x t)/2

Key Features & Specifications

  • Input Voltage: 5V
  • Current Draw: 20mA (Max)
  • Digital Output: 5V
  • Digital Output: 0V (Low)
  • Working Temperature: -15°C to 70°C
  • Sensing Angle: 30° Cone
  • Ultrasonic Frequency: 40kHz
  • Range: 2cm – 400cm
  • Accuracy: 3mm
  • Measuring angle covered: <15°
  • Operating Current: <15mA
  • Operating Frequency: 40Hz
  • Transmitter diameter: 8mm

Hardware Components

you will need the following parts to build this project

S.NoComponentValueQty
1)Arduino UnoRev3, 8KB1
2)Ultrasonic SensorHC-SR041
3)Buzzer5V1
4)LED5mm, 3.5V1
5)Resistor220 Ohm2
6)Arduino USB Cable1
7)Laptop/PC1
8)Breadboard1
9)Jumper WiresAs per need

HC-SR04 Pinout

Useful Steps

1) Connect the HC-SR04 sensor on the breadboard. After that connect the sensor Vcc terminal with the +5V terminal of the Arduino uno.

2) After that connect the sensor GND pin with the GND terminal of the Arduino board.

3) After that, connect the sensors TRIG pin with the Digital I/O pin 9 of the Arduino board.

4) After that, connect the sensors echo pin with the digital I/O pin 10 of the Arduino Uno.

5) Now, connect the +ve pin of the 5V buzzer with Digital I/O pin 11 of the Arduino board. After that, connect the -ve pin of the buzzer with the GND of the Arduino.

6) Connect the +ve pin of the LED with the Digital I/O pin 13 of the Arduino board through a 220 Ohm resistor. After that, connect the -ve pin of the LED with the GND of the Arduino.

7) Connect the Arduino Board to a Laptop/PC and upload the code given below. After that, power up and test the circuit.

Circuit Diagram

Source Code

const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;

long duration;
int distance;
int safetyDistance;

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

void loop() 
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance= duration*0.034/2;

safetyDistance = distance;
if (safetyDistance <= 5)
{
  digitalWrite(buzzer, HIGH);
  digitalWrite(ledPin, HIGH);
}
else
{
  digitalWrite(buzzer, LOW);
  digitalWrite(ledPin, LOW);
}

  Serial.print("Distance: ");
  Serial.println(distance);
}

Working Explanation

The working of this circuit is as follows. On powering up the circuit, the ultrasonic sensor fires the ultrasonic waves in the order of 40,000 Hz through the air medium. The majority of the waves then strike the surface of the object & the reflected waves are then collected by the sensor’s receiver.

As the waves are collected back by the receiver, the ECHO pin on the sensor will turn ON & will remain on until the sound waves are received by the receiver. This time will be conveyed back to the Arduino module & will allow you to measure the distance of the target object from the sensor module.

Applications

  • Used in procedures such as Medical ultrasonography, Deep sea expeditions, Deep state oil mining, etc.
  • Also used for wireless charging.

See Also: Ding Dong Sound Generator Circuit | How to setup WiFi on Raspberry Pi 3? | Automatic Water Level Controller