กลับหน้าหลัก
views
How to Use HC-SR04 Ultrasonic Sensor with Arduino
Last updated on

How to Use HC-SR04 Ultrasonic Sensor with Arduino


How to Use HC-SR04 Ultrasonic Sensor with Arduino

The HC-SR04 is an ultrasonic distance sensor that works by emitting sound waves and measuring the time it takes for the echo to return. This principle allows for non-contact distance measurement.

The sensor measures distances from 2-400 centimeters with approximately ±3mm accuracy. This makes it suitable for various DIY projects like obstacle-avoiding robots, parking assist systems, or water level monitoring.

Components Needed

  • Arduino UNO or compatible board
  • HC-SR04 ultrasonic sensor
  • Jumper wires
  • Computer with Arduino IDE installed

Wiring HC-SR04 to Arduino

Circuit diagram showing HC-SR04 connected to Arduino UNO with Vcc→5V, Gnd→Gnd, Trig→13, Echo→12
HC-SR04Arduino
Vcc5V
GndGnd
TrigPin 13
EchoPin 12

The wiring is straightforward. Connect Vcc and Gnd to Arduino’s 5V and Gnd respectively. Trig pin triggers the ultrasonic burst, while Echo pin receives the return signal.

Arduino Code for HC-SR04 Distance Measurement

const int trigPin = 13;
const int echoPin = 12;

void setup() {
  Serial.begin(9600);
}

void loop() {
  long duration, cm;

  // Trigger ultrasonic burst
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);

  // Read the echo signal
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // Convert to centimeters
  cm = microsecondsToCentimeters(duration);

  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(100);
}

long microsecondsToCentimeters(long microseconds) {
  // Speed of sound is 340 m/s or 29 microseconds per centimeter
  // Round trip distance, so divide by 2
  return microseconds / 29 / 2;
}

How the Code Works

Sending the Burst

  • Set trigPin as OUTPUT
  • Send a 5 microsecond HIGH pulse to trigger HC-SR04 to emit ultrasonic waves

Receiving the Signal

  • Switch echoPin to INPUT mode
  • Use pulseIn to measure how long echoPin stays HIGH, which equals the round-trip travel time

Distance Calculation The speed of sound in air is approximately 340 meters per second, or about 29 microseconds per centimeter. Since this measures round-trip distance, we divide by 2 to get the actual distance.

Customizing the Code for Your Project

Changing Connected Pins If pins 13 or 12 are already in use, you can change to any available pins. Just update the trigPin and echoPin variable values to match your wiring.

Improving Measurement Stability Add delays after triggering to let HC-SR04 settle, or take multiple readings and average them to reduce noise.

Adding Range Checks

if (cm > 0 && cm < 400) {
  // Value is within valid range
  Serial.println(cm);
} else {
  Serial.println("Out of range");
}
Serial Monitor showing distance readings from HC-SR04

Practical Considerations

Surface Type Smooth surfaces perpendicular to the sensor give best results. Sound-absorbing materials like foam may not return any echo signal.

Detection Angle HC-SR04 has approximately 15-degree detection cone. Small objects or distant targets may not be detected reliably.

Interference Other ultrasonic sensors operating in similar frequencies may cause interference with readings.

Comparing HC-SR04 detection cone with various object sizes and distances

Reference Video

https://www.youtube.com/embed/pGK5jBbEJXs

อยากทำโปรเจคแบบนี้?

รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน

If you need Arduino project service or urgent IoT development, see full service details on the home page

จ้างทำโปรเจคเลย

ความคิดเห็น

Verified user reviews

รีวิวและความคิดเห็นจากผู้ใช้จริง

ล็อกอินด้วยบัญชีบนเว็บนี้แล้วให้คะแนนหรือคอมเมนต์ได้เลย ระบบเก็บผ่าน Supabase ไม่ต้องใช้ GitHub แล้ว

กำลังโหลดรีวิว...