Wiring guide

How to wire an HC-SR04 to a Raspberry Pi Pico

To wire an HC-SR04 to a Raspberry Pi Pico, connect VCC to VBUS (5 V from USB), TRIG to GP15 (physical pin 20), ECHO to GP14 (physical pin 19) and GND to GND. The sensor needs 5 V and its ECHO output is a 5 V signal, which a Raspberry Pi Pico pin cannot take, so ECHO goes through a 1 kilohm and 2 kilohm divider. The sketch below uses no library beyond what the board includes. It sends a ping every 100 ms and prints the distance in centimeters.

  • 3.3 V logic
  • Library: none
  • 4 wires, 2 helper parts
Raspberry Pi Pico · HC-SR04live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Pin to pin

HC-SR04 to Raspberry Pi Pico wiring table

Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.

HC-SR04 pinRaspberry Pi Pico pinWire colorWhy
VCCVBUS (5 V from USB)redThe HC-SR04 is a 5 V module; below about 4.5 V many units stop answering.
TRIGGP15 (physical pin 20)greenOutput from the board: a 10 microsecond pulse starts a ping.
ECHOGP14 (physical pin 19), through R1 (1 kΩ)amberInput to the board, through the 1 k / 2 k divider: ECHO swings to 5 V (see below).
GNDGNDblueCommon ground.

Helper parts

The extra parts in the circuit above, and where their wires go:

FromToWire color
R1 (1 kΩ) pin 2GP14amber
R2 (2 kΩ) pin 1GP14amber
R2 (2 kΩ) pin 2GNDblue
Voltage

Power and logic levels for HC-SR04 and Raspberry Pi Pico

Logic level

The Raspberry Pi Pico’s GPIO pins are 3.3 V logic and are not 5 V tolerant. Nothing wired to a pin may go above 3.3 V.

Supply

The HC-SR04 takes 5 V from VBUS (5 V from USB). That is the Raspberry Pi Pico’s 5 V rail, so the part shares the current the board can supply.

This pair

The HC-SR04 needs 5 V, so VCC goes to VBUS (5 V from USB), and its ECHO output is then a 5 V signal. The RP2040’s pins are not 5 V tolerant, so ECHO goes through a divider: 1 k from ECHO to GP14 (physical pin 19), and 2 k from GP14 (physical pin 19) to GND, which brings 5 V down to 3.3 V. TRIG needs nothing; 3.3 V is enough to trigger it.

Pins to leave alone

The numbers in the code are GP numbers, not the physical pin numbers printed beside the header: GP15 is pin 20. GP23 to GP25 are used on the board itself, and GP25 is the LED.

The sketch

The working code for the Raspberry Pi Pico

Library: none, the sketch times the ECHO pulse with pulseIn(). This is the sketch the circuit above runs.

HC-SR04 on Raspberry Pi Pico
const int TRIG_PIN = 15;   // GP15 (physical pin 20)
const int ECHO_PIN = 14;   // GP14 (physical pin 19)

void setup() {
  Serial.begin(115200);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
}

void loop() {
  // A pulse of at least 10 us on TRIG starts one measurement.
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // ECHO stays high for the sound's round trip; 38 ms means nothing in range.
  unsigned long us = pulseIn(ECHO_PIN, HIGH, 38000UL);
  if (us == 0) {
    Serial.println("Out of range");
  } else {
    Serial.print("Distance: ");
    Serial.print(us / 58);
    Serial.println(" cm");
  }
  delay(100);
}
Output

What you should see

Open the serial monitor at 115200 baud. Each ping prints one line. Set the sensor’s distance to 150 cm and the line reads Distance: 150 cm; the sketch divides the ECHO pulse in microseconds by 58 to get centimeters.

When nothing is in range, or ECHO is not connected, the pulse never arrives and the sketch prints Out of range after its 38 ms timeout.

The serial monitor prints this, recorded from the circuit above with the distance set to 150 cm after the first 300 ms:

Distance: 100 cm
Distance: 150 cm
Pitfalls

Common mistakes wiring an HC-SR04 to a Raspberry Pi Pico

ECHO straight into a 3.3 V pin

It reads correctly, so it looks fine. But ECHO swings to 5 V and the RP2040 is rated to 3.6 V, so the pin is overstressed on every ping. Add the 1 k and 2 k divider, or use an HC-SR04P, the 3.3 V version.

pulseIn() without a timeout

With nothing in front of the sensor, pulseIn() with no third argument waits a whole second for a pulse that never comes, and the sketch looks frozen. Pass a timeout of about 38000 microseconds.

Pinging too fast

Fire the next ping before the last echo has died away and the module hears its own previous ping, so short random distances appear. Leave at least 60 ms between measurements.

Build your own HC-SR04 project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.