Cirkit Designer Logo
Cirkit Designer
Your all-in-one circuit design IDE
Home / 
Component Documentation

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

Image of HC-SR04 Ultrasonic Sensor
Cirkit Designer LogoDesign 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 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 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!
Arduino UNO Based Ultrasonic Distance Measurement with HC-SR04 and Bluetooth Communication via HC-05
Image of hc sr`: A project utilizing HC-SR04 Ultrasonic Sensor in a practical application
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.
Cirkit Designer LogoOpen Project in Cirkit Designer
Arduino UNO and HC-SR04 Ultrasonic Sensor Distance Measurement System
Image of Task 1: A project utilizing HC-SR04 Ultrasonic Sensor in a practical application
This circuit uses an Arduino UNO to interface with an HC-SR04 Ultrasonic Sensor for distance measurement. The Arduino provides power to the sensor and reads the echo signal on pin D7, while triggering the sensor via pin D8. The provided code is a basic template for further development.
Cirkit Designer LogoOpen Project in Cirkit Designer
STM32F103C8T6 and HC-SR04 Ultrasonic Sensor Distance Measurement System
Image of hscr04: A project utilizing HC-SR04 Ultrasonic Sensor in a practical application
This circuit interfaces an HC-SR04 Ultrasonic Sensor with an STM32F103C8T6 microcontroller. The microcontroller powers the sensor and reads distance measurements by triggering the sensor and receiving the echo signal.
Cirkit Designer LogoOpen Project in Cirkit Designer
Arduino Mega 2560 Bluetooth-Controlled Ultrasonic Distance Measurement
Image of circuitcycle: A project utilizing HC-SR04 Ultrasonic Sensor in a practical application
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.
Cirkit Designer LogoOpen Project in Cirkit Designer

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 hc sr`: 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.
Cirkit Designer LogoOpen Project in Cirkit Designer
Image of Task 1: A project utilizing HC-SR04 Ultrasonic Sensor in a practical application
Arduino UNO and HC-SR04 Ultrasonic Sensor Distance Measurement System
This circuit uses an Arduino UNO to interface with an HC-SR04 Ultrasonic Sensor for distance measurement. The Arduino provides power to the sensor and reads the echo signal on pin D7, while triggering the sensor via pin D8. The provided code is a basic template for further development.
Cirkit Designer LogoOpen Project in Cirkit Designer
Image of hscr04: A project utilizing HC-SR04 Ultrasonic Sensor in a practical application
STM32F103C8T6 and HC-SR04 Ultrasonic Sensor Distance Measurement System
This circuit interfaces an HC-SR04 Ultrasonic Sensor with an STM32F103C8T6 microcontroller. The microcontroller powers the sensor and reads distance measurements by triggering the sensor and receiving the echo signal.
Cirkit Designer LogoOpen Project in Cirkit Designer
Image of circuitcycle: 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.
Cirkit Designer LogoOpen Project in Cirkit Designer

Common Applications and Use Cases

  • Obstacle detection in robotics
  • Distance measurement in automation systems
  • Liquid level sensing
  • Proximity detection in security systems
  • Parking assistance systems

Technical Specifications

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)
Accuracy ±3 mm
Measuring Angle 15°
Operating Frequency 40 kHz
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 and Descriptions

The HC-SR04 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

The HC-SR04 sensor is simple to use and can be integrated into a variety of circuits. Below are the steps to use it effectively:

Connecting the HC-SR04

  1. Connect the VCC pin to a 5V power supply.
  2. Connect the GND pin to the ground of the power supply.
  3. Connect the Trig pin to a digital output pin of your microcontroller (e.g., Arduino).
  4. Connect the Echo pin to a digital input pin of your microcontroller.

Measuring Distance

  1. Send a 10 µs HIGH pulse to the Trig pin to trigger the sensor.
  2. The sensor will emit an ultrasonic wave and set the Echo pin HIGH.
  3. Measure the duration of the HIGH signal on the Echo pin. This duration corresponds to the time taken for the sound wave to travel to the object and back.
  4. Use the formula below to calculate the distance: [ \text{Distance (cm)} = \frac{\text{Time (µ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.

Example Code for Arduino UNO

Below is an example Arduino sketch to measure distance using the HC-SR04:

// Define pins for the HC-SR04 sensor
const int trigPin = 9; // Trigger 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 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 signal 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);
}

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 these can interfere with measurements.
  • The sensor's measuring angle is 15°, so ensure there are no obstructions within this cone for accurate results.
  • Use a capacitor (e.g., 10 µF) across the VCC and GND pins to filter out power supply noise.

Troubleshooting and FAQs

Common Issues and Solutions

  1. No Output or Incorrect Readings

    • Ensure the sensor is connected to a 5V power supply.
    • Verify the wiring and ensure the Trig and Echo pins are connected to the correct microcontroller pins.
    • Check for loose connections or damaged wires.
  2. Fluctuating Distance Measurements

    • Ensure there are no moving objects or noise sources near the sensor.
    • Add a capacitor across the power supply pins to stabilize the voltage.
  3. Sensor Not Responding

    • Confirm that the Trig pin is receiving a 10 µs HIGH pulse.
    • Test the sensor with a different microcontroller or power supply to rule out hardware issues.

FAQs

Q: Can the HC-SR04 measure distances less than 2 cm?
A: No, the minimum measurable distance is 2 cm. Objects closer than this may not be detected accurately.

Q: Can the HC-SR04 be powered with 3.3V?
A: The HC-SR04 is designed for 5V operation. While it may work at 3.3V, the performance and accuracy may be compromised.

Q: How can I increase the accuracy of the sensor?
A: Use the sensor in a stable environment, avoid obstructions within the measuring angle, and ensure a clean power supply.

Q: Can the HC-SR04 detect transparent objects?
A: The HC-SR04 may struggle to detect transparent objects like glass, as ultrasonic waves can pass through them or reflect poorly.