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

QtyPartWhat it does
1Arduino Unothe microcontroller board
1HC-SR04 ultrasonic sensorA 10 µs pulse on TRIG, then pulseIn(ECHO, HIGH): 58 µs per cm. VCC to 5V, GND to GND.
1220 Ω resistorLimits current. 220 Ω in series with an LED; 10 kΩ as a pull-up or pull-down.
1red LEDLights when A (anode, the bent leg) is high and C (cathode) is low. Use a resistor.

Wiring

#FromTo
1Arduino Uno 5VHC-SR04 ultrasonic sensor VCC
2Arduino Uno GNDHC-SR04 ultrasonic sensor GND
3Arduino Uno pin 9HC-SR04 ultrasonic sensor TRIG
4Arduino Uno pin 10HC-SR04 ultrasonic sensor ECHO
5Arduino Uno pin 13220 Ω resistor leg 1
6220 Ω resistor leg 2red LED anode (A, long leg)
7Arduino Uno GNDred LED cathode (C, short leg)

Step by step

  1. Open the simulator. Open the BitForBytes Embedded Lab in your browser and start from the Arduino Uno. No install or account is needed.
  2. Add the parts. Add an HC-SR04 ultrasonic sensor, a 220 Ω resistor and a red LED from the parts palette.
  3. Wire 1. Connect Arduino Uno 5V to HC-SR04 ultrasonic sensor VCC.
  4. Wire 2. Connect Arduino Uno GND to HC-SR04 ultrasonic sensor GND.
  5. Wire 3. Connect Arduino Uno pin 9 to HC-SR04 ultrasonic sensor TRIG.
  6. Wire 4. Connect Arduino Uno pin 10 to HC-SR04 ultrasonic sensor ECHO.
  7. Wire 5. Connect Arduino Uno pin 13 to 220 Ω resistor leg 1.
  8. Wire 6. Connect 220 Ω resistor leg 2 to red LED anode (A, long leg).
  9. Wire 7. Connect Arduino Uno GND to red LED cathode (C, short leg).
  10. Paste the Arduino C++ code. Paste the Arduino C++ code below into the code editor.
  11. 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