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

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

Image of HC-SR04 Ultrasonic Distance Sensor (Wokwi Compatible)Design 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!
Image of Arduino Nano-Based Ultrasonic Distance Measurement System with Audio Alerts: 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.

Open Project in Cirkit Designer

Common Applications and Use Cases

Technical Specifications

Key Technical Details

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

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

Solutions and Tips for Troubleshooting

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.