Pico distance tape measure
Build it step by step
0 of 7 doneWire the sensor
VCC to VBUS, GND, Trig to GP3.
Build the echo divider
Echo through 1 kilo-ohm to GP2, and 2 kilo-ohms from GP2 to GND.
Wire the display
SDA to GP0, SCL to GP1, power at 3.3 V.
Wire the button
GP14 to GND.
Save the program as main.py
On the Pico.
Point the sensor at a wall
The distance shows.
Press the button
The reading freezes.
How do you build a pico distance tape measure?
A Pico measures distance with an HC-SR04 and shows centimetres on an OLED, with a hold button to freeze the reading. You need Raspberry Pi Pico H, HC-SR04 Ultrasonic Sensor, Resistor 1 kΩ × 3, SSD1306 OLED Display, Tactile push button 6 mm and Breadboard 830 Tie Points. It takes 7 steps and about 140 minutes.
- Level
- Intermediate
- Time
- About 140 min
- Parts
- 6
Parts list
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| HC-SR04 VCC | Pico VBUS pin 40 (USB 5 V) | The sensor needs 5 V |
| HC-SR04 GND | Pico GND | Common ground |
| HC-SR04 Trig | Pico GP3 pin 5 | Trigger pulse (3.3 V is enough) |
| HC-SR04 Echo | 1 kilo-ohm to Pico GP2 pin 4 | Echo is 5 V, so divide it |
| GP2 to GND | Two 1 kilo-ohm resistors in series | Divider gives about 3.3 V |
| OLED VCC and GND | Pico 3V3(OUT) and GND | Power at 3.3 V |
| OLED SDA and SCL | Pico GP0 pin 1 and GP1 pin 2 | I2C data and clock |
| Hold button | Pico GP14 pin 19 and GND | Freezes the reading |
Code
An ultrasonic sensor sends a burst of sound and times how long the echo takes to come back. The program sends a ten microsecond trigger pulse, then measures how long the echo pin stays high with a built-in MicroPython timer function. Sound travels about 29 microseconds for each centimetre in each direction, so the pulse width divided by 58 is the distance in centimetres. The sensor runs from 5 V and its echo pin swings to 5 V, but the Pico pins are 3.3 V and must not see 5 V. A divider of one 1 kilo-ohm resistor above the pin and two in series below it brings the echo down to about 3.3 V. The hold button freezes the display so you can read it after moving the sensor. A soft or angled surface may not return an echo, and the sensor has a blind spot of a few centimetres. Readings are indicative only, good to a centimetre or so.
import time
from machine import Pin, I2C, time_pulse_us
from ssd1306 import SSD1306_I2C
trig = Pin(3, Pin.OUT)
echo = Pin(2, Pin.IN)
button = Pin(14, Pin.IN, Pin.PULL_UP)
i2c = I2C(0, sda=Pin(0), scl=Pin(1), freq=400000)
oled = SSD1306_I2C(128, 64, i2c)
def read_cm():
trig.low()
time.sleep_us(3)
trig.high()
time.sleep_us(10) # 10 microsecond trigger pulse
trig.low()
us = time_pulse_us(echo, 1, 30000) # time the echo, give up after 30 ms
if us < 0:
return None # no echo
return us / 58.0 # microseconds to centimetres
hold = False
last = None
prev = 1
while True:
b = button.value()
if prev == 1 and b == 0:
hold = not hold
time.sleep_ms(40)
prev = b
if not hold:
last = read_cm()
oled.fill(0)
oled.text("Tape measure", 0, 0)
if hold:
oled.text("HOLD", 96, 0)
if last is None:
oled.text("No echo", 0, 28)
else:
oled.text("{:.1f} cm".format(last), 0, 28)
oled.show()
time.sleep_ms(100)Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Wall at 50 cm | About 50 cm on the display |
| Wall at 1 m | About 100 cm |
| Nothing in range | No echo message |
| Hold pressed | Value stays put |
| Compared with a tape | Within a centimetre or two |
If it does not work
No echo
Check 5 V on the sensor and the echo divider.
Values jump
Use a flat, hard target and keep wires short.
Pico damaged risk
Never feed the echo straight into a pin, always divide it.
ImportError for ssd1306
Install the micropython-ssd1306 package.
Blank screen
Check the OLED address 0x3C and the GP0 and GP1 wires.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the pico distance tape measure?
Raspberry Pi Pico H, HC-SR04 Ultrasonic Sensor, Resistor 1 kΩ × 3, SSD1306 OLED Display, Tactile push button 6 mm and Breadboard 830 Tie Points.
How long does the pico distance tape measure take to build?
About 140 minutes for a intermediate maker, in 7 steps.
How do I know the pico distance tape measure works?
Wall at 50 cm: About 50 cm on the display. Wall at 1 m: About 100 cm. Nothing in range: No echo message.
No echo: what should I check?
Check 5 V on the sensor and the echo divider.
Values jump: what should I check?
Use a flat, hard target and keep wires short.
Pico damaged risk: what should I check?
Never feed the echo straight into a pin, always divide it.
ImportError for ssd1306: what should I check?
Install the micropython-ssd1306 package.
