The HC-SR04 is an ultrasonic distance sensor that uses sonar to measure the distance to an object. It emits ultrasonic waves and calculates the time it takes for the echo to return, providing accurate distance measurements. This sensor is widely used in robotics, automation, and obstacle detection systems due to its reliability and ease of use.
Explore Projects Built with Sensor - UltrassonicoHC - 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 is designed for environmental monitoring and response, featuring sensors for temperature, humidity, distance, and soil moisture, with actuation through a servomotor and audio feedback. It is powered by a solar-charged battery system, indicating outdoor or remote deployment with renewable energy utilization.
This circuit features an Arduino UNO microcontroller interfaced with two HC-SR04 ultrasonic sensors for distance measurement, a 4-channel relay module for controlling external devices, and an EZ-SFX amplifier connected to two loudspeakers for audio output. The system is powered by a Polymer Lithium Ion Battery and includes basic setup and loop code for the Arduino.
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 (4 meters)
Measuring Angle
15°
Accuracy
±3 mm
Ultrasonic 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 a 5V DC source.
2
Trig
Trigger pin. A 10 µs HIGH pulse initiates the ultrasonic signal.
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
The HC-SR04 sensor is simple to use and can be integrated into a variety of circuits. Below are the steps to use the sensor effectively:
Connecting the HC-SR04 to an Arduino UNO
Wiring:
Connect the VCC pin to the 5V pin on the Arduino.
Connect the GND pin to the GND pin on the Arduino.
Connect the Trig pin to a digital I/O pin (e.g., pin 9).
Connect the Echo pin to another digital I/O pin (e.g., pin 10).
Arduino Code:
Use the following code to measure distance with the HC-SR04 sensor:
// Define pins for the HC-SR04 sensor
const int trigPin = 9; // Trigger pin connected to Arduino pin 9
const int echoPin = 10; // Echo pin connected to Arduino 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 HIGH pulse to the Trig pin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the echo pulse
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in centimeters
long 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);
}
Important Considerations and Best Practices
Ensure the sensor is powered with a stable 5V DC supply for accurate readings.
Avoid placing the sensor in environments with excessive noise or vibrations, as this may interfere with the ultrasonic signal.
The sensor's measuring angle is 15°, so ensure there are no obstructions within this cone for accurate measurements.
Use a resistor divider or logic level shifter if connecting the Echo pin to a microcontroller operating at 3.3V logic levels.
Troubleshooting and FAQs
Common Issues and Solutions
No Output or Incorrect Readings:
Cause: Improper wiring or loose connections.
Solution: Double-check all connections and ensure the sensor is powered correctly.
Unstable or Fluctuating Measurements:
Cause: Electrical noise or interference.
Solution: Use decoupling capacitors near the sensor's power pins to reduce noise.
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 4 m range and within the 15° detection cone.
Echo Pin Voltage Too High for Microcontroller:
Cause: Direct connection to a 3.3V logic microcontroller.
Solution: Use a voltage divider or level shifter to step down the voltage.
FAQs
Q1: Can the HC-SR04 measure distances less than 2 cm?
A1: No, the sensor's minimum range is 2 cm. Objects closer than this may not be detected accurately.
Q2: Can I use the HC-SR04 with a 3.3V microcontroller?
A2: Yes, but you must use a logic level shifter or resistor divider for the Echo pin to avoid damaging the microcontroller.
Q3: How can I improve the accuracy of the sensor?
A3: Use the sensor in a stable environment, avoid obstructions in the detection cone, and ensure proper power supply.
Q4: Can the HC-SR04 detect transparent objects?
A4: The sensor may struggle to detect transparent objects like glass, as ultrasonic waves may pass through or reflect unpredictably.