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

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

Image of HC-SR04 Ultrasonic Distance Sensor
Cirkit Designer LogoDesign with HC-SR04 Ultrasonic Distance Sensor in Cirkit Designer

Introduction

The HC-SR04 is an ultrasonic distance sensor that measures distance by emitting ultrasonic waves and calculating the time it takes for the echo to return. It is widely used in robotics, automation, and IoT applications for obstacle detection, distance measurement, and proximity sensing. Its affordability, ease of use, and accuracy make it a popular choice for hobbyists and professionals alike.

Explore Projects Built with HC-SR04 Ultrasonic Distance 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 with HC-SR04 Ultrasonic Sensor and Buzzer Alert System
Image of ultrasonic: A project utilizing HC-SR04 Ultrasonic Distance Sensor in a practical application
This circuit consists of an HC-SR04 Ultrasonic Distance Sensor interfaced with an Arduino UNO for measuring distances. The Arduino is programmed to trigger the sensor and read the echo signal to determine the distance to an object. A buzzer is also connected to the Arduino and can be used to provide audible feedback based on the distance measurements.
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 Distance 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
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 Distance 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 Based Ultrasonic Distance Measurement with Buzzer Indicator
Image of echo loco: A project utilizing HC-SR04 Ultrasonic Distance Sensor in a practical application
This circuit is designed to measure distance using an HC-SR04 Ultrasonic Sensor connected to an Arduino UNO. The Arduino powers the sensor and processes its signal to determine the distance to an object. A buzzer is included and can be programmed to sound based on the measured distance, although the current code does not implement this functionality.
Cirkit Designer LogoOpen Project in Cirkit Designer

Explore Projects Built with HC-SR04 Ultrasonic Distance 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 ultrasonic: A project utilizing HC-SR04 Ultrasonic Distance Sensor in a practical application
Arduino UNO with HC-SR04 Ultrasonic Sensor and Buzzer Alert System
This circuit consists of an HC-SR04 Ultrasonic Distance Sensor interfaced with an Arduino UNO for measuring distances. The Arduino is programmed to trigger the sensor and read the echo signal to determine the distance to an object. A buzzer is also connected to the Arduino and can be used to provide audible feedback based on the distance measurements.
Cirkit Designer LogoOpen Project in Cirkit Designer
Image of Task 1: A project utilizing HC-SR04 Ultrasonic Distance 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 hc sr`: A project utilizing HC-SR04 Ultrasonic Distance 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 echo loco: A project utilizing HC-SR04 Ultrasonic Distance Sensor in a practical application
Arduino UNO Based Ultrasonic Distance Measurement with Buzzer Indicator
This circuit is designed to measure distance using an HC-SR04 Ultrasonic Sensor connected to an Arduino UNO. The Arduino powers the sensor and processes its signal to determine the distance to an object. A buzzer is included and can be programmed to sound based on the measured distance, although the current code does not implement this functionality.
Cirkit Designer LogoOpen Project in Cirkit Designer

Common Applications:

  • Obstacle detection in robotics
  • Distance measurement in automation systems
  • Proximity sensing in IoT devices
  • Liquid level measurement in tanks
  • Parking assistance systems

Technical Specifications

The HC-SR04 sensor operates by emitting ultrasonic waves at 40 kHz and measuring the time it takes for the echo to return after hitting an object. Below are its key technical details:

Parameter Value
Operating Voltage 5V DC
Operating Current 15 mA
Measuring Range 2 cm to 400 cm
Accuracy ±3 mm
Operating Frequency 40 kHz
Trigger Input Signal 10 µs TTL pulse
Echo Output Signal TTL pulse proportional to distance
Dimensions 45 mm x 20 mm x 15 mm

Pin Configuration:

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 duration 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:

  • 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 readings.
  • The sensor has a minimum range of 2 cm; objects closer than this may not be detected accurately.
  • Use a resistor divider or level shifter if interfacing with a 3.3V microcontroller.

Example Code for Arduino UNO:

Below is an example of how to use the HC-SR04 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 for Trig and Echo
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, 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 pulse on the Echo pin
  long duration = pulseIn(echoPin, HIGH);

  // Calculate the distance in centimeters
  float distance = duration / 58.0;

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

  // Wait for a short period before the next measurement
  delay(500);
}

Notes on the Code:

  • The pulseIn() function measures the duration of the HIGH pulse on the Echo pin.
  • The calculated distance is printed to the Serial Monitor for debugging or display purposes.
  • Adjust the delay() value in the loop() function to control the measurement frequency.

Troubleshooting and FAQs

Common Issues:

  1. No Output or Incorrect Readings:

    • Cause: Incorrect wiring or loose connections.
    • Solution: Double-check all connections and ensure the sensor is powered correctly.
  2. Unstable or Fluctuating Measurements:

    • Cause: Electrical noise or interference.
    • Solution: Use decoupling capacitors near the sensor's power pins and avoid noisy environments.
  3. Sensor Not Detecting Objects:

    • Cause: Object is outside the sensor's range or too small to reflect ultrasonic waves.
    • Solution: Ensure the object is within the 2 cm to 400 cm range and has a sufficient surface area.
  4. Echo Pin Always HIGH or LOW:

    • Cause: Faulty sensor or incorrect trigger signal.
    • Solution: Verify the trigger signal timing and replace the sensor if necessary.

FAQs:

Q1: Can the HC-SR04 measure distances through transparent materials like glass?
A1: No, the HC-SR04 relies on ultrasonic waves, which do not pass through transparent materials like glass effectively.

Q2: Can I use the HC-SR04 with a 3.3V microcontroller?
A2: Yes, but you need a level shifter or resistor divider to safely interface the Echo pin with the microcontroller.

Q3: What is the maximum measurement frequency of the HC-SR04?
A3: The sensor requires a minimum delay of 60 ms between measurements, allowing for a maximum frequency of approximately 16 Hz.

Q4: How can I improve measurement accuracy?
A4: Use a stable power supply, avoid ultrasonic interference, and ensure the sensor is mounted securely and aligned properly.