Projects / Pico distance tape measure

Pico distance tape measure

Intermediate2.3 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. PICO

    Wire the sensor

    VCC to VBUS, GND, Trig to GP3.

  2. PICO

    Build the echo divider

    Echo through 1 kilo-ohm to GP2, and 2 kilo-ohms from GP2 to GND.

  3. PICO

    Wire the display

    SDA to GP0, SCL to GP1, power at 3.3 V.

  4. PICO

    Wire the button

    GP14 to GND.

  5. PICO

    Save the program as main.py

    On the Pico.

  6. PICO

    Point the sensor at a wall

    The distance shows.

  7. PICO

    Press the button

    The reading freezes.

In short

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

FromToWhy
HC-SR04 VCCPico VBUS pin 40 (USB 5 V)The sensor needs 5 V
HC-SR04 GNDPico GNDCommon ground
HC-SR04 TrigPico GP3 pin 5Trigger pulse (3.3 V is enough)
HC-SR04 Echo1 kilo-ohm to Pico GP2 pin 4Echo is 5 V, so divide it
GP2 to GNDTwo 1 kilo-ohm resistors in seriesDivider gives about 3.3 V
OLED VCC and GNDPico 3V3(OUT) and GNDPower at 3.3 V
OLED SDA and SCLPico GP0 pin 1 and GP1 pin 2I2C data and clock
Hold buttonPico GP14 pin 19 and GNDFreezes 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.

CheckExpected
Wall at 50 cmAbout 50 cm on the display
Wall at 1 mAbout 100 cm
Nothing in rangeNo echo message
Hold pressedValue stays put
Compared with a tapeWithin 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 →

Ask Lark AI

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.