The HC-SR05 is an ultrasonic sensor designed for distance measurement. It operates by emitting an ultrasonic wave and measuring the time it takes for the echo to return. This time delay is then used to calculate the distance to an object. The HC-SR05 is widely used in various applications, including robotics, obstacle detection, and distance measurement systems.
Parameter | Value |
---|---|
Operating Voltage | 5V DC |
Operating Current | 15mA |
Frequency | 40kHz |
Max Range | 4 meters |
Min Range | 2 cm |
Measuring Angle | 15 degrees |
Trigger Input Pulse Width | 10µs |
Echo Output Pulse Width | Proportional to distance |
Pin Name | Description |
---|---|
VCC | Power supply (5V) |
Trig | Trigger input (10µs pulse to initiate measurement) |
Echo | Echo output (pulse width proportional to distance) |
GND | Ground |
// HC-SR05 Ultrasonic Sensor - Arduino UNO Example Code
const int trigPin = 9; // Trigger pin connected to digital pin 9
const int echoPin = 10; // Echo pin connected to digital pin 10
void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 baud rate
pinMode(trigPin, OUTPUT); // Set trigPin as an output
pinMode(echoPin, INPUT); // Set echoPin as an input
}
void loop() {
long duration, distance;
// Clear the trigPin by setting it LOW
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Set the trigPin HIGH for 10 microseconds to send out the pulse
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the echoPin, which returns the duration of the pulse
duration = pulseIn(echoPin, HIGH);
// Calculate the distance (duration / 2) * speed of sound (34300 cm/s)
distance = (duration / 2) * 0.0343;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500); // Wait for 500 milliseconds before the next measurement
}
No Output or Incorrect Readings:
Inconsistent Measurements:
Q: What is the maximum range of the HC-SR05? A: The maximum range is 4 meters.
Q: What is the minimum range of the HC-SR05? A: The minimum range is 2 cm.
Q: Can the HC-SR05 be used outdoors? A: While it can be used outdoors, it is recommended to protect it from direct exposure to harsh weather conditions.
Q: How accurate is the HC-SR05? A: The HC-SR05 has an accuracy of approximately 3mm.
By following this documentation, users can effectively integrate the HC-SR05 ultrasonic sensor into their projects, ensuring accurate and reliable distance measurements.