2 · Inputs & sensors
Ultrasonic range finder (HC-SR04)
Advanced20 min
Goal
Measure a distance in centimetres with an ultrasonic sensor.
Why it matters
You measure a time of flight: distance = (duration × 340 m/s) / 2, exactly like a radar.
What you need
- Arduino UNO board
- Piezo buzzer
- MPU6050 IMU
- HC-SR04 distance
Nothing to buy: every component below is simulated in the browser.
Steps
- 01Add the ultrasonic sensor (TRIG on 9, ECHO on 10).
- 02Send a 10 µs pulse on TRIG.
- 03Measure the echo duration with pulseIn(ECHO, HIGH).
- 04Convert: cm = duration / 58.
This tutorial runs inside the Circuitly simulator: your wiring and your code are checked automatically at every step.
Frequently asked questions
- Do I need to buy hardware for this tutorial?
- No. Everything runs in the browser: Circuitly simulates the board, the components and the code. Real hardware is optional if you want to rebuild the circuit afterwards.
- Which components are used?
- Arduino UNO board, Piezo buzzer, MPU6050 IMU, HC-SR04 distance. They are already available in the simulator library.
- How long does it take?
- About 20 minutes, advanced level, in 4 guided steps.
- What should I know before starting?
- The previous tutorial in the series, “Thermistor: measuring temperature”, is enough preparation. No software install is required.
- Is the code provided and checked?
- The simulator checks your wiring and your code automatically at every step, and shows the commented solution if you get stuck.
For teachers
Get the teaching pack: 6 ready-to-run sessions
Full progression, objectives, student steps and an assessment grid — printable, free.
One address, no spam: just the pack and classroom updates.
In the same series
- Read a push button
Light the LED only while the button is pressed.
- Toggle button (debounced)
One press turns on, the next turns off — with no contact bounce.
- Read a potentiometer
Print the analog value of A0 on the serial monitor.
- Convert to volts (map & float)
Turn the ADC's 0–1023 into a 0.00–5.00 V reading.