Arduino Ultrasonic Library HC-SR04: Wiring, Code and Simulator
To build this Arduino Ultrasonic Library HC-SR04 project you need an Arduino Uno and an HC-SR04 ultrasonic sensor. The simplest HC-SR04 library: sonar.read() gives centimetres. Make the 4 connections in the wiring table, upload the Arduino C++ code, and light an LED when something comes closer than 20 cm.
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. |
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 |
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 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.
- Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
- Run it. Press Run. Light an LED when something comes closer than 20 cm.
Arduino C++ code
// Ultrasonic (Erick Simoes): the simplest HC-SR04 library. TRIG 9, ECHO 10.
#include <Ultrasonic.h>
Ultrasonic sonar(9, 10);
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print("Distance: ");
Serial.print(sonar.read());
Serial.println(" cm");
delay(200);
}
What to try
Light an LED when something comes closer than 20 cm.
Frequently asked questions
How do I wire the Ultrasonic Library HC-SR04 project on an Arduino Uno?
Make 4 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.
What parts do I need for this Arduino project?
An Arduino Uno and an HC-SR04 ultrasonic sensor.
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?
The simplest HC-SR04 library: sonar.read() gives centimetres.
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