HC-SR04 Ultrasonic Sensor: How It Works + Arduino Wiring

HC-SR04 Ultrasonic Sensor: How It Works + Arduino Wiring

Quick answer: The HC-SR04 (Rs.115) sends a 40kHz ping and times the echo. Four wires, no library, distance = echo pulse width / 58. Two catches: its Echo pin outputs 5V and will damage a 3.3V board without a divider, and its own record calls readings beyond 3m inaccurate despite a 400cm spec.

HC-SR04 Ultrasonic Sensor - the part this guide is about, in stock at Compoden
HC-SR04 Ultrasonic Sensor — the actual part this guide describes, photographed from our own stock.

Written and fact-checked by Compoden's engineering team, India. Every spec, pin and price below is read from our live catalogue record for the exact part named. The sketch was written for this guide and has not been compiled by us — it uses no external library, so every line is auditable. Published 18 August 2026 · Last updated 18 August 2026.

The HC-SR04 is the cheapest way to measure a distance without touching anything, and it is in nearly every beginner robot for good reason. It is also a sensor whose datasheet number and whose useful number are not the same, and whose most common failure is a 5V signal quietly cooking a 3.3V input pin. Both of those are in our own catalogue record for the part, and both are worth knowing before the module arrives.

HC-SR04 Ultrasonic Sensor — catalogue record, price checked August 2026
Spec Value
Price (India, COD) Rs.115
Operating voltage 5V only
Logic level 5V — Echo pin is NOT 3.3V safe
Current draw 15mA typical, 20mA peak
Frequency 40 kHz ultrasonic
Stated range 2 cm – 400 cm
Stated accuracy / resolution 3 cm / 0.3 cm
Minimum read interval 60 ms
Operating temperature -15 to 70 °C
Pins VCC, TRIG, ECHO, GND

Verdict in one line: excellent value for indoor obstacle detection inside 3 metres, and the wrong sensor for precision, for outdoors, or for a 3.3V board without a divider.

How does the HC-SR04 actually measure distance?

It shouts and times the echo. You hold the TRIG pin HIGH for 10 microseconds; the module emits a burst of 40kHz ultrasound, then raises its ECHO pin and holds it high for exactly as long as the sound takes to travel out and back. Your code measures that pulse width and converts it to a distance. Sound covers roughly 1 cm in 29 microseconds, and the pulse covers the distance twice — out and back — so microseconds divided by 58 gives centimetres. That is the entire algorithm, which is why no library is required: pulseIn() in the standard Arduino core does the timing for you. (The 29 µs/cm figure is textbook physics for the speed of sound near room temperature, not a value from our record.)

Why is the 400cm range not really 400cm?

Because our own record says so. The specification line reads 2cm-400cm, and the same record's known issues list "inaccurate readings beyond 3m". Both statements are true and they describe different things: the module will still return a number out to four metres, but the number stops being trustworthy at about three. That means roughly a quarter of the advertised range is detection rather than measurement — fine for "is something there", not fine for "how far". Design around 3 metres and treat anything past it as a hint. Stated accuracy is 3cm anyway, so this was never a precision instrument at any distance.

HC-SR04 advertised range against the range its own record calls accurate The HC-SR04 is specified from 2 to 400 centimetres, but the same catalogue record lists readings beyond 300 centimetres as inaccurate — about a quarter of the advertised range is unreliable. HC-SR04 range: specified vs trustworthy — drawn to scale Specified2 – 400 cm Trustworthy2 – ~300 cm Bar widths proportional: 480px = 400cm, so 300cm = 360px. The 120px gap is about 25% of the advertised range that the record's own known-issues list calls inaccurate. Stated accuracy across the whole usable band is 3 cm.
The specification and the known-issues list on the same catalogue record disagree by about a metre. Both figures are quoted from that record: range "2cm-400cm", known issue "inaccurate readings beyond 3m".

How do you wire an HC-SR04 to an Arduino Uno?

Four wires, and on a 5V Uno you can connect it directly — the sensor is a 5V part talking to a 5V board, which is the one combination that needs no protection. TRIG and ECHO can be any plain digital pins; D8 and D7 are good choices because neither carries PWM, bus or LED duty on the Uno. Our record also recommends a 10µF capacitor across VCC and GND for stable operation, which is worth adding if readings jitter, and is close to essential once a motor shares the same supply.

HC-SR04 to Arduino Uno — wiring
Sensor pin Arduino Uno pin Note
VCC 5V 15mA typical — the 5V rail, not a GPIO
TRIG D8 Any plain digital pin
ECHO D7 Direct on a 5V Uno; needs a divider on a 3.3V board
GND GND Common ground

What happens if you wire the Echo pin to an ESP32?

You put 5V into a 3.3V input, and that is the single most common way this sensor kills a board. Our record is explicit: "Echo pin outputs 5V, not 3.3V tolerant". The ESP32, ESP8266 and every other 3.3V board need the ECHO line divided down before it reaches a GPIO, and our record even names the divider: 10kΩ and 20kΩ. Wire ECHO through the 10kΩ to the GPIO, and from that same GPIO node through the 20kΩ to GND — that puts about two-thirds of 5V, roughly 3.3V, on the pin. TRIG is safe unprotected in the other direction, since a 3.3V output is comfortably read as HIGH by the sensor. There is no software workaround for this; it is a hardware fix or a damaged pin.

What code reads an HC-SR04?

Under thirty lines and no library, though our record names NewPing if you want one (it adds median filtering and a cleaner timeout API). The sketch below uses only the standard Arduino core. Note the timeout in pulseIn(): without one, an out-of-range reading blocks your program for a second waiting for an echo that never comes.

// HC-SR04 distance in centimetres. No library required.
// Board: Arduino Uno R3 (5V) - ECHO connects directly.
// On a 3.3V board, divide ECHO with 10k/20k first.
// Wiring: VCC->5V, GND->GND, TRIG->D8, ECHO->D7.

const int TRIG_PIN = 8;
const int ECHO_PIN = 7;

void setup() {
  Serial.begin(9600);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  digitalWrite(TRIG_PIN, LOW);
}

void loop() {
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);            // the 10us trigger pulse
  digitalWrite(TRIG_PIN, LOW);

  // Timeout 30000us is about 5m round trip; 0 means no echo.
  unsigned long us = pulseIn(ECHO_PIN, HIGH, 30000UL);

  if (us == 0) {
    Serial.println("no echo - out of range or nothing in front");
  } else {
    Serial.print(us / 58.0, 1);     // 58us per cm, out and back
    Serial.println(" cm");
  }
  delay(100);                       // record: 60ms minimum between reads
}

Why are the readings erratic or stuck?

Four causes, all recorded on the part. Reading too fast is the first — our record sets a 60ms minimum interval between readings, and polling faster lets the previous ping's echoes contaminate the next measurement. Power instability is the second, which is what the recommended 10µF capacitor across VCC and GND addresses; a servo or motor on the same rail will produce exactly this symptom. Acoustic conditions are the third: soft, angled or fabric surfaces scatter the ping instead of returning it, so a reading of zero often means "the sound never came back" rather than "nothing is there" — which is why the sketch above prints a distinct message for that case rather than a distance of 0cm. Temperature is the fourth: the speed of sound rises with air temperature, so the same echo time means a slightly different distance on a hot afternoon than on a cold morning. The HC-SR04 has no compensation for this at all.

Which ultrasonic sensor should you buy?

For indoor robots, parking sensors and level monitors inside 3 metres, the HC-SR04 Ultrasonic Sensor (Rs.115) is the right default and hard to beat on price. If temperature drift or precision matters — a tank gauge you will trust, a measurement rather than a detection — the US-100 module (Rs.240) adds built-in temperature compensation and a stated ±1mm accuracy against the HC-SR04's 3cm, with a UART mode as well as pulse-width. For outdoors, a water tank, or anywhere wet, the Waterproof Ultrasonic Distance Sensor (Rs.280) has an IP67 head and a 20-600cm range — note its 20cm blind zone and that its cable joint is not fully sealed. Mounting one on a robot chassis? The HC-SR04 mounting bracket is Rs.25. All available cash on delivery across India.

What should you read next?

Putting one on a robot? Our line follower robot build and the reverse parking sensor build both use distance sensing for real. Check the Arduino Uno pinout reference before you claim D7 and D8 for something else.

Want the wiring and code for your exact board? Open Soldr, describe your build in one sentence, and it will produce the pin plan and the sketch — including the divider if your board is 3.3V.

Frequently asked questions

HC-SR04 ko ESP32 se kaise connect karein?

HC-SR04 ka ECHO pin 5V nikalta hai aur ESP32 ka GPIO sirf 3.3V leta hai - seedha jodne se pin kharaab ho sakta hai. ECHO aur GPIO ke beech 10k resistor lagayein, aur usi GPIO point se 20k resistor GND tak - isse voltage 5V se ghat kar lagbhag 3.3V ho jaata hai. TRIG ko seedha jod sakte hain kyunki 3.3V output sensor ke liye kaafi hai. VCC ko 5V par hi rakhein - yeh sensor 3.3V par nahi chalta.

Do I need a library for the HC-SR04?

No. The sensor is read by pulsing TRIG for 10 microseconds and timing the ECHO pulse with pulseIn(), both of which are in the standard Arduino core. Divide the pulse width in microseconds by 58 to get centimetres. Our catalogue record does name NewPing as the usual library if you want median filtering and a cleaner timeout API, but nothing about this sensor requires one.

Why does my HC-SR04 read 0 or give erratic distances?

Usually one of four things, all listed on our record for the part. Reading faster than the 60ms minimum interval lets the previous ping's echoes contaminate the next one. An unstable supply — a motor or servo on the same rail — causes jitter, which is what the recommended 10µF capacitor across VCC and GND fixes. Soft, angled or fabric surfaces scatter the ping so no echo returns, which reads as 0 rather than as "far away". And beyond about 3 metres the record calls the readings inaccurate regardless.

How accurate is the HC-SR04?

Our catalogue record states 3cm accuracy with 0.3cm resolution, over a specified 2-400cm range whose top metre the same record calls inaccurate. It also has no temperature compensation, and the speed of sound changes with air temperature, so the same echo time reads slightly differently on a hot day. For a measurement you need to trust, the US-100 at Rs.240 states ±1mm with built-in temperature compensation.

What is the price of an HC-SR04 in India?

At Compoden, the HC-SR04 Ultrasonic Sensor is Rs.115 and its ABS mounting bracket is Rs.25. The temperature-compensated US-100 is Rs.240 and the IP67 waterproof ultrasonic sensor is Rs.280. All available with cash on delivery across India. Prices checked August 2026.

Back to blog

Open this project in the Soldr app → What is Soldr.ai → parts, wiring and code for what this guide builds