HC-SR04 Ultrasonic Distance Sensor

HC-SR04 Ultrasonic Distance Sensor image

View on Amazon

Overview

The HC-SR04 is a versatile ultrasonic distance sensor capable of measuring distances up to 4 meters with high precision. It is widely used in robotics and automation applications due to its low cost and simple operation. The sensor's compact design and efficient performance make it an excellent choice for both hobbyist and professional projects.

About HC-SR04 Ultrasonic Distance Sensor

The HC-SR04 is a budget-friendly ultrasonic sensor designed for precise distance measurement. Using ultrasonic sound waves, it accurately detects objects between 2 cm and 400 cm, making it ideal for obstacle detection, robotics, and automation projects.

Key Features

  • Wide Measurement Range – Detects distances from 2 cm to 400 cm.
  • Ultrasonic Sound Waves – Provides reliable and accurate distance calculations.
  • Trigger & Echo Mechanism – Simple operation with digital pulse signals.
  • Compatible with ESP32 & Arduino – Easy integration into microcontroller-based projects.

With its low cost, ease of use, and reliable performance, the HC-SR04 is a top choice for DIY electronics and robotic applications. 🚀

Where to Buy

Get Your HC-SR04

HC-SR04 Ultrasonic Distance Sensor

Starting from

1.5$ per unit

Amazon com
Amazon.com Ships worldwide
Amazon de
Amazon.de Ships to EU
Aliexpress
AliExpress Best value option

Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.

Technical Specs

HC-SR04 Specifications

Complete technical specification details for HC-SR04 Ultrasonic Distance Sensor

📊 Technical Parameters

Measurement Range 2 cm to 400 cm
Resolution 0.3 cm
Accuracy ±3 mm
Operating Voltage 5V DC
Trigger Pulse Duration 10 µs
Interface Trigger/Echo
Pin Configuration

HC-SR04 Pinout

The HC-SR04 features a simple 4-pin design for power and trigger/echo ultrasonic measurement.

Visual Pinout Diagram

Pinout Diagram Primary
HC-SR04 Ultrasonic Distance Sensor pinout
4
Total Pins

Pin Types

Power
2
Communication
1
Control
1

Quick Tips

Requires 5V power - most ESP32 boards provide this,⚠️ ECHO pin outputs 5V - use voltage divider for 3.3V ESP32

💡

Measures 2cm to 400cm with ±3mm accuracy,🔊 Uses ultrasonic sound waves (40kHz)

⏱️

Measurement takes about 10-25ms

Pin Descriptions

Pin NameTypeDescriptionNotes
1 VCC
Power

Power supply input (5V required)

Requires stable 5V power supply

2 GND
Power

Ground connection

Connect to ESP32 ground

3 TRIG
Control

Trigger input pin

Send 10µs pulse to initiate measurement

4 ECHO
Communication

Echo output pin

Returns pulse width proportional to distance

Connection Guide

Wiring HC-SR04 to ESP32

Connect the HC-SR04 with voltage divider for ECHO pin to protect 3.3V ESP32 GPIO.

Visual Wiring Diagram

Wiring Diagram Recommended
HC-SR04 Ultrasonic Distance Sensor wiring with ESP32
5
Connections

Connection Status

Required
5

Protocol

Trigger/Echo

Pin Connections

HC-SR04 PinConnectionESP32 PinDescription
1 VCC Required
5V

5V power supply required

2 GND Required
GND

Ground connection

3 TRIG Required
GPIO5

Trigger input (can handle 3.3V logic)

4 ECHO Required
GPIO18

Echo output (use voltage divider)

5 ECHO (Divider) Required
1kΩ + 2kΩ

Voltage divider to convert 5V to 3.3V

⚠️

ECHO pin outputs 5V - can damage 3.3V ESP32 GPIO

🔧

Use voltage divider: 1kΩ (ECHO to GPIO) + 2kΩ (GPIO to GND)

💡

TRIG pin accepts 3.3V logic directly

🎯

Send 10µs HIGH pulse on TRIG to start measurement

⏱️

ECHO pulse width in µs ÷ 58 = distance in cm

Help & Support

HC-SR04 Troubleshooting

Common issues and solutions to help you get your sensor working

Common Issues

Debugging Tips

Code Examples

HC-SR04 Programming Examples

Ready-to-use code examples for different platforms and frameworks

Summary

Wrapping Up HC-SR04

The ESP32 HC-SR04 Ultrasonic Distance Sensor is a powerful distance sensor that offers excellent performance and reliability. With support for multiple development platforms including Arduino, ESP-IDF, ESPHome, PlatformIO, and MicroPython, it's a versatile choice for your IoT projects.

Best Practices

For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.

Safety First

Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.

Ready to Start Building?

Now that you have all the information you need, it's time to integrate the HC-SR04 into your ESP32 project and bring your ideas to life!

Similar Options

Explore Alternative Sensors

Looking for alternatives to the HC-SR04? Check out these similar sensors that might fit your project needs.

TOF050C Time-of-Flight Sensor

TOF050C Time-of-Flight Sensor

DISTANCE I2C

The TOF050C is a short-range laser-ranging sensor using the VL6180 chip, offering highly accurate distance measurements up to 50 cm. It is...

View Details
A02YYUW Waterproof Ultrasonic Distance Sensor

A02YYUW Waterproof Ultrasonic Distance Sensor

DISTANCE UART

The A02YYUW is a waterproof ultrasonic distance sensor ideal for outdoor and industrial applications. With a measuring range of up to 4.5...

View Details
VL53L0X Time-of-Flight Distance Sensor

VL53L0X Time-of-Flight Distance Sensor

DISTANCE I2C

The VL53L0X is an advanced Time-of-Flight distance sensor offering accurate, laser-based measurements over a range of 30 mm to 2,000 mm. It...

View Details