Sensors & Detectors
Ultrasonic Sonar
2cm - 400cm Range
3mm Accuracy
HC-SR04 Ultrasonic Distance Sensor - Pinout, Calculation & 3D STL Bracket
Drag to rotate | Scroll to zoom | Double-click to reset
Advertisement
Module Overview & How It Works
The HC-SR04 is a popular, cost-effective ultrasonic ranging module providing non-contact measurement from 2 cm to 400 cm with a precision down to 3 mm. The module includes an ultrasonic transmitter transducer, a receiver transducer, and a control circuit.
To measure distance, the microcontroller sends a short 10μs high pulse to the Trig pin. The sensor automatically transmits an 8-cycle sonic burst at 40 kHz. When the reflected sound waves bounce back off an obstacle, the Echo pin goes HIGH for a duration proportional to the round-trip distance traveled by sound.
Technical Specifications
| Parameter | Specification |
|---|---|
| Operating Voltage | 5V DC (Note: Echo output is 5V TTL) |
| Operating Current | 15 mA |
| Ultrasonic Frequency | 40 kHz |
| Max Range | 400 cm (4 meters) |
| Min Range | 2 cm |
| Measuring Angle | 15 degrees |
| Trigger Input Signal | 10μs TTL pulse |
Pinout Table & Voltage Divider for 3.3V MCUs (ESP32)
| Pin Name | Function | Arduino Uno Connection | ESP32 Connection (3.3V) |
|---|---|---|---|
| VCC | 5V Power Input | 5V | VIN / 5V rail |
| Trig | Trigger Input (10μs pulse) | Digital Pin 9 | GPIO 5 (Direct) |
| Echo | Echo Output (5V Pulse) | Digital Pin 10 | GPIO 18 (via 1k/2k voltage divider) |
| GND | Ground | GND | GND |
Arduino Sample Code (Distance in cm & inches)
HC_SR04_Distance.ino
C++ / Arduino
const int trigPin = 9;
const int echoPin = 10;
void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Clear trigger
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Send 10 microsecond pulse
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Read Echo duration
long duration = pulseIn(echoPin, HIGH);
// Calculate distance: sound speed is ~0.0343 cm/microsecond
float distanceCm = duration * 0.0343 / 2.0;
Serial.print("Distance: ");
Serial.print(distanceCm);
Serial.println(" cm");
delay(200); // 5Hz sampling rate
}
3D Printing & Enclosure Recommendations
Mounting the HC-SR04 firmly is critical to prevent acoustic echo false-triggers caused by mechanical wobble:
- Transducer Hole Diameter: The two metal transducer barrels have a standard diameter of 16.0mm. Our 3D model features a 16.3mm opening for a clean friction fit without fracturing the solder joints.
- Servo Mount Compatibility: The base includes mounting bosses designed to screw directly into SG90 or MG90S micro servo horns for obstacle-avoiding radar swiveling.