Ultrasonic — Distance Sensor
Measure how far away an object is
Ultrasonic
Distance
Sensor
Timing
What you will learn
- How sonar / echolocation works in nature and in sensors
- How to use pulseIn() to measure a pulse width in microseconds
- How to convert microseconds to centimeters using the speed of sound
What you will need
| Component |
|---|
| 1× 9V battery |
| 1× HC-SR04 ultrasonic sensor |
| Breadboard |
| 4× jumper wires |
Step by step
Take it further
- Convert cm to inches: inches = cm / 2.54.
- Print the value as both a number and a bar (####) so you can see the size at a glance.
- Try the sensor at very long distances (5+ meters) — see how accuracy drops.
Practise it before you build it
Wire this circuit in the ThinkEdge Learn simulator first. The simulator runs a real Arduino sketch against a modified-nodal-analysis solver, so the LED on screen actually lights when the current is right — no hardware, no burnt components, and a class can try it in a browser.
Related projects
LDR — Automatic Night LightThe room gets dark, the LED turns on
DHT11 — Digital ThermometerRead the room temperature on your laptop
PIR — Motion-Activated LEDWalk past, the light turns on. Stand still, it goes off.
Sound Sensor — Loud Sound LightLED brightness follows the sound · quiet keeps it off
Arduino Nano — Parking Sensor4 distance zones — 1 slow low beep far, 2 fast high-pitch beeps close (like a real car reversing buzzer)
Smart Recycling BinUltrasonic sensor detects when the bin is full
Guides that use this project
8 guides on the site build this project as part of a longer explanation. The guide says why the circuit works; this page says how to wire it.
How to Learn RoboticsHow to learn robotics without a degree: the order to learn electronics, code and control in, how long each stage takes, and the projects that prove each one landed.
Robotics for BeginnersRobotics for beginners: five projects that take you from a blinking LED to a robot that steers itself, with the parts and the mistakes at each step.
Robotic Lawn Mowers: Boundary, Blades and BalanceHow robotic lawn mowers find the edge of the grass, why the blade motor causes brownouts, and what a $30 ultrasonic sensor can and cannot do.
Cleaning Robots: Bumpers, Cliff Sensors and the BrushHow a robot vacuum finds its way around a room, why cliff sensors matter more than the brush motor, and which Arduino projects teach each part of the loop.
Autonomous Vehicles: What the Car Actually SensesAutonomous vehicles fuse lidar, radar, cameras and ultrasonics into one picture of the road. Here is what each sensor is good at, and how to build the loop.
How to Learn ArduinoHow to learn Arduino properly: an eight-week plan from a blinking LED to real builds, with the parts to buy first and the mistake that slows most beginners down.
Tinkercad CircuitsTinkercad Circuits is a free browser simulator for Arduino. What it teaches well, where it stops, and why it is a good first step before a real board.
Drone Inspection and SurveillanceWhere drones genuinely pay for themselves: inspection of towers, roofs, pipelines and power lines. The sensor choices, the regulation and the real economics.
Where this project sits
It is also part of these lists, which is how a class usually finds it.
Try the full simulator with your class
103 guided circuit projects, 21 lessons and an in-browser Arduino simulator. The first 15 projects are free, no account needed. One licence covers a whole school.