HC-SR04 Ultrasonic Sensor with Raspberry Pi Pico [Arduino IDE]

The HC-SR04 ultrasonic sensor is widely used among hobbyists for measuring distance in robotics, automation, and IoT applications. When it comes to using the HC-SR04 module with the Raspberry Pi Pico, there’s a tiny problem though. The HC-SR04 requires 5V, while the Pico operates at 3.3V.

In this tutorial, I will show you how to still make use of your ultrasonic sensor together with your Raspberry Pi Pico or Pico 2 using the Arduino IDE.

You can follow along with this tutorial using a Wokwi Online Simulation:

Table of Contents

    Components Required

    Here’s what you need to connect the HC-SR04 Ultrasonic sensor with the Raspberry Pi Pico:

    Raspberry Pi Pico HC-SR04: Wiring Diagram

    Raspberry Pi Pico HC-SR04 Wiring Diagram

    Since the HC-SR04 usually operates at 5V, but the Raspberry Pi Pico uses 3.3V logic, we need a voltage divider to step down the 5V Echo pin signal to 3.3V.

    To achieve this, we use a 330Ω and 10kΩ resistor voltage divider:

    • Connect the Echo pin of the HC-SR04 to a 330Ω resistor, which then connects to the GPIO pin on the Pico.
    • Also, connect a 10kΩ resistor between the GPIO pin and GND.

    HC-SR04 PinRaspberry Pi Pico Pin
    VCCVBUS (5V)
    TrigGPIO 2
    EchoGPIO 3 (via 330Ω + 10kΩ to GND voltage devider)
    GNDGND

    Check out the complete pinout of the Raspberry Pi Pico here!

    Does the Echo Pin Need to be an ADC Pin?

    No, the Echo pin does not need to be an ADC (Analog-to-Digital Convert) pin because it outputs a digital signal (HIGH or LOW) that represents the time duration of the ultrasonic pulse. Any digital GPIO pin on the Raspberry Pi Pico can be used to read the Echo pin signal.

    How Measuring the Distance with an Ultrasonic Sensor Works

    To start the distance measurement, the Pico sends a 10µs HIGH pulse to the HC-SR04 Trigger pin.

    Now, the sensor emits an ultrasonic sound wave at 40kHz.

    When the wave reflects back after hitting an object, the Echo pin stays HIGH for the time it takes for the wave to return.

    Using the time it took for the wave to be emitted and reflected, we can calculate the distance of an object using the following formula:

    \[Distance=\frac{Time(s)\times343\frac{m}{s}}{2}\]

    We divide by 2 because the time measured by the sensor includes the way towards an object and back.

    The 343m/s represents the speed of sound. Since the time measured by the ultrasonic sensor is returned in microseconds and we would like to know the distance in centimeters, we convert the unit m/s into cm/µs.

    \[Distance=\frac{Time(µs)\times0.0343\frac{cm}{µs}}{2}\]

    Raspberry Pi Pico HC-SR04: Arduino IDE Sketch

    Before we can get started with the sketch, make sure you have properly installed the Raspberry Pi Pico board in the Arduino IDE.
    If you need help in doing so, check out this guide on how to install the Raspberry Pi Pico in the Arduino IDE!

    Luckily, the sketch for measuring the distance using an HC-SR04 ultrasonic sensor is very easy. Basically, we will trigger the sensor and apply the formula from above:

    // trig & echo pins
    const int trigger = 2;
    int echo = 3;
    
    void setup() 
    {
      pinMode(trigger, OUTPUT);
      pinMode(echo, INPUT);
    
      Serial.begin(115200);
    }
    
    void loop() 
    {
      // trigger for 10 microseconds
      digitalWrite(trigger, HIGH);
      delayMicroseconds(10);
      digitalWrite(trigger, LOW);
    
      // pulseIn -> measure duration (microseconds) of HIGH pulse from Echo pin
      int time = pulseIn(echo, HIGH);
    
      // calculate the distance in cm
      int distance = (time * 0.03432) / 2;
      delay(1000);
    
      Serial.print("Distance in cm: ");
      Serial.println(distance);
    }

    We use the pulseIn() function to measure the duration of the HIGH pulse on the Echo pin sent by the HC-SR04 sensor in microseconds.

    Watch the serial monitor in the Arduino IDE for the measurements taken by your Pico!

    Raspberry Pi Pico (2 W) HC-SR04 Measurements in Serial Monitor

    Wrapping Up

    You have successfully interfaced the HC-SR04 ultrasonic sensor with the Raspberry Pi Pico (2 W) using the Arduino IDE! Now, you can integrate this setup into IoT projects, robotics, or automation systems!

    Fun Fact: I use this setup myself to check whether there’s a car parked in my garage by measuring the distance from the ceiling to the ground (or the car).


    What would you use the distance sensor with your Pico for? Share your ideas in the comments!

    Thanks for reading!

    Links marked with an asterisk (*) are affiliate links which means we may receive a commission for purchases made through these links at no extra cost to you. Read more on our Affiliate Disclosure Page.

    Share this article

    Leave a Reply

    Your email address will not be published. Required fields are marked *