Ultrasonic Sensor HC SR04 Pinout, Connection, Wiring Diagram
Hi, in this article, we are going to see the Ultrasonic Sensor HC SR04 Pinout, Connection, and Wiring Diagram. The Ultrasonic Sensor HC-SR04 is a device that is used to measure distances using sound waves. It works by sending out an ultrasonic sound (which is too high-pitched for humans to hear) and then listening for the echo when the sound bounces back from an object. The sensor basically has two main parts, a transmitter and a receiver. The transmitter sends out a quick burst of sound and the receiver waits for the sound to bounce back from an object in front of it. The time it takes for the sound to return is recorded. The sensor uses the time it took for the sound to bounce back to calculate how far away the object is. The longer it takes, the farther away the object is. The HC-SR04 sensor is commonly used in projects like robots, obstacle detection, and measuring the distance to objects.
Ultrasonic Sensor HC SR04 Pinout Diagram
Here, you can see the pinout diagram of the HC SR04 Ultrasonic Sensor.
As you can see in the above diagram, the HC-SR04 Ultrasonic Sensor has four pins.
The VCC pin is the Power supply pin. This pin should be connected to the 5V power output of your microcontroller or Arduino.
The Trig (Trigger) pin is used to initiate the measurement. This pin should be connected to a digital output pin on your microcontroller. When you send a short pulse (usually 10 microseconds) to this pin, it triggers the sensor to send out an ultrasonic pulse.
The Echo pin is used to receive the echo signal. This pin is to be connected to a digital input pin on your microcontroller. The sensor will set this pin to HIGH for the amount of time it takes for the echo to return. You measure this time to calculate the distance.
The GND or Ground pin is to be connected to the ground (GND) pin of your microcontroller.
Ultrasonic Sensor HC SR04 Connection(Interfacing) with Arduino
Here, you can see the connection diagram between Ultrasonic Sensor HC SR04 and Arduino Uno.
Connection Procedure
- Connect the VCC pin of the sensor to the 5V pin on the Arduino.
- Connect the GND pin of the sensor to one of the GND pins on the Arduino.
- Connect the Trig pin of the sensor to Digital Pin 9 on the Arduino.
- Connect the Echo pin of the sensor to Digital Pin 10 on the Arduino.
Ultrasonic Sensor HC SR04 Wiring with Arduino for Distance Measurement
Here, you can see the wiring diagram for Distance Measurement using Ultrasonic Sensor HC SR04 and Arduino.
Working Principle
First of all, the Arduino sends a short pulse to the HC-SR04's Trig pin. This pulse triggers the sensor to emit an ultrasonic sound wave. The sensor emits a sound wave, which travels through the air, reflects off an object, and returns to the sensor. The Echo pin receives the reflected wave and sends a pulse back to the Arduino.
Now, the Arduino measures the duration of the pulse received on the Echo pin. The distance to the object is calculated based on the time it takes for the pulse to return.
The formula used is,
Distance = (Time × Speed of Sound) / 2
Remember that the speed of sound is approximately 343 meters per second (or 0.0343 cm per microsecond).
Once the distance is calculated, the LCD is initialized to prepare it for displaying text. This is done in the setup phase of the Arduino code. The Arduino converts the calculated distance to centimeters and sends this information to the LCD. The LCD displays the distance, updating in real-time as the distance changes.
Here, the below example code for distance measurement which helps to display the distance on the display.
Arduino Distance Measurement with HC-SR04 and 16x2 LCD
This example demonstrates how to use an HC-SR04 ultrasonic sensor to measure distance and display the result on a 16x2 LCD display connected to an Arduino Uno.
Arduino Code
Copy the code below into your Arduino IDE and upload it to your Arduino Uno:
#include <LiquidCrystal.h>
// Initialize the LCD with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
// HC-SR04 pins
const int trigPin = 9;
const int echoPin = 10;
void setup() {
// Start the LCD and set the number of columns and rows
lcd.begin(16, 2);
lcd.print("Distance:");
// Initialize the HC-SR04 pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Clear the trigger pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigger pin high for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echo pin
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance
float distance = duration * 0.0344 / 2; // Convert to centimeters
// Clear the LCD and print the distance
lcd.setCursor(0, 1);
lcd.print("Dist: ");
lcd.print(distance);
lcd.print(" cm");
// Wait for a short period before taking another measurement
delay(500);
}
Make sure to connect the components as described in the instructions above. Once everything is set up, the distance measured by the ultrasonic sensor will be displayed on the LCD.
Read Also:
Ultrasonic Sensor HC SR04 Pinout, Connection, Wiring Diagram
Reviewed by Author
on
August 19, 2024
Rating: