Getting Started with an Ultrasonic Sensor Module on Raspberry Pi Pico

Small educational robot uses ultrasonic sensor to navigate obstacles like cones and boxes while moving through a bright indoor course

An ultrasonic sensor module measures distance by using sound waves rather than light, making it a popular choice in robotics and electronics projects. A common example is the HC-SR04 ultrasonic sensor module, which uses ultrasonic sound pulses to detect how far away an object is. The module includes a transmitter that sends out a short burst of high-frequency sound (around 40 kHz) and a receiver that listens for the sound to reflect back after hitting an object. This echo-based approach allows the ultrasonic sensor module to work reliably in a wide range of lighting conditions.

To use an ultrasonic sensor module with a Pico, the Pico first sends a short signal to the TRIG pin to start a measurement. This trigger causes the sensor to emit an ultrasonic ping. Immediately after, the ECHO pin goes high and remains high until the reflected sound is detected. The Pico measures how long the ECHO pin stays high, which represents the total time taken for the sound to travel from the ultrasonic sensor module to the object and back again.

The measured time can be used to calculate the distance to an object. Since the speed of sound in air is known, the Pico can work out how far away the object is by using a simple time-of-flight calculation. Because the sound wave travels to the object and then back again, the total measured time is first divided by two to find the one-way travel time. This value is then multiplied by the speed of sound in air, which is about 343 metres per second.

For example, if the sensor measures a time of 0.01 seconds between sending the sound and receiving the echo, the Pico divides this by two to get 0.005 seconds. It then multiplies this by 343, giving a distance of 1.715 metres. This means the object is approximately 1.7 metres away from the ultrasonic sensor.

This simple timing-based approach is why the ultrasonic sensor module is so widely used in beginner tutorials, obstacle detection, and distance measurement projects in robotics and embedded systems.

You can read more about how ultrasonic ranging sensors work here.

Wire up the circuit

In order to run the project firstly the IHC-SR04 ultrasonic sensor module needs to be connected as shown in the diagram below. The circuit diagram below is shown for clarity and the actual connection on the breadboard follow.

The circuit looks as shown below on the set up that was used to create this tutorial.

Running the Code

In order to get started you will need to follow this tutorial, which will ensure that your environment and Pico are configured correctly.

Now create a file called main.py on the Raspberry Pico with the following content.

Next, click the run button and you should see the following in the console.

The output represents the distance from the sensor to the nearest object in front of it. You can try moving objects in front of the sensor closer and further away to see the sensor working.

Main Topic

The Raspberry Pi Pico

Raspberry Pi Pico on a breadboard with sensors and jumper wires arranged neatly on a desk, ready for an electronics project.

Raspberry Pi Pico is an affordable, efficient microcontroller offering flexible GPIO, strong performance, and versatile hardware features for beginner electronics projects.

Other Tutorials in this Topic

Cartoon Raspberry Pi Pico on a breadboard with glowing LED and wires beside a computer screen showing the Thonny picozero plugin installation.

Getting Started With The Raspberry Pico

Getting started guide showing how to set up a Raspberry Pi Pico on a breadboard and install…

A cheerful orange octopus adjusts wires on a breadboard with a Raspberry Pi Pico, potentiometer, and glowing LED beneath the text Basic Input / Output On the Pico.

Basic Input and Output With The Raspberry Pi…

The article explains how a Raspberry Pi Pico uses a potentiometer to control LED brightness, demonstrating basic…

A cheerful cartoon duck turns a control dial beside a spinning motor while an oscilloscope displays a squarewave showing PWM motor control.

Pico Motor Control Using the L9110 Control Board

A tutorial showing how to control a motor using PWM modulation to varying the speed of a…

Cartoon computer and Raspberry Pi Pico shout echo into a glowing cave with bold title Using Bluetooth On The Raspberry Pico.

Using Bluetooth on the Raspberry Pico

The article explains how to use Bluetooth Low Energy on the Raspberry Pi Pico, set up BLE…

Cartoon robot spins happily while IMU detects z-axis rotation with arrows screens and motion lines in colorful laboratory scene background

Using the MPU6050 Gyro With Raspberry Pi Pico

The article explains how the MPU6050 IMU measures motion and rotation and communicates with microcontrollers using the…

Cartoon LCD on breadboard shows Hello from Tinkimo heart while silly banana sparks and googly eyed hammer watch Pico project

Using an LCD with the Pico

Beginner friendly guide explains using an LCD with the Raspberry Pi Pico for wiring, control signals, and…

Raspberry Pi Pico on breadboard drives LCD showing Hello message surrounded by colourful wires LEDs creating playful beginner electronics scene

Controlling an ST7789 Display With a Raspberry Pi…

Beginner guide explains using an ST7789 LCD with a Raspberry Pi Pico to create simple colourful display…