The HC-SR04 is an ultrasonic distance sensor that measures the distance to an object by emitting ultrasonic waves and calculating the time it takes for the echo to return. This sensor is widely used in robotics and automation for applications such as obstacle detection, distance measurement, and level sensing. Its ability to provide accurate distance readings makes it a popular choice for projects involving autonomous vehicles, drones, and various other electronic devices.
Specification | Value |
---|---|
Operating Voltage | 5V DC |
Current Consumption | 15 mA (max) |
Frequency | 40 kHz |
Measuring Range | 2 cm to 400 cm |
Accuracy | ±3 mm |
Beam Angle | 15 degrees |
Pin Number | Pin Name | Description |
---|---|---|
1 | VCC | Power supply (5V) |
2 | Trig | Trigger pin for initiating measurement |
3 | Echo | Echo pin for receiving the reflected signal |
4 | GND | Ground connection |
Wiring the Sensor:
Basic Circuit Diagram:
+5V ---- VCC (HC-SR04)
GND ---- GND (HC-SR04)
D2 ---- Trig (HC-SR04)
D3 ---- Echo (HC-SR04)
Here is a simple example code to use the HC-SR04 with an Arduino UNO:
#define TRIG_PIN 2 // Trigger pin connected to digital pin 2
#define ECHO_PIN 3 // Echo pin connected to digital pin 3
void setup() {
Serial.begin(9600); // Start serial communication
pinMode(TRIG_PIN, OUTPUT); // Set trigger pin as output
pinMode(ECHO_PIN, INPUT); // Set echo pin as input
}
void loop() {
long duration, distance;
// Clear the trigger
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
// Set the trigger high for 10 microseconds
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// Read the echo pin, return the sound wave travel time
duration = pulseIn(ECHO_PIN, HIGH);
// Calculate the distance (duration/2) * speed of sound (0.034 cm/us)
distance = (duration * 0.034) / 2;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(1000); // Wait for a second before the next measurement
}
Inaccurate Distance Readings:
No Response from the Sensor:
Sensor Not Powering On:
By following this documentation, users can effectively utilize the HC-SR04 ultrasonic distance sensor in their projects, ensuring accurate distance measurements and reliable performance.