Guide for HC-SR04 Ultrasonic Distance Sensor Module

HC-SR04 Ultrasonic Distance Sensor Module
Precise Non-Contact Distance Measurement for Arduino Projects
Introduction
The HC-SR04 ultrasonic sensor is a popular module for measuring distances from 2cm to 400cm with 3mm accuracy. Using sound waves beyond human hearing (40kHz), it provides reliable non-contact distance measurement for robotics, automation, and IoT applications.

HC-SR04 ultrasonic sensor
Key Features
Precise Measurement
2cm-400cm range with 3mm accuracy
Ultrasonic Technology
40kHz sound waves for reliable detection
Simple Interface
Only 4 pins (VCC, GND, Trig, Echo)
Easy Integration
Works with 5V microcontrollers
Technical Specifications
Operating Voltage | 5V DC |
---|---|
Working Current | 15mA |
Measuring Angle | 15° |
Resolution | 0.3cm |
Measuring Cycle | ≥50ms |
Dimensions | 45mm × 20mm × 15mm |
Pin Configuration
Pin | Label | Description | Arduino Connection |
---|---|---|---|
1 | VCC | Power (5V) | 5V |
2 | Trig | Trigger Input | Digital Pin |
3 | Echo | Echo Output | Digital Pin |
4 | GND | Ground | GND |
Note: The Echo pin outputs 5V which may need a voltage divider for 3.3V microcontrollers
Wiring with Arduino
// For 3.3V boards (ESP8266/ESP32):
// Echo → Voltage divider (5V to 3.3V)
Important: Avoid placing objects closer than 2cm for accurate readings
// Basic Connections: // VCC → 5V // GND → GND // Trig → D9 // Echo → D10
Basic Distance Measurement
// HC-SR04 Basic Example const int trigPin = 9; const int echoPin = 10; void setup() { Serial.begin(9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); } void loop() { // Clear trigger pin digitalWrite(trigPin, LOW); delayMicroseconds(2); // Send 10μs pulse digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); // Measure echo pulse duration long duration = pulseIn(echoPin, HIGH); // Calculate distance (cm) float distance = duration * 0.034 / 2; Serial.print("Distance: "); Serial.print(distance); Serial.println(" cm"); delay(100); }
Advanced Applications
Obstacle Avoidance
// Robot obstacle detection
if(distance < 20) { // 20cm threshold
stopMotors();
turnRight();
}
Liquid Level Monitoring
// Tank level measurement
float tankHeight = 50; // cm
float level = tankHeight - distance;
float percentage = (level/tankHeight)*100;
Multiple Sensors
// Array of sensors
const int sensors = 3;
const int trigPins[sensors] = {2,4,6};
const int echoPins[sensors] = {3,5,7};
void readAllSensors() {
for(int i=0; i<sensors; i++) {
// Add sensor reading logic
}
}
Moving Average Filter
// Smooth noisy readings
float readings[5];
int index = 0;
float getFilteredDistance() {
readings[index] = getDistance();
index = (index+1)%5;
float sum = 0;
for(int i=0; i<5; i++) {
sum += readings[i];
}
return sum/5;
}
Troubleshooting
Inconsistent Readings
- Ensure stable power supply (5V)
- Keep sensor perpendicular to target
- Add delay between measurements (≥50ms)
No Readings
- Check wiring connections
- Verify trigger pulse (10μs)
- Test with different objects
Incorrect Distance
- Calibrate speed of sound constant
- Check for acoustic interference
- Avoid soft/angled surfaces
Related Posts
MG90S Mini Digital 180° Servo
MG90S Mini Digital 180° Servo
Metal Gear, 2.2kg·cm Torque for RC and Robotics
Introduction
The MG90S is a compact di...
XKC-Y25-V Non-Contact Water Liquid Level Sensor
XKC-Y25-V Non-Contact Water Liquid Level Sensor
Capacitive Detection Without Physical Contact
Introduction
The XKC-Y...
Waterproof Ultrasonic Obstacle Sensor, Sensor with Separate Probe
+
Waterproof Ultrasonic Obstacle Sensor
Distance Measurement with Separate Waterproof Probe
Introduction
The Wa...
Water Level Depth Detection Sensor
Water Level Depth Detection Sensor
Liquid Measurement for Arduino and IoT Projects
Introduction
The Water Level Dept...
VL53L0X Purple Laser Distance Sensor Module
VL53L0X Laser Distance Sensor Module
High-Speed, High-Precision Time-of-Flight Distance Measurement
Introduction
The...
TCS34725 RGB Color Sensor Module
TCS34725 RGB Color Sensor Module
High-Accuracy Digital Color Detection with IR Filter
Introduction
The TCS34725 is a...
TCS3200 Color Sensor Module
TCS3200 Color Sensor Module
Precise RGB Color Detection for Arduino and Embedded Projects
Introduction
The TCS3200 C...
PN532 NFC RFID Read/Write Module V3 Kit
PN532 NFC RFID Read/Write Module V3 Kit
Advanced Near Field Communication for Arduino and Embedded Systems
Introducti...
HC-SR501 PIR Motion Sensor Module
HC-SR501 PIR Motion Sensor Module
Passive Infrared Detection for Security and Automation Projects
Introduction
The H...
Flex Sensor 5.6 cm (Detect Bending Motion)
Flex Sensor 5.6cm
Bend Detection Sensor for Arduino and Wearable Electronics Projects
Introduction
The 5.6cm...
ACS712 Current Sensor Module
ACS712 5A Current Sensor Module
Hall-Effect Based AC/DC Current Measurement for Arduino Projects
Introduction
The AC...
AS608 Optical Fingerprint Sensor Module
AS608 Optical Fingerprint Sensor Module
High-Precision Biometric Recognition for Arduino and Microcontroller Projects
...
Recent Comments