Ultrasonic Sensor (HC-SR04): Pinout, Code, Common Faults

· 3 min read · Sensors Beginner12+

Author: Tekko BD Team

How the HC-SR04 ultrasonic sensor measures distance, its four pins, the Arduino code that turns microseconds into cm, and five fixes for bad readings.

A smart dustbin, an obstacle-avoiding robot, a car's parking sensor: inside all three sits roughly the same part, an ultrasonic sensor. The one you will meet in hobby projects is the HC-SR04. It works like a bat. It fires a 40 kHz burst of sound, times how long the echo takes to come back, and reports the distance in centimeters.

The two round "eyes" on the front do the work. One is a speaker (T) that sends the burst, the other a microphone (R) that hears the echo. Sound moves at 343 m/s in air. It travels out and back, so distance = time × 343 ÷ 2.

What you need

  • One HC-SR04 sensor
  • Any Arduino board
  • Four jumper wires
HC-SR04 Ultrasonic Distance SensorHC-SR04 Ultrasonic Distance Sensor৳140View details
Arduino UNO R3Arduino UNO R3৳910View details

Prices can change; check Tekko for the latest.

Ultrasonic sensor pinout

HC-SR04 pinout: VCC, Trig, Echo, GND
HC-SR04 pins from the left: VCC, Trig, Echo, GND
PinFunctionArduino
VCC5V power5V
Trigtrigger the burstdigital pin 9
Echoecho pulse outdigital pin 10
GNDgroundGND

Note

On an ESP32, do not feed the Echo pin straight in — it outputs 5V and ESP32 pins tolerate 3.3V. Put a divider on Echo (e.g. 1kΩ + 2kΩ) and the level drops to ≈3.3V.

Once it is wired, compare your build with this drawing from Tekko Sim. The whole circuit runs free in the browser.

Tekko Sim ultrasonic-alarm circuit: HC-SR04 with an Uno
ultrasonic-alarm: how the HC-SR04 connects to an Uno

The ultrasonic sensor code

cpp
const int TRIG = 9;
const int ECHO = 10;

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  Serial.begin(9600);
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);          // at least 10 µs = "measure now"
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);

  long duration = pulseIn(ECHO, HIGH, 30000);  // wait max 30 ms
  int cm = duration * 0.0343 / 2;

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

pulseIn() measures how long Echo stays HIGH, in microseconds. 343 m/s is 0.0343 cm/µs. Divide by two for the round trip.

Verify it works

Open the Serial Monitor at 9600 baud. It prints a distance about three times a second. Bring your hand toward the sensor slowly: 20, then 10, then 5 cm. Point it at a wall and the number should sit steady at the wall's distance.

Common problems

  1. Always 0 or tiny numbers — Trig and Echo are swapped. Trig goes to the pin your code sets as OUTPUT.
  2. pulseIn returns 0 — nothing within range (over 4 m), or nothing in front at all. Treat the 30 ms timeout as "saw nothing" and handle it in code.
  3. Soft targets vanish — cloth, foam and curtains soak up the sound. Test on a hard surface.
  4. Wrong readings on an angle — at 45° the echo bounces away. Face the surface squarely.
  5. Odd numbers with metal behind or beside the sensor — leave free space around the module. Hard objects hugging it can muddy the echo.

Specification

SpecValue
Power5V, ≈15 mA
Range2–400 cm
Effective cone≈15°
Frequency40 kHz
ProtocolTrig 10 µs pulse → Echo pulse width

Worth knowing

  • For outdoors or wet use, take the JSN-SR04T waterproof version. Its probe sits on a separate cable.
  • Several HC-SR04s in one circuit can hear each other. Space the measurements 30 ms apart (the delay in the code above does it), or run them in groups.

FAQ

Why does it fail under 2 cm? The receiver needs a moment after the burst before it can listen. An echo from under about 2 cm comes back before it is ready. Every ultrasonic sensor has this blind zone.

How much is it? We do not print prices on the blog because they change. Check the sensor page or ask on WhatsApp.

The reading wobbles a little each time. Is that normal? Up to ±3 mm, yes. Take the median of five readings in a row to steady the number.

More from Tekko

Sources

  • Arduino pulseIn() reference — arduino.cc

Try it free before you buy

This project runs free in Tekko Sim, right in your browser — before you spend a taka.

Ultrasonic Sensor Arduino (HC-SR04) — Run in Sim

#sensor#arduino#distance#science-fair