กลับหน้าหลัก
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

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)
Circuit diagram showing wiring between Arduino UNO and HC-SR04 with labeled pins VCC GND Trig and Echo on a breadboard

Wiring HC-SR04 to Arduino UNO

HC-SR04Arduino UNO
VCC5V
GNDGND
TrigPin 13
EchoPin 12

How HC-SR04 works:

  1. Arduino sends a short ultrasonic pulse through the Trig pin
  2. The sound wave travels, hits an object, and bounces back to the sensor’s Echo pin
  3. 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.

Diagram illustrating HC-SR04 working principle: Trig sends pulse outward, Echo receives the reflected wave back, with distance calculation shown

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 trigPin and echoPin to 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) / 2 instead

How to Upload and View Results

  1. Open Arduino IDE and paste the code above
  2. Select board: Tools → Board → Arduino Uno
  3. Select port: Tools → Port and choose the COM port your board is connected to
  4. Click the Upload button (right arrow →)
  5. Wait for Done uploading message, then open Serial Monitor
  6. Set Baud Rate to 9600
  7. Distance values will print every 500 milliseconds
Serial Monitor screenshot displaying "Distance: 25.4 cm" readings updating in real time

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

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

Project estimate

Want something like this? Open the estimate page.

The long form is now on a separate estimate page, so this article stays clean.

ความคิดเห็น

รีวิวจากคนใช้งานจริง

รีวิวจากลูกค้าและคนที่เคยใช้งาน

ถ้าเคยสั่งงาน เคยอ่านหน้านี้แล้วได้ประโยชน์ หรือมีข้อเสนอแนะ ฝากรีวิวไว้ได้เลย

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