The HC-SR04 is an ultrasonic distance sensor that uses sonar to measure the distance to an object. It emits a high-frequency sound wave and measures the time it takes for the echo to return, allowing it to calculate the distance based on the speed of sound. This sensor is widely used in robotics, automation, and obstacle detection systems due to its accuracy, affordability, and ease of use.
Explore Projects Built with UNLTRASONIC SENSOR HC - SR04
Use Cirkit Designer to design, explore, and prototype these projects online. Some projects support real-time simulation. Click "Open Project" to start designing instantly!
This circuit features an Arduino Mega 2560 microcontroller interfaced with an HC-05 Bluetooth Module and an HC-SR04 Ultrasonic Sensor. The HC-05 is powered by the Arduino's VIN pin and is grounded to the Arduino's GND, enabling wireless communication capabilities. The HC-SR04 is powered by the Arduino's 5V output and uses two digital PWM pins (D7 for TRIG and D6 for ECHO) to measure distances via ultrasonic waves.
This circuit features an Arduino UNO microcontroller interfaced with an HC-SR04 Ultrasonic Sensor and an HC-05 Bluetooth module. The Arduino is configured to trigger the ultrasonic sensor to measure distance and communicate the data wirelessly via the HC-05 module. Power is supplied to both the sensor and the Bluetooth module from the Arduino's 5V output, and ground connections are shared among all components.
This circuit features a STM32 Nucleo F303RE microcontroller interfaced with three HC-SR04 ultrasonic sensors for distance measurement and a 20x4 LCD display over I2C for data output. Additionally, there is a WS2812 RGB LED strip controlled by the microcontroller for visual feedback. The power supply provides a common 5V to the LCD, ultrasonic sensors, LED strip, and the microcontroller's +5V input, with all components sharing a common ground.
This circuit features an Arduino UNO microcontroller connected to an HC-SR04 ultrasonic sensor, a Tower Pro SG90 servo motor, and a GPS NEO 6M module. The ultrasonic sensor's ECHO and TRIG pins are interfaced with the Arduino's digital pins for distance measurement, while the servo motor is controlled via another digital pin. The GPS module communicates with the Arduino through serial pins, and two LEDs with series resistors are connected to digital pins for indication purposes.
The HC-SR04 sensor is designed for precise distance measurement and operates within a specific range of environmental conditions. Below are its key technical details:
Parameter
Value
Operating Voltage
5V DC
Operating Current
15 mA
Measuring Range
2 cm to 400 cm (0.02 m to 4 m)
Measuring Angle
15°
Accuracy
±3 mm
Frequency
40 kHz
Dimensions
45 mm x 20 mm x 15 mm
Pin Configuration and Descriptions
The HC-SR04 sensor has four pins, as described in the table below:
Pin
Name
Description
1
VCC
Power supply pin. Connect to 5V DC.
2
Trig
Trigger pin. Sends a 10 µs pulse to initiate distance measurement.
3
Echo
Echo pin. Outputs a pulse proportional to the distance of the detected object.
4
GND
Ground pin. Connect to the ground of the power supply.
Usage Instructions
How to Use the HC-SR04 in a Circuit
Power the Sensor: Connect the VCC pin to a 5V power source and the GND pin to ground.
Trigger the Sensor: Send a 10 µs HIGH pulse to the Trig pin to initiate a measurement.
Read the Echo: Measure the duration of the HIGH pulse on the Echo pin. This duration corresponds to the time taken for the sound wave to travel to the object and back.
Calculate Distance: Use the formula below to calculate the distance:
[
\text{Distance (cm)} = \frac{\text{Pulse Duration (µs)} \times 0.034}{2}
]
The factor 0.034 represents the speed of sound in cm/µs, and the division by 2 accounts for the round trip of the sound wave.
Important Considerations and Best Practices
Ensure the sensor is mounted securely and aligned properly for accurate measurements.
Avoid placing the sensor near ultrasonic noise sources, as they may interfere with its operation.
Use a capacitor (e.g., 10 µF) across the VCC and GND pins to stabilize the power supply.
The sensor works best in environments with minimal temperature fluctuations, as the speed of sound varies with temperature.
Example Code for Arduino UNO
Below is an example of how to use the HC-SR04 sensor with an Arduino UNO:
// Define pins for the HC-SR04 sensor
const int trigPin = 9; // Trig pin connected to digital pin 9
const int echoPin = 10; // Echo pin connected to digital pin 10
void setup() {
// Initialize serial communication for debugging
Serial.begin(9600);
// Set pin modes
pinMode(trigPin, OUTPUT); // Trig pin as output
pinMode(echoPin, INPUT); // Echo pin as input
}
void loop() {
// Send a 10 µs pulse to the Trig pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the pulse on the Echo pin
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in cm
float distance = (duration * 0.034) / 2;
// Print the distance to the Serial Monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Wait before the next measurement
delay(500);
}
Troubleshooting and FAQs
Common Issues and Solutions
No Output or Incorrect Readings
Cause: Loose or incorrect wiring.
Solution: Double-check all connections, ensuring the VCC, GND, Trig, and Echo pins are connected properly.
Fluctuating Distance Measurements
Cause: Electrical noise or unstable power supply.
Solution: Add a decoupling capacitor (e.g., 10 µF) across the VCC and GND pins.
Sensor Not Detecting Objects
Cause: Object is outside the sensor's range or at an angle.
Solution: Ensure the object is within the 2 cm to 400 cm range and aligned with the sensor's measuring angle.
Echo Pin Always HIGH
Cause: Faulty sensor or incorrect trigger pulse.
Solution: Verify the trigger pulse duration is exactly 10 µs. Replace the sensor if the issue persists.
FAQs
Q: Can the HC-SR04 measure distances through transparent materials like glass?
A: No, the HC-SR04 relies on sound waves, which do not pass through transparent materials like glass effectively.
Q: What is the maximum sampling rate of the HC-SR04?
A: The HC-SR04 requires approximately 60 ms for each measurement, allowing for a maximum sampling rate of about 16 measurements per second.
Q: Can I use the HC-SR04 with a 3.3V microcontroller?
A: While the HC-SR04 operates at 5V, it can be interfaced with a 3.3V microcontroller using a voltage divider on the Echo pin to protect the microcontroller's input.
Q: Does temperature affect the accuracy of the HC-SR04?
A: Yes, the speed of sound varies with temperature. For precise measurements, consider compensating for temperature variations.