Arduino HC-SR04 Ultrasonic Distance Sensor: Wiring, Code and Simulator
To build this Arduino HC-SR04 Ultrasonic Distance Sensor project you need an Arduino Uno and an HC-SR04 ultrasonic sensor, a 220 Ω resistor and a red LED. An HC-SR04 on pins 9 (TRIG) and 10 (ECHO), timed with pulseIn. The LED lights under 20 cm. Make the 7 connections in the wiring table, upload the Arduino C++ code, and move the distance slider in the controls. Below 20 cm the LED comes on.
Open this circuit in the free Arduino Uno simulator
Parts list
| Qty | Part | What it does |
|---|---|---|
| 1 | Arduino Uno | the microcontroller board |
| 1 | HC-SR04 ultrasonic sensor | A 10 µs pulse on TRIG, then pulseIn(ECHO, HIGH): 58 µs per cm. VCC to 5V, GND to GND. |
| 1 | 220 Ω resistor | Limits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down. |
| 1 | red LED | Lights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor. |
Wiring
| # | From | To |
|---|---|---|
| 1 | Arduino Uno 5V | HC-SR04 ultrasonic sensor VCC |
| 2 | Arduino Uno GND | HC-SR04 ultrasonic sensor GND |
| 3 | Arduino Uno pin 9 | HC-SR04 ultrasonic sensor TRIG |
| 4 | Arduino Uno pin 10 | HC-SR04 ultrasonic sensor ECHO |
| 5 | Arduino Uno pin 13 | 220 Ω resistor leg 1 |
| 6 | 220 Ω resistor leg 2 | red LED anode (A, long leg) |
| 7 | Arduino Uno GND | red LED cathode (C, short leg) |
Step by step
- Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
- Add the parts. Add an HC-SR04 ultrasonic sensor, a 220 Ω resistor and a red LED from the parts palette.
- Wire 1. Connect Arduino Uno 5V to HC-SR04 ultrasonic sensor VCC.
- Wire 2. Connect Arduino Uno GND to HC-SR04 ultrasonic sensor GND.
- Wire 3. Connect Arduino Uno pin 9 to HC-SR04 ultrasonic sensor TRIG.
- Wire 4. Connect Arduino Uno pin 10 to HC-SR04 ultrasonic sensor ECHO.
- Wire 5. Connect Arduino Uno pin 13 to 220 Ω resistor leg 1.
- Wire 6. Connect 220 Ω resistor leg 2 to red LED anode (A, long leg).
- Wire 7. Connect Arduino Uno GND to red LED cathode (C, short leg).
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- Run it. Press Run. Move the distance slider in the controls. Below 20 cm the LED comes on.
Arduino C++ code
// Ultrasonic ruler: an HC-SR04 measures distance; the LED lights when
// something is closer than 20 cm.
const int TRIG = 9;
const int ECHO = 10;
const int LED = 13;
void setup() {
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(LED, OUTPUT);
Serial.begin(9600);
}
void loop() {
// A 10 microsecond pulse starts a measurement.
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
// ECHO stays HIGH for the sound's round trip: 58 microseconds per cm.
unsigned long us = pulseIn(ECHO, HIGH, 30000UL);
int cm = us / 58;
Serial.print("Distance: ");
Serial.print(cm);
Serial.println(" cm");
digitalWrite(LED, cm > 0 && cm < 20 ? HIGH : LOW);
delay(200);
}
What to try
Move the distance slider in the controls. Below 20 cm the LED comes on.
Frequently asked questions
How do I wire the HC-SR04 Ultrasonic Distance Sensor project on an Arduino Uno?
Make 7 connections: Arduino Uno 5V to HC-SR04 ultrasonic sensor VCC; Arduino Uno GND to HC-SR04 ultrasonic sensor GND; Arduino Uno pin 9 to HC-SR04 ultrasonic sensor TRIG; Arduino Uno pin 10 to HC-SR04 ultrasonic sensor ECHO; Arduino Uno pin 13 to 220 Ω resistor leg 1; 220 Ω resistor leg 2 to red LED anode (A, long leg); Arduino Uno GND to red LED cathode (C, short leg).
What parts do I need for this Arduino project?
An Arduino Uno and an HC-SR04 ultrasonic sensor, a 220 Ω resistor and a red LED.
Can I simulate this Arduino project online for free?
Yes. The BitForBytes Embedded Lab runs this exact circuit and Arduino C++ code in your browser for free, with no install and no hardware. The sketch is compiled with the real Arduino toolchain and runs on an emulated ATmega328P.
What does the code do?
An HC-SR04 on pins 9 (TRIG) and 10 (ECHO), timed with pulseIn. The LED lights under 20 cm.
More Arduino Uno projects
- Arduino LED Blink: Wiring, Code and Simulator
- Arduino LDR Light Sensor: Wiring, Code and Simulator
- Arduino NTC Thermistor Thermometer: Wiring, Code and Simulator
- Arduino PWM to Analog RC Filter: Wiring, Code and Simulator
- Arduino Capacitance Meter: Wiring, Code and Simulator
- Arduino DC Motor PWM Speed Control with MOSFET: Wiring, Code and Simulator