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

How to Use HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible): Examples, Pinouts, and Specs

Image of HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible)
Cirkit Designer LogoDesign with HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible) in Cirkit Designer

Introduction

The HC-SR04 Ultrasonic Distance Sensor is a popular and cost-effective solution for measuring distance without contact. It operates by emitting ultrasonic sound waves and then listening for an echo once these waves bounce off an object. This sensor is widely used in robotics for obstacle avoidance, in security systems, and for automatic distance measurement in various DIY projects.

Explore Projects Built with HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible)

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 (Wokwi Compatible) 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
Raspberry Pi 3B Ultrasonic Distance Sensor
Image of Deteksi jarak 15 cm: A project utilizing HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible) in a practical application
This circuit interfaces a Raspberry Pi 3B with an HC-SR04 Ultrasonic Distance Sensor. The Raspberry Pi provides power to the sensor and uses GPIO pins to trigger the sensor and read the echo signal, enabling distance measurement functionality.
Cirkit Designer LogoOpen Project in Cirkit Designer
Arduino Nano-Based Ultrasonic Distance Measurement System with Audio Alerts
Image of ploutot bob: A project utilizing HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible) in a practical application
This circuit features an Arduino Nano microcontroller interfaced with multiple HC-SR04 ultrasonic distance sensors and piezo buzzers. The sensors are used to measure distances, and their trigger and echo pins are connected to various digital and analog pins on the Arduino. The piezo buzzers are driven by digital pins on the Arduino to provide audible feedback, possibly as a response to the distance measurements. Power is supplied to the circuit through a USB power source or a Lipo battery, with connections indicating that these power sources may be used interchangeably.
Cirkit Designer LogoOpen Project in Cirkit Designer
ESP32-Based Ultrasonic Distance Measurement System with GSM Reporting
Image of notification sms: A project utilizing HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible) in a practical application
This circuit is designed to measure distances with three ultrasonic sensors and transmit the data via GSM using an ESP32 microcontroller. It is powered by a battery with a voltage regulation provided by an XL4015 buck converter.
Cirkit Designer LogoOpen Project in Cirkit Designer

Explore Projects Built with HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible)

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 (Wokwi Compatible) 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 Deteksi jarak 15 cm: A project utilizing HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible) in a practical application
Raspberry Pi 3B Ultrasonic Distance Sensor
This circuit interfaces a Raspberry Pi 3B with an HC-SR04 Ultrasonic Distance Sensor. The Raspberry Pi provides power to the sensor and uses GPIO pins to trigger the sensor and read the echo signal, enabling distance measurement functionality.
Cirkit Designer LogoOpen Project in Cirkit Designer
Image of ploutot bob: A project utilizing HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible) in a practical application
Arduino Nano-Based Ultrasonic Distance Measurement System with Audio Alerts
This circuit features an Arduino Nano microcontroller interfaced with multiple HC-SR04 ultrasonic distance sensors and piezo buzzers. The sensors are used to measure distances, and their trigger and echo pins are connected to various digital and analog pins on the Arduino. The piezo buzzers are driven by digital pins on the Arduino to provide audible feedback, possibly as a response to the distance measurements. Power is supplied to the circuit through a USB power source or a Lipo battery, with connections indicating that these power sources may be used interchangeably.
Cirkit Designer LogoOpen Project in Cirkit Designer
Image of notification sms: A project utilizing HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible) in a practical application
ESP32-Based Ultrasonic Distance Measurement System with GSM Reporting
This circuit is designed to measure distances with three ultrasonic sensors and transmit the data via GSM using an ESP32 microcontroller. It is powered by a battery with a voltage regulation provided by an XL4015 buck converter.
Cirkit Designer LogoOpen Project in Cirkit Designer

Common Applications and Use Cases

  • Robotics: Obstacle detection and navigation
  • Security systems: Proximity detection
  • Automotive: Parking assistance systems
  • DIY projects: Distance measurement and interactive installations

Technical Specifications

Key Technical Details

  • Operating Voltage: 5V DC
  • Current Consumption: 15 mA
  • Acoustic Emission Frequency: 40 kHz
  • Max Range: 4m
  • Min Range: 2cm
  • Resolution: 3mm
  • Measuring Angle: 15 degrees

Pin Configuration and Descriptions

Pin Number Pin Name Description
1 VCC Power supply (5V DC)
2 TRIG Trigger input (TTL pulse)
3 ECHO Echo output (TTL level signal)
4 GND Ground

Usage Instructions

How to Use the Component in a Circuit

  1. Power Supply: Connect the VCC pin to a 5V power supply and the GND pin to the ground.

  2. Triggering the Sensor: Send a 10µs TTL pulse to the TRIG pin to start the measurement.

  3. Receiving the Echo: Monitor the ECHO pin; the duration of the high signal will give you the time interval between sending and receiving the echo.

  4. Calculating Distance: Use the following formula to calculate the distance:

    Distance (in cm) = (Time x Speed of Sound (34300 cm/s)) / 2
    

Important Considerations and Best Practices

  • Ensure that the sensor is mounted on a stable surface to avoid false readings due to vibrations.
  • Avoid using the sensor in environments with high dust, humidity, or temperature extremes, as these can affect the accuracy.
  • The sensor should not be used to measure absorbent materials like cloth, as they may not reflect sound waves properly.
  • Keep the sensor away from noisy environments as this can interfere with the ultrasonic signals.

Example Code for Arduino UNO

#include <NewPing.h>

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the sensor.
#define MAX_DISTANCE 400 // Maximum distance we want to ping for (in centimeters).

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(9600); // Open serial monitor at 9600 baud to see ping results.
}

void loop() {
  delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert time into distance and print result (0 = outside set distance range)
  Serial.println("cm");
}

Troubleshooting and FAQs

Common Issues Users Might Face

  • Inaccurate Readings: Ensure that the sensor is not facing any obstacles that are too close or too far. Also, check for any electrical noise or interference.
  • No Readings: Verify that the wiring is correct and that the sensor is receiving the correct voltage. Also, ensure that the trigger pulse is being sent correctly.

Solutions and Tips for Troubleshooting

  • Check Connections: Double-check all connections, especially the VCC and GND pins, to ensure they are secure.
  • Avoid Obstacles: Make sure the path between the sensor and the object is clear of any obstructions.
  • Use Libraries: Utilize Arduino libraries like NewPing for easier implementation and more reliable readings.

FAQs

Q: What is the maximum range of the HC-SR04 sensor? A: The maximum range is approximately 4 meters, but optimal performance is found within 2 meters.

Q: Can the HC-SR04 sensor detect soft materials? A: Soft and absorbent materials may not reflect sound waves well, leading to less reliable readings.

Q: How can I increase the accuracy of the sensor? A: Ensure stable mounting, avoid interference, and use averaging of multiple readings to increase accuracy.