How to Use HC-SR04 Ultrasonic Sensor with Arduino
How to Use HC-SR04 Ultrasonic Sensor with Arduino
This guide walks you through wiring an HC-SR04 ultrasonic distance sensor to Arduino UNO and reading the distance between the sensor and an object. The data is displayed on Serial Monitor using Arduino’s built-in pulseIn() function, so no external libraries are needed.
Required Components
- Arduino UNO R3 with USB cable
- HC-SR04 Ultrasonic Distance Sensor
- MB-102 Breadboard (830 points)
- Jumper wires M-M 20 cm (about 4 pieces)
- Jumper wires M-F 20 cm (optional)
Wiring HC-SR04 to Arduino UNO
| HC-SR04 | Arduino UNO |
|---|---|
| VCC | 5V |
| GND | GND |
| Trig | Pin 13 |
| Echo | Pin 12 |
How HC-SR04 works:
- Arduino sends a short ultrasonic pulse through the Trig pin
- The sound wave travels, hits an object, and bounces back to the sensor’s Echo pin
- Arduino measures the travel time and calculates the distance
Distance formula: distance = (time × 0.034) / 2
Where 0.034 is the speed of sound in cm/μs, and divided by 2 because the time includes both the outbound and return trip.
Arduino Code for HC-SR04 Distance Measurement
// Define the pins connected to HC-SR04
const int trigPin = 13;
const int echoPin = 12;
void setup() {
Serial.begin(9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}
void loop() {
// Send Trig pulse: 10 microseconds HIGH
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the Echo pulse width in microseconds
long duration = pulseIn(echoPin, HIGH);
// Convert duration to distance in centimeters
float distance = (duration * 0.034) / 2;
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
delay(500);
}
Adjustable parts for your project:
- Change
trigPinandechoPinto match your actual wiring - Adjust
delay(500)to change the measurement interval (faster intervals may cause cross-talk between consecutive readings) - For distance in meters, use
distance = (duration * 0.017) / 2instead
How to Upload and View Results
- Open Arduino IDE and paste the code above
- Select board: Tools → Board → Arduino Uno
- Select port: Tools → Port and choose the COM port your board is connected to
- Click the Upload button (right arrow →)
- Wait for Done uploading message, then open Serial Monitor
- Set Baud Rate to 9600
- Distance values will print every 500 milliseconds
Testing the HC-SR04 Sensor
After uploading, place a hand or object in front of the sensor and slowly move it:
- Move closer → distance value decreases
- Move farther → distance value increases
If the values stay at 0 or show abnormal numbers, double-check that VCC, GND, Trig, and Echo connections match the wiring table.
Precautions When Using HC-SR04
- Beam angle is about 15 degrees. Very small or highly angled objects may not reflect enough sound back to the sensor.
- Maximum range is approximately 4 meters. The sensor returns 0 for objects beyond that range.
- Measurement frequency when using
pulseIn()in default mode, wait at least 60 milliseconds between measurements to avoid old echoes interfering with new ones. - Multiple sensors must be triggered one at a time, not simultaneously, or the sound pulses will interfere with each other.
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย