The HC-SR04 is the workhorse of embedded distance measurement. It uses a 40 kHz ultrasonic burst to calculate time-of-flight, giving you a reliable 2 cm to 400 cm range. But because it operates on 5V logic and relies on microsecond-precision timing, sloppy wiring will result in erratic readings or, worse, fried GPIO pins if you eventually migrate your code to a 3.3V microcontroller.
The direct answer for standard Arduino Uno (5V) wiring is: VCC to 5V, GND to GND, Trig to Digital Pin 9, and Echo to Digital Pin 10. Below is the complete, bench-tested procedure to terminate, verify, and debug this circuit.
Tools, Materials, and Device Ratings
Before you start stripping wire, gather the correct materials. Using the wrong wire gauge or an underpowered supply will cause brownouts when the sensor draws its peak current.
- Microcontroller: Arduino Uno R3 (or compatible ATmega328P board).
- Sensor: HC-SR04 Ultrasonic Module (4-pin variant).
- Wire: 22 AWG stranded copper hook-up wire. Colors: Red, Black, Yellow, Blue.
- Terminals: Solderless breadboard OR an Arduino Screw Shield.
- Power Supply: 5V 2A USB wall adapter (UL/CE listed) or a 5V DC bench supply.
- Test Equipment: Digital Multimeter (DMM) with CAT III rating and fresh leads.
The HC-SR04 draws roughly 15 mA during standby and peaks at 20 mA during the 40 kHz transmit burst. A standard Arduino Uno 5V rail can supply up to 500 mA via USB. 22 AWG wire is rated for up to 7A in chassis wiring, making it vastly over-specified for current but perfectly sized for the physical friction of 0.1-inch breadboard terminals and screw shields.
Mains Power Safety and Preparation
While the sensor itself operates at a safe 5V DC, your Arduino requires a power source. If you are using a factory-sealed USB wall adapter, simply plug it into the receptacle. However, if you are building a permanent installation and hardwiring an AC-to-DC buck converter (like a Mean Well RS-15-5) to supply the Arduino's 5V rail, you are working with lethal mains voltage.
Before inspecting, stripping, or terminating any wiring on the AC side of your power supply, you must de-energize the circuit at the main breaker panel. Apply lockout/tagout (LOTO) to prevent accidental re-energization. Use a CAT III or CAT IV multimeter to verify dead across Line-to-Neutral and Line-to-Ground before touching any conductors. Never open a sealed USB wall adapter; if the plastic casing is cracked, discard it. NEC-style guidance applies; your local AHJ has final authority on permanent mains wiring.
Step-by-Step Wiring Procedure
Standard Arduino Uno boards use 0.1-inch female header terminals rather than screw terminals. If you are using an Arduino Screw Shield or a PLC-style terminal block adapter for a permanent install, land the wires on the corresponding screw terminals and torque to 0.5 Nm (4.4 in-lbs) to prevent stranded wire pull-out. For breadboards, ensure your 22 AWG stranded wire is tinned with a small amount of rosin-core solder, or use ferrules, to prevent fraying.
- Terminate VCC (Red Wire): Strip 1/4 inch of insulation from the red 22 AWG wire. Insert one end into the VCC pin of the HC-SR04. Route the other end to the Arduino Uno and land it on the 5V female header terminal (or the screw terminal labeled '5V'). Do not use the 3.3V pin; the sensor will fail to trigger.
- Terminate GND (Black Wire): Strip the black wire. Connect it from the GND pin on the HC-SR04 to any of the three GND terminals on the Arduino Uno's POWER header block. A solid ground reference is critical to prevent noise on the Echo return signal.
- Terminate Trig (Yellow Wire): Connect the yellow wire from the Trig pin on the sensor to Digital Pin 9 on the Arduino's digital header. This pin will output a 5V, 10-microsecond pulse to initiate the ultrasonic burst.
- Terminate Echo (Blue Wire): Connect the blue wire from the Echo pin on the sensor to Digital Pin 10 on the Arduino. This pin will receive the 5V HIGH pulse that corresponds to the time-of-flight of the sound wave.
Verify and Test with a Multimeter
Never skip the verification step. A swapped power and ground wire will instantly destroy the sensor's internal oscillator.
- Verify Supply Voltage: Set your DMM to DC Volts. Place the black probe on the Arduino GND pin and the red probe on the Arduino 5V pin. You should read between 4.80V and 5.15V. If you read 3.3V, you are probing the wrong rail. If you read 0V, check your USB cable and power adapter.
- Verify Sensor Power: Move the red DMM probe directly to the HC-SR04 VCC pin (at the sensor body, not the Arduino header). This checks for voltage drop across your wires. It should read within 0.05V of your source measurement.
- Verify Echo Logic State: Keep the DMM on DC Volts. Place the red probe on the Echo pin (Digital Pin 10). Upload a basic
pingsketch using the Arduino pulseIn() function. Because the Echo pin outputs a rapid pulse, a standard DMM will average the voltage. Expect a reading between 0.2V and 2.5V depending on the distance to the target. If it reads a solid 0V, your Trig wire is likely disconnected. If it reads a solid 5V, the sensor is stuck in a timeout state.
The Most Common Wiring Botch (and How to Fix It)
The Botch: Wiring the HC-SR04 Echo pin directly to a 3.3V microcontroller (like an ESP32, Raspberry Pi Pico, or Arduino Due) without stepping down the voltage.
The Symptom: The microcontroller's GPIO pin reads permanently HIGH, the chip becomes warm to the touch, or the board randomly brownouts and resets. In severe cases, the internal ESD protection diodes on the GPIO pin burn out, permanently bricking that specific pin.
The Fix: The HC-SR04 outputs a 5V logic HIGH on the Echo pin. 3.3V microcontrollers will tolerate a maximum of 3.6V on their GPIO pins. You must use a voltage divider. Wire a 1 kΩ resistor in series with the Echo pin, and a 2 kΩ resistor from the junction of the first resistor to ground. This drops the 5V pulse down to a safe ~3.33V. For a deeper understanding of logic level thresholds, refer to SparkFun's guide on Logic Levels.
Frequently Asked Questions
How to wire an ultrasonic sensor to an Arduino without a breadboard?
For permanent, vibration-resistant installations without a breadboard, use an Arduino Proto Shield or a dedicated Screw Terminal Shield. Solder 0.1-inch male header pins to your HC-SR04, plug it directly into the shield's female sockets, and land your 22 AWG stranded extension wires into the shield's screw terminals. Alternatively, you can solder the wires directly to the sensor pads, but always use heat-shrink tubing over the solder joints to prevent short circuits against the sensor's metal mesh.
Why is my Arduino ultrasonic sensor reading 0 cm or stuck at 400 cm?
A reading of exactly 0 cm usually means the Echo pin is returning a pulse width of 0, which happens if the Trig and Echo wires are swapped, or if the VCC is sagging below 4.5V under load. A reading stuck at 400 cm (or a timeout value like 30000 microseconds) means the pulseIn() function timed out waiting for the Echo pin to go HIGH. This occurs when the sound wave scatters off an angled surface, gets absorbed by soft materials (like foam or clothing), or if the target is genuinely out of range. Ensure your target is flat, hard, and perpendicular to the sensor.
How to wire a 3-pin ultrasonic sensor to an Arduino?
Some variants, like the US-015 or certain RCWL modules, combine the Trig and Echo pins into a single "Signal" pin to save microcontroller I/O. To wire a 3-pin sensor: connect VCC to 5V, GND to GND, and the Signal pin to a single digital pin (e.g., Pin 9). In your code, you must set the pin to OUTPUT, send the 10µs trigger pulse, then immediately switch the pin to INPUT before calling pulseIn() to read the return echo.
What is the maximum cable length for an HC-SR04 ultrasonic sensor?
Keep your wire runs under 1 meter (3 feet). The Echo pin relies on fast, sharp logic edges to measure microsecond timing. Long, unshielded 22 AWG wires act as capacitors, rounding off the sharp 5V square wave into a slow curve. This capacitance causes the Arduino's pulseIn() function to misread the exact start and stop times, resulting in distance errors of several centimeters. If you must run the sensor further than 1 meter, use a 74HC14 Schmitt trigger buffer at the Arduino end to square the signal back up, or switch to an I2C-based time-of-flight sensor like the VL53L0X.






