Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp

Interfacing Ultrasonic Sensor HC-SR04 with Arduino Uno R3

Here we come with the topic Interfacing Ultrasonic Sensor HC-SR04 with Arduino Uno R3. As we know after getting the interfacing successfully lots of projects window will be open for us. In my earlier post, we saw that how to interface LM35 analog temperature sensor with Arduino at “Arduino based digital thermometer”. Now come to our topic, the Interfacing Ultrasonic Sensor HC-SR04 with Arduino Uno R3, for that what should we have to learn?

Obviously What is an Ultrsonic sensor? And how it work?

HC-SR04 module:

Interfacing Ultrasonic Sensor HC-SR04 with Arduino Uno R3

The ultrasonic sensor also known as distance measuring sensor which provides 2cm to 4m of non-contact measurement functionality and along with a ranging accuracy that can reach up to 3mm. Each HC-SR04 module includes an ultrasonic transmitter, a receiver and a control circuit. There are only four pins that you need to worry about on the HC-SR04. Those are VCC (Power), Trigger, Echo, and GND (Ground). Interfacing this Sensor with Arduino is easy which we going to learn now in some easy 7 steps.

Apparatus required 

  1. Arduino Uno R3 (ATMega328P) with Arduino Cable
  2. Ultrasonic Sensor HC-SR04 Module
  3. 4 Male-to-Female Jumper Wires & 8 Male-to-Male Jumper wires
  4. Breadboard
  5. Buzzer
  6. LED
  7. Battery 9V
  8. On-Off switch

Circuit Connection 

As we already know that there are 4 pins in the Ultrasonic Sensor HC-SRO4 Module and 28 pins in the Arduino Uno R3.

First, we have to set up the sensor module in the breadboard.

Second, we have to connect the 4 pins with one end of each of the male-to-female jumper wires. The other ends of the jumper wires coming out of the Vcc & GND pin are connected on the two separate sides of the breadboard & the other two ends coming out of the Trigger & Vcc pins are connected elsewhere on the breadboard.

Third, we connect two male-to-male jumper wires with the points where the Trigger & Echo pins have been connected & connect their other two ends with the Digital pins (pin7 with Trigger & pin8 with Echo) of the Arduino device.

Fourth, we take another two male-to-male jumper wires & connect them with the 5V (Vcc pin) & GND pin of the Arduino device & connect their other two ends at those points where we have placed the Vcc & GND pins of the Sensor Module. This simply means we setting up a common ground for all the GND pins.

Interfacing Ultrasonic Sensor HC-SR04 with Arduino Uno R3

Fifth, we set up the Buzzer & LED on the breadboard. For that, we connect the positive ends of the Buzzer & LED with the Arduino at the Digital pins (pin9 with Buzzer & pin3 with LED) by two male-to-male jumper wires.

Sixth, we connect the negative pins of the Buzzer & LED at the common ground where the GND pins of the Arduino & Sensor Module have been connected.

Finally we connect the 9V Battery & the switch with the breadboard.

Hence our circuit connection has been completed. Now we have to develop the program for Arduino interfacing with Ultrasonic Sensor. After developing the program we connect the Arduino cable with the Arduino & the pc.

Program Execution

The program to be uploaded in the Arduino device is written below. It has been verified accordingly in the Arduino app in the pc.

Embedded C Code
// Name : Interfacing Ultrasonic Sensor HC-SR04 with Arduino Uno R3
// Author : Subham Dutta
// Date : 24-04-18
// Website : www.nbcafe.in
#define buzzer 9
#define triggerPin 7 //triggering on pin 7
#define echoPin 8 //echo on pin 8
#define LED 3
void setup()
{
Serial.begin(9600); //start serial comunication
pinMode(triggerPin, INPUT); //defining pins
pinMode(echoPin, OUTPUT);
pinMode(buzzer, OUTPUT);
digitalWrite(buzzer,LOW); }
void loop()
{
int duration, distance; //Adding duration and distance
digitalWrite(triggerPin, HIGH); //triggering the wave(like blinking an LED)
delay(10);
digitalWrite(triggerPin, LOW);
duration = pulseIn(echoPin, HIGH); //a special function for listening and waiting for the wave
distance = (duration/2) / 29.1; //transforming the number to cm(if you want inches, you have to change the 29.1 with a suitable number)
delay(1000);
Serial.print(“SIEM”);
Serial.print(distance); //printing the numbers
Serial.print(“cm”); //and the unit
Serial.println(” “); //just printing to a new line
if (distance < 50) { digitalWrite(buzzer,HIGH); Serial.println(“Buzzer On”); digitalWrite(LED,HIGH); Serial.println(“LED On”); delay(1000); } if (distance > 50)
{
digitalWrite(buzzer,LOW);
Serial.println(“Buzzer Off”);
digitalWrite(LED,LOW);
Serial.println(“LED Off”);
delay(1000);
}
}

Working Action of the Arduino-Ultrasonic Sensor Interfacing

So the program for interfacing Ultrasonic Sensor & Arduino has been uploaded to the Arduino device through the Arduino app in pc.

To check whether the interfacing is working or not ? We will have to switch it ON. And we will able to see that it starts working. So what we learn till now it will be better to see the working action of the device which has been shown in the video below.

Here you see the complete video of  my project “Interfacing Ultrasonic Sensor HC-SR04 with Arduino Uno R3”.

Not Enough, Need More

E-Mail Subscription





E-Mail Subscription





Table of Contents
Subham Dutta

Subham Dutta

Hi myself Subham Dutta, having 15+ years experience in filed of Engineering. I love to teach and try to build foundation of students. Try to make them imagine what they learn.

Need more this type of content in your E-Mail?



Do NOT follow this link or you will be banned from the site!