The HC-SR04 from AZDelivery is a widely used ultrasonic sensor that provides a non-contact distance measurement function. It operates by emitting ultrasonic waves at a frequency of 40 kHz and measures the time taken for the echo to return after bouncing off an object. This sensor is commonly used in robotics, obstacle avoidance systems, distance measurement applications, and various automation projects.
Pin Number | Pin Name | Description |
---|---|---|
1 | VCC | Power supply (5V DC) |
2 | TRIG | Trigger input (10µs pulse to start measurement) |
3 | ECHO | Echo output (pulse width represents distance) |
4 | GND | Ground |
// Define the connections to the Arduino
const int trigPin = 9;
const int echoPin = 10;
void setup() {
// Initialize the serial communication
Serial.begin(9600);
// Define the TRIG pin as an Output and the ECHO pin as an Input
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Clear the TRIG pin by setting it LOW
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Trigger the sensor by setting the TRIG pin HIGH for 10 microseconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read the ECHO pin; the duration is the time (in microseconds) from sending
// the signal to receiving its echo
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance (in cm) based on the speed of sound (34300 cm/s)
// and the time it took for the echo to return. We divide by 2 because the
// sound wave travels to the object and back.
int distance = duration * 0.034 / 2;
// Print the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Delay between measurements
delay(1000);
}
Q: Can the HC-SR04 sensor be used outdoors? A: The HC-SR04 can be used outdoors but may be less reliable in extreme weather conditions. It is not waterproof, so it should be protected from water and moisture.
Q: What is the maximum and minimum range of the HC-SR04 sensor? A: The maximum range is approximately 4 meters, and the minimum range is 2 centimeters.
Q: How can I increase the accuracy of the sensor? A: Ensure that the sensor is placed in a stable environment, away from wind and noise. Also, consider averaging multiple readings to reduce the effect of any anomalies.
Q: Is it possible to use multiple HC-SR04 sensors together? A: Yes, but ensure that each sensor's trigger and echo pins are controlled separately to avoid crosstalk and interference between sensors.
This documentation provides a comprehensive guide to using the HC-SR04 ultrasonic sensor with an Arduino UNO. For further assistance or technical support, please contact the manufacturer or refer to the community forums dedicated to Arduino and electronics enthusiasts.