Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

How to Use HC-SR04 Ultrasonic Sensor: Examples, Pinouts, and Specs

Image of HC-SR04 Ultrasonic SensorDesign with HC-SR04 Ultrasonic Sensor in Cirkit Designer

Introduction

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 typically ranging from 2 cm to 4 meters. This sensor is widely used in robotics, obstacle detection, and distance measurement applications due to its simplicity, affordability, and reliability.

Explore Projects Built with HC-SR04 Ultrasonic Sensor

Use Cirkit Designer to design, explore, and prototype these projects online. Some projects support real-time simulation. Click "Open Project" to start designing instantly!
Image of Arduino UNO Based Ultrasonic Distance Measurement with HC-SR04 and Bluetooth Communication via HC-05: A project utilizing HC-SR04 Ultrasonic Sensor in a practical application
Arduino UNO Based Ultrasonic Distance Measurement with HC-SR04 and Bluetooth Communication via HC-05

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.

Open Project in Cirkit Designer
Image of Arduino Mega 2560 Bluetooth-Controlled Ultrasonic Distance Measurement: A project utilizing HC-SR04 Ultrasonic Sensor in a practical application
Arduino Mega 2560 Bluetooth-Controlled Ultrasonic Distance Measurement

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.

Open Project in Cirkit Designer

Common Applications

Technical Specifications

The HC-SR04 sensor operates by emitting ultrasonic waves at a frequency of 40 kHz and measuring the time it takes for the echo to return. Below are the key technical details:

Parameter Value
Operating Voltage 5V DC
Operating Current 15 mA
Measuring Range 2 cm to 400 cm
Measuring Angle 15°
Ultrasonic Frequency 40 kHz
Resolution 0.3 cm
Trigger Input Signal 10 µs TTL pulse
Echo Output Signal Pulse width proportional to distance
Dimensions 45 mm x 20 mm x 15 mm

Pin Configuration

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. Send a 10 µs HIGH pulse to initiate distance measurement.
3 Echo Echo pin. Outputs a pulse whose width corresponds to the measured distance.
4 GND Ground pin. Connect to the ground of the power supply.

Usage Instructions

How to Use the HC-SR04 in a Circuit

  1. Power the Sensor: Connect the VCC pin to a 5V power supply and the GND pin to ground.
  2. Trigger the Sensor: Send a 10 µs HIGH pulse to the Trig pin to initiate a measurement.
  3. Read the Echo: Measure the duration of the HIGH pulse on the Echo pin. This duration is proportional to the distance of the object.
  4. Calculate Distance: Use the formula below to calculate the distance: [ \text{Distance (cm)} = \frac{\text{Pulse Duration (µs)}}{58} ] Alternatively, for distance in inches: [ \text{Distance (in)} = \frac{\text{Pulse Duration (µs)}}{148} ]

Important Considerations

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() {
  pinMode(trigPin, OUTPUT); // Set Trig pin as output
  pinMode(echoPin, INPUT);  // Set Echo pin as input
  Serial.begin(9600);       // Initialize serial communication at 9600 baud
}

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 HIGH pulse on the Echo pin
  long duration = pulseIn(echoPin, HIGH);

  // Calculate the distance in centimeters
  float distance = duration * 0.034 / 2; // Speed of sound is ~0.034 cm/µs

  // Print the distance to the Serial Monitor
  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  delay(500); // Wait for 500 ms before the next measurement
}

Notes on the Code

Troubleshooting and FAQs

Common Issues

  1. No Output or Incorrect Readings

    • Cause: Improper wiring or loose connections.
    • Solution: Double-check all connections and ensure the sensor is powered correctly.
  2. Constant Maximum Distance (400 cm)

    • Cause: No object detected within range or object is too close (<2 cm).
    • Solution: Ensure there is an object within the sensor's range and avoid placing objects too close.
  3. Fluctuating Readings

    • Cause: Environmental noise or reflective surfaces.
    • Solution: Use a stable mounting setup and avoid placing the sensor near reflective or vibrating surfaces.
  4. Echo Pin Voltage Too High

    • Cause: Direct connection to a 3.3V microcontroller without level shifting.
    • Solution: Use a resistor divider or level shifter to step down the voltage.

FAQs

Q: Can the HC-SR04 detect transparent objects?
A: The sensor may struggle to detect transparent objects like glass due to poor ultrasonic wave reflection.

Q: Can I use the HC-SR04 with a 3.3V microcontroller?
A: Yes, but you must use a level shifter or resistor divider for the Echo pin to avoid damaging the microcontroller.

Q: What is the maximum sampling rate of the HC-SR04?
A: The sensor requires a minimum delay of 60 ms between measurements, allowing for a maximum sampling rate of approximately 16 Hz.

Q: How do I improve accuracy?
A: Use a stable power supply, avoid environmental noise, and ensure proper alignment of the sensor.