How To Connect Hc-sr04 Ultrasonic With Raspberry Pi Pico

About the project

Learn how to get started with HCSR04 Ultrasonic readings using the Raspberry Pi Pico and MicroPython

Project info

Difficulty: Easy

Platforms: Raspberry PiMicroPython

Estimated time: 1 hour

License: GNU General Public License, version 3 or later (GPL3+)

Items used in this project

Hardware components

USB-A to Micro-USB Cable USB-A to Micro-USB Cable x 1
DIYables Breadboard DIYables Breadboard x 1
DIYables Jumper Wires DIYables Jumper Wires x 1
Raspberry Pi Pico Raspberry Pi Pico x 1
Amazon HC-SR04 with Metal Bracket Amazon HC-SR04 with Metal Bracket x 1

Software apps and online services

MicroPython MicroPython


In this tutorial, I quickly demonstrate how to set up the HC-SR04 distance sensor for distance measuring with the Raspberry Pi Pico. I am demonstrating this because it took me a while to find some code that works with 5V logic on the sensor.

Before you begin note that the HCSR04 ultrasonic sensor operates at 5V, which is higher than the voltage tolerance of the Raspberry Pi Pico's GPIO pins (3.3V). Connecting the sensor directly to the Pico's GPIO pins can result in damage to the device. To use the HCSR04 with the Pico, a voltage level converter is required to safely convert the sensor's 5V output to a level that is compatible with the Pico's GPIO pins.

I demonstrate in this tutorial for simplicity, without the additional apparatus but the connections and the code work the same way. However please take precautions from having this setup long-term if you only have one Pico laying around.

1-) Physical Setup:

  • Get four jumper wires and connect them as shown.
  • Note you do not necessarily need a breadboard but I highly recommend it as it will make your life a lot easier dealing with electronics in testing.
  • The main point here is that an Echo and a Trig pin is corresponding to the Transmitter and the Receiver respectively. We use the GP14 and GP15 pins on the Raspberry Pi Pico to do this; these are two of many General Purpose Input/Output (GPIO) pins found on the device. These pins can be used for a variety of purposes, such as reading digital signals, controlling LEDs, or communicating with other devices.
  • The other two pins (black and red), are used for power (VSYS) and ground (GND). A ground pin (or GND pin) is a pin on an electronic device that is connected to the ground, or zero volts, of the electrical system. Ground pins serve as a reference point for all other voltages in the circuit, and are used to complete circuits and provide a path for electrical current to flow.

2-) Code Setup:
  • Copy the code snippet from the bottom of this link.
frommachineimport Pin, time_pulse_us

SOUND_SPEED=340 # Vitesse du son dans l'air

trig_pin = Pin(15, Pin.OUT) # Broche GP15 de la Pico
echo_pin = Pin(14, Pin.IN) # Broche GP14 de la Pico

# Prepare le signal
# Créer une impulsion de 10 µs

ultrason_duration = time_pulse_us(echo_pin, 1, 30000) # Renvoie le temps de propagation de l'onde (en µs)
distance_cm = SOUND_SPEED * ultrason_duration / 20000

print(f"Distance : {distance_cm} cm")
  • Want to thank this gentleman (uPesy) for simplifying this code without the need for external libraries. He deserves most of the credit here but it did take me some time to find this, as all of the other libraries did not work with 5V logic.
  • At a high level, the code uses a simple formula to get the distance in centimeters using the speed of sound (some unit conversion is being done as well).


If you have the physical setup and your Raspberry Pi turned on. You should be able to run the code snippet in the MicroPython environment and start getting values. It's that simple! If you enjoyed this or want to see more short tutorials like this please follow me on Youtube.


Photo of mahmood-m-shilleh


Mechanical and Software Engineering Background. University at Buffalo 2019 Texas A&M 2021 I make data pipelines for my day job. Outside of work, I participate in online communities regarding Full Stack Engineering, Microelectronics, and more. You can find more details about me on my Youtube Channel. Feel free to reach out!


Leave your feedback...