Same-day dispatch before 2 PM · Free delivery over ₹999 · GST invoices Get the appPrices incl. GST
Geekobot
Snap & SendNEWGeeko AIWhere's myOrder
Log in
Products
  • Projects
    • Component guidesHow parts work, with code you can run
    • Learning pathsFrom your first LED to your first robot
    • Workshops & eventsOnline sessions for makers and classes
    • ElectrolarkFree guided builds, simulator and certificates
    • Students & teachersStudent discount and classroom help
    • CommunityShare builds and ask other makers
    • CalculatorsOhm's law, LED resistor, 555 and 100+ more
    • Circuit builderWire parts and run the circuit, powered by Electrolark
    • Geeko AIAsk what to buy or how to wire it
    • Snap & SendPhotograph a part or list, we find it
    • Part finderAnswer a few questions, get the right part
    • Compatibility checkWill these two parts work together?
    • Compare partsSpecs side by side
    • Business & bulk ordersGST invoices, credit and volume prices
    • Request a quotationSend your list, get a quote
    • Classroom kitsPer-student kits and lab bundles
    • Gift cardsFor students, teams and events
  • Atal Tinkering Lab
BOM to cart
Geekobot

Components, kits and know-how for makers, students, engineers, labs and businesses across India.

Ask Geeko AIOrders, parts and projects, any timeEmail supportReply within 1 working dayGet the Geekobot appAdd it to your home screen, free

Follow us

Shop

  • Boards & SBCs
  • Sensors & Modules
  • IoT & Wireless
  • Drone & FPV
  • 3D Printing
  • Filaments & Resins
  • Motors & Drivers
  • Power & Batteries
  • Semiconductors
  • Passive
  • Displays & LEDs
  • Switches & Relays
  • Wires & Cables
  • Prototyping
  • Tools & Test
  • Robotics
  • STEM Kits
  • Atal Tinkering Lab
  • Mechanical Parts
  • Audio

Orders

  • Track your order
  • Help & support
  • Shipping and delivery
  • Refunds and cancellation
  • Contact us

Learn

  • Projects
  • Geeko AI project assistant
  • Learning paths
  • Guides
  • Calculators
  • Electrolark: free guided builds

Part tools

  • Part finder
  • Compare parts
  • Will it work with my board?

Institutions

  • Classroom kits
  • Atal Tinkering Lab
  • Students & teachers
  • Workshops & Events

Business

  • Business & education
  • Request a quotation
  • BOM bulk upload
  • Quick order by SKU

Company

  • About Geekobot
  • Community
  • Gift cards
  • Terms & conditions
  • Privacy policy
  • Product directory
  • Sitemap

© 2026 Geekobot, a trade name of OLatus Systems Private Limited. All rights reserved.

20, 1st Floor, Neeladri Complex, 10th Cross, Sampige Rd, 2nd Main, Malleshwaram, Bengaluru, Karnataka 560003 · GSTIN 29AACCO0462F1ZM

+91 91810 15607 · connect@geekobot.in · Monday to Saturday, 10 AM to 2 PM IST

UPI · CARDS · NETBANKING · GST INVOICES

ShopProjectsGeeko AIGuides
Compare 0 parts →
Log in

Shop by category

Shop all components
  • Boards & SBCs1,910 products
    • Alternative Single Board Computers
    • Arduino Ecosystem
    • FPGA & Logic Boards
    • Microcontroller & Wireless Boards
    View all
  • Sensors & Modules4,701 products
    • Biomedical & Heart Rate
    • Current, Voltage & Power
    • Force, Weight & Pressure
    • Gas, Weather & Environmental
    View all
  • IoT & Wireless2,314 products
    • RF Modules
    • RFID & NFC
    • Antennas & RF Accessories
    • Cameras & Smart Vision
    View all
  • Drone & FPV3,558 products
    • Flight Control & Navigation
    • FPV Video & Optics
    • Frames & Structural Parts
    • Motors & Propulsion
    View all
  • 3D Printing2,204 products
    • 3D Printers
    • 3D Scanners & Digitizers
    • Laser Engravers & Cutters
    • Material Management & Upgrades
    View all
  • Filaments & Resins1,472 products
    • Filaments
    • Photopolymer Resins
    View all
  • Motors & Drivers463 products
    • BLDC Motor Drivers
    • DC & Geared Motors
    • DC Motor Drivers
    • Linear Actuators & Solenoids
    View all
  • Power & Batteries557 products
    • Battery Holders & Connectors
    • Breadboard Power
    • Chargers & BMS
    • DC-DC Converters
    View all
  • Semiconductors295 products
    • Diodes & Rectifiers
    • Interface & RF Encoder ICs
    • Logic ICs (74xx)
    • Logic ICs (CD4000)
    View all
  • Passive67 products
    • Capacitors
    • Circuit Protection
    • Crystals & Oscillators
    • Inductors
    View all
  • Displays & LEDs572 products
    • 7-Segment & Dot Matrix
    • Addressable LEDs
    • Character LCDs
    • Graphic LCDs
    View all
  • Switches & Relays184 products
    • Keypads & Touch
    • Push Buttons & Tactile
    • Relay Modules
    • Relays
    View all
  • Wires & Cables233 products
    • Board-to-Wire Connectors
    • Crimp & Splice
    • Headers & Dupont
    • Heat Shrink & Sleeving
    View all
  • Prototyping101 products
    • Breadboards
    • Interface Modules
    • PCBs & Perfboards
    View all
  • Tools & Test717 products
    • Bench Power Supplies
    • Hand Tools
    • Measuring Tools
    • Meters & Testers
    View all
  • Robotics180 products
    • Humanoid & Legged Robots
    • Robot Chassis & Platforms
    • Robotic Arms & Grippers
    • Wheels & Casters
    View all
  • STEM Kits268 products
    • Arduino Learning Kits
    • Coding & IoT Kits
    • Early Coding (Ages 3-8)
    • Invention Kits
    View all
  • Atal Tinkering Lab196 products
    • Package 1: Electronics, Robotics, IoT and Sensors
    • Package 2: Rapid Prototyping Tools
    • Package 3: Mechanical, Electrical and Measurement Tools
    • Package 4: Power Supply, Accessories and Safety Equipment
    View all
  • Mechanical Parts212 products
    • Bearings
    • Brackets & Mounts
    • Couplers
    • Fasteners & Standoffs
    View all
  • Audio109 products
    • Amplifier Modules
    • Buzzers & Sirens
    • Speakers
    View all
  1. Home
  2. ›Guides
  3. ›Sensors
  4. ›HC-SR04 Ultrasonic Sensor
Step 4 of 18 in Sensors and measurementPart of a learning path.
Next

Component guide · Beginner · 3 min read

HC-SR04 Ultrasonic Sensor

How It Works, Wiring and Code

By Geekobot Learning Team · Updated 24 Oct 2026

I'm using
Wiring, code and the Run it panels follow your board.
On this page
Short answerHow an ultrasonic distance sensor worksHC-SR04 wiring for Arduino and ESP32Arduino code for the HC-SR04How to get accurate readingsHC-SR04 vs HC-SR04P vs waterproof JSN-SR04TTry itStep by stepCommon mistakesFAQsParts and next steps
Short answer

The HC-SR04 measures distance from about 2 cm to 4 m by sending a 40 kHz ultrasonic pulse and timing the echo. You trigger it with a 10 µs pulse on TRIG, then read how long ECHO stays high: distance in cm is that time in microseconds divided by 58. It runs on 5 V, so the echo pin needs a voltage divider on 3.3 V boards.

Key specs at a glance

Supply
5 V DC, about 15 mA
Range
2 cm to 400 cm (reliable to about 2.5 to 3 m indoors)
Resolution
About 3 mm
Beam angle
About 15 degrees
Frequency
40 kHz
Trigger
10 µs high pulse on TRIG
Output
ECHO pulse, width proportional to distance, 5 V level
Pins
VCC, TRIG, ECHO, GND
01

How an ultrasonic distance sensor works

One transducer on the HC-SR04 acts as a speaker and the other as a microphone. When TRIG goes high for 10 µs, the module sends a burst of eight 40 kHz pulses and sets ECHO high. When the reflected sound returns, ECHO goes low. Sound travels at about 343 m/s in air at 20 °C, or 0.0343 cm/µs, and the pulse travels there and back, so:

distance (cm) = echo time (µs) x 0.0343 / 2, which simplifies to echo time / 58.

02

HC-SR04 wiring for Arduino and ESP32

HC-SR04 pinArduino UnoESP32 / Pico (3.3 V)
VCC5V5V (VIN / VBUS)
TRIGD9Any output GPIO (3.3 V is enough to trigger)
ECHOD10Through a 1 kΩ / 2 kΩ voltage divider to an input GPIO
GNDGNDGND
VCC
Arduino Uno:
5V
ESP32 / Pico (3.3 V):
5V (VIN / VBUS)
TRIG
Arduino Uno:
D9
ESP32 / Pico (3.3 V):
Any output GPIO (3.3 V is enough to trigger)
ECHO
Arduino Uno:
D10
ESP32 / Pico (3.3 V):
Through a 1 kΩ / 2 kΩ voltage divider to an input GPIO
GND
Arduino Uno:
GND
ESP32 / Pico (3.3 V):
GND
03

Arduino code for the HC-SR04

This sketch prints the distance twice a second. The timeout in pulseIn stops the code hanging when nothing is in range.

Read distance from an HC-SR04 · Arduino C++Verify & upload in Electrolark
const int TRIG = 9;
const int ECHO = 10;

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

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);

  long us = pulseIn(ECHO, HIGH, 30000);   // 30 ms timeout, about 5 m
  if (us == 0) {
    Serial.println("Out of range");
  } else {
    Serial.print(us / 58.0);
    Serial.println(" cm");
  }
  delay(500);
}
04

How to get accurate readings

Aim at hard, flat surfaces.

Cloth, foam, fur and steep angles absorb or scatter the pulse.

Wait at least 60 ms between pings

so old echoes do not return as false readings.

Average or take the median of 3 to 5 readings

to remove spikes.

Correct for temperature

in precise work: the speed of sound rises by about 0.6 m/s per °C.

Keep two sensors from facing each other

or firing at the same time; they hear each other's pulses.

05

HC-SR04 vs HC-SR04P vs waterproof JSN-SR04T

SensorSupplyRangeChoose it for
HC-SR045 V2 to 400 cmArduino Uno projects, classroom kits
HC-SR04P / RCWL-16013 to 5.5 V2 to 400 cmESP32, Pico and battery builds, no divider needed
JSN-SR04T / AJ-SR04M5 V20 to 600 cmWater tanks, outdoor and parking sensors; waterproof probe
VL53L0X (laser ToF)2.8 to 5 V3 to 200 cmSmall targets, narrow beam, I2C
HC-SR04
Supply:
5 V
Range:
2 to 400 cm
Choose it for:
Arduino Uno projects, classroom kits
HC-SR04P / RCWL-1601
Supply:
3 to 5.5 V
Range:
2 to 400 cm
Choose it for:
ESP32, Pico and battery builds, no divider needed
JSN-SR04T / AJ-SR04M
Supply:
5 V
Range:
20 to 600 cm
Choose it for:
Water tanks, outdoor and parking sensors; waterproof probe
VL53L0X (laser ToF)
Supply:
2.8 to 5 V
Range:
3 to 200 cm
Choose it for:
Small targets, narrow beam, I2C
06

Try it: HC-SR04 ultrasonic distance calculator

Change the values and the diagram and results update as you type.

07

Measure distance with an HC-SR04 and Arduino Uno

You need: Arduino Uno, HC-SR04 sensor, 4 jumper wires, Breadboard (optional) · Tools: Computer with Arduino IDE, USB cable

Get the parts3 of 4 found in our store · ₹666

  • Arduino UnoArduino Uno R3 Development Board with USB Cable₹385
  • HC-SR04 sensorHC-SR04 Ultrasonic Sensor with Acrylic Mounting Bracket₹85
  • 4 jumper wiresDupont Jumper Wires (40 pcs)₹49
  • Breadboard (optional)Find this part
  1. 1
    Wire powerConnect VCC to 5V and GND to GND on the Uno.
  2. 2
    Wire signalsConnect TRIG to D9 and ECHO to D10.
  3. 3
    Upload codeUpload the sketch from this guide and open the Serial Monitor at 9600 baud.
  4. 4
    TestHold a book 10 to 100 cm in front of the sensor and check the printed distance with a ruler.
08

Common mistakes to avoid

  • !Connecting ECHO directly to an ESP32 or Pico pin; the 5 V pulse can damage it.
  • !Pinging too fast, which produces ghost readings from earlier echoes.
  • !Mounting the sensor behind a hole that is too small, so the housing reflects the pulse.
  • !Using pulseIn without a timeout, which freezes the loop for a second when nothing is in range.
  • !Expecting a reading under 2 cm; the sensor cannot hear an echo that soon.
09

Frequently asked questions

What is the range of the HC-SR04?

2 cm to 400 cm on paper. Indoors on typical targets, expect reliable readings to about 2.5 to 3 m.

Can I use the HC-SR04 with a 3.3 V board?

Power it from 5 V and put a voltage divider on ECHO, or buy the HC-SR04P version that runs on 3.3 V.

Can the HC-SR04 measure water level?

Yes, by pointing it down at the surface, but condensation will eventually damage it. A waterproof JSN-SR04T lasts much longer in tanks.

Why does my HC-SR04 always read 0 or a fixed value?

Check that TRIG and ECHO are not swapped, that the module has 5 V, and that you use a timeout. A reading stuck near the maximum usually means the echo is being absorbed or the target is at an angle.

Do I need a library for the HC-SR04?

No, pulseIn is enough. Libraries such as NewPing add timeouts, median filtering and support for many sensors.

Parts for this guide

  • 4-Channel Logic Level Converter (BSS138)
    ₹20View
  • Dupont Jumper Wires (40 pcs)
    ₹49View
Shop Motion & Position Sensors

Terms in this guide

UltrasonicSound above human hearing (over 20 kHz); the HC-SR04 uses 40 kHz.Time of flightMeasuring distance from how long a signal (sound or light) takes to bounce back.Voltage dividerTwo resistors in series that output a fixed fraction of the input voltage.Logic levelThe voltage that represents a digital high: 5 V on classic Arduinos, 3.3 V on ESP32, Pico and Raspberry Pi.PWMPulse-width modulation: switching a signal on and off quickly so its average sets brightness, speed or position.

Projects that use it

Put this into practice: 71 step-by-step builds in our project library use parts from this guide.

Beginner · 1 hourWater tank overflow alarmView projectBeginner · 1–2 hoursGarage parking sensorView projectBeginner · 2 hoursTouchless hand sanitiser dispenserView project
Browse all 71 projects

Keep learning

Component guideBeginnerArduino UnoThe Arduino Uno is a beginner-friendly microcontroller board that reads sensors and controls LEDs, motors and relays using simple C++ programs called sketches.5 min read · Boards & MicrocontrollersComponent guideBeginnerESP32 GuideThe ESP32 is a low-cost 3.3 V microcontroller from Espressif with built-in Wi-Fi and Bluetooth, used for IoT devices, smart-home controllers and web dashboards.5 min read · Boards & MicrocontrollersHow-toIntermediate3.3 V vs 5 V LogicYou need level shifting when a 5 V device sends signals to a 3.3 V board such as an ESP32 or Raspberry Pi Pico, because 5 V on their pins can damage them.4 min read · Prototyping & WiringComponent guideBeginnerServo Motors GuideA hobby servo is a small motor with a gearbox, position sensor and control circuit that moves to an angle you set with a pulse signal.3 min read · Motors & Motion

Sources

  • HC-SR04 user guide (Cytron / ElecFreaks module documentation)
  • VL53L0X datasheet (STMicroelectronics)

Written by the Geekobot team from manufacturer datasheets. Always check the datasheet for the exact part and variant you buy.

On this page
Short answerHow an ultrasonic distance sensor worksHC-SR04 wiring for Arduino and ESP32Arduino code for the HC-SR04How to get accurate readingsHC-SR04 vs HC-SR04P vs waterproof JSN-SR04TTry itStep by stepCommon mistakesFAQsParts and next steps
Need a hand?Ask Geeko AI which part fits your project, or talk to the Geekobot team.Ask Geeko AIHelp & support

Parts for this guide (Arduino Uno)3 items · ₹322

HC-SR04 Ultrasonic Range Finder Sensor Module for Arduino · Dupont Jumper Wires (40 pcs) · Uno R3 CH340G ATmega328p Compatible Development Board

Run itChange the inputs and watch the output

Move the object and the sketch's reading follows. Dividing by 58 assumes about 20 °C air; change the air temperature to see the error it causes.

1744 µsECHO high for
Distance30 cm
Serial monitor9600 baud
30.07 cm

A simulation of the sketch above, so you know what to expect. Real readings vary with your parts and wiring.

Ask the Community

Ask a question