INTERFACING OF ARDUINO UNO WITH ULTRASONIC SENSOR 

Complete arduino kit for beginners

This tutorial is about the interfacing of the Arduino UNO board with HC-SR04 commonly known as ultrasonic sensor and code to run the sensor and obtain the output from the sensor accordingly.

RE

List of the components

  1. Arduino uno board
  2. HC-SR04 ultrasonic senso
  3. Bread board
  4. Jumper wires

Working principle of the ultrasonic sensor 

The working is very simple, the trigger pin of the HC-SR04 emits and transmits a high-frequency pulse. The HC-SR04 uses high frequency sound waves to find this distance, a kind of echolocation. If we need a machine to know about an obstacle in its path,we should  use an ultrasonic sensor. The HC-SR04 sensor echo pin senses the reflection. 

Pins Of Ultrasonic Sensor

HC-SR04 Ultrasonic sensor has four pins.

Pin 1=Vcc=5v input  voltage to power the sensor. 

Pin 2=trig pin=input pin for sending ultrasonic waves.

Pin 3=echo pin=output pin.

Pin 4=ground pin=it should be connected to the ground.

Explanation

The Arduino UNO has a micro-controller that reads data from various inputs and can compute it according to the programming code. Arduino Uno has 14 digital input & output pins and also 6 analog pins. The Arduino Uno power supply can be done with the help of a usb cable or an external power supply. The suggestion voltage range power is 9 to 12 volts. The circuit of the Arduino and distance sensor consists of  three parts.

(i) Micro-controller

(ii) Transmitter 

(iii) Receiver

Microcontroller

  1. It controls the timing and sending of pulses. 
  2. It receives the signal from the receiver as an interrupt, it also controls the frequency and speed of the sound wave generated and also record’s the time taken to receive the reflected wave.

Transmitter

The transmitter of the HC-SR04 emits high frequency sound waves.

Receiver

It receives the ultrasonic wave reflected by the object in front of the sensor 

Pin Diagram

  1. Connect the VCC pin of the distance sensor(HC-SR04) with the 5V VCC output pin of the Arduino Uno board.
  2. Now, connect the trigger of HC-SR04 to digital pin 11 on Arduino board
  3. Then connect the echo pin of HC-SR04 to digital pin 12 on Arduino board.
  4. Finaly, Connect the ground pin of the ultrasonic sensor to Arduino’s ground pin.

Code

#define echoPin 2 // attach pin D2 Arduino to pin Echo of HC-SR04
#define trigPin 3 //attach pin D3 Arduino to pin Trig of HC-SR04
// defines variables
long duration; // variable for the duration of sound wave travel
int distance; // variable for the distance measurement
void setup() {
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an OUTPUT
  pinMode(echoPin, INPUT); // Sets the echoPin as an INPUT
  Serial.begin(9600); // // Serial Communication is starting with 9600 of baudrate speed
  Serial.println("Ultrasonic Sensor HC-SR04 Test"); // print some text in Serial Monitor
  Serial.println("with Arduino UNO R3");
}
void loop() {
  // Clears the trigPin condition
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin HIGH (ACTIVE) for 10 microseconds
  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; // Speed of sound wave divided by 2 (go and back)
  // Displays the distance on the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");
}

Result

Upload the code to the Arduino UNO board and the distance of the object can be seen on the serial monitor screen. For any queries, the comment box is always open for you.

Spread knowledge

5 thoughts on “Interfacing of HC-SR04 Ultrasonic Sensor with Arduino Uno”

Leave a Comment

Your email address will not be published. Required fields are marked *