Raspberry Pi Pico weather display (MicroPython)
Your first MicroPython project: temperature and humidity on an OLED.
Shop by category
Shop all componentsThe HC-SR04 sends a burst of ultrasound and the Pico times the echo with time_pulse_us. Half the round trip at 343 m/s gives the distance. Because the sensor's echo pin gives 5 V and the Pico runs at 3.3 V, a two-resistor divider protects the input: a good habit for every 5 V sensor.
Tick anything you already have and the total updates.
Sends a 40 kHz ultrasonic ping and times the echo to measure distance from 2 cm to about 4 m.
Follow the colours: red is power, dark grey is ground, blue is a signal.
main.py# Pico ultrasonic distance meter with an OLED (MicroPython)
from machine import Pin, I2C, time_pulse_us
import ssd1306, time
trig = Pin(2, Pin.OUT)
echo = Pin(3, Pin.IN) # through a 1 kΩ + 2 kΩ divider: the HC-SR04 echo is 5 V
oled = ssd1306.SSD1306_I2C(128, 64, I2C(0, sda=Pin(4), scl=Pin(5)))
def distance_cm():
trig.value(0); time.sleep_us(2)
trig.value(1); time.sleep_us(10); trig.value(0)
t = time_pulse_us(echo, 1, 30000) # time the echo pulse, give up after 30 ms
return -1 if t < 0 else t * 0.0343 / 2
while True:
d = distance_cm()
oled.fill(0)
oled.text("Distance", 0, 0)
oled.text("--" if d < 0 else "{:.1f} cm".format(d), 0, 24)
if d > 0:
oled.fill_rect(0, 48, min(128, int(d * 128 / 200)), 10, 1) # bar up to 2 m
oled.show()
print(d)
time.sleep_ms(200)
TRIG and ECHO are swapped, or the sensor has no 5 V. Swap the two signal wires and check VCC.
Average several readings and keep wires short. Two HC-SR04s pinging at once will confuse each other.
Check SDA/SCL and try address 0x3D. Run an I²C scanner to confirm.
6 parts: HC-SR04 Ultrasonic Range Finder Sensor Module, 0.96" OLED Display 128x64 (I2C), Raspberry Pi Pico, Resistor Assortment Kit (30 Values), Solderless Breadboard (830 points) and Dupont Jumper Wires (40 pcs). Tools: Digital Multimeter and Wire Stripper and Cutter. On this page you can tick the parts you already have and add only the rest to your cart.
It is a beginner project and takes about 45 minutes. The build on this page has 4 steps.
No. This page has the complete, tested code (main.py). Install this library first: ssd1306 (micropython-lib).
About ₹735 for the parts at the prices on this page, plus tools if you do not have them. Quantity price breaks lower the cost when you build several.
Drawing text on an I²C display; Time-of-flight measurement with sound.
Ideas that reuse this build: Arduino ultrasonic distance meter and Pico soil monitor. They are listed under Take it further on this page.
Your first MicroPython project: temperature and humidity on an OLED.
A web page with ON and OFF buttons that switches a relay over Wi-Fi.
A warm ring light that fades up at dusk and down at dawn.
Turn a Pi and a touchscreen into a home command centre with a clock and to-do list.
Learning project. Check every circuit and the datasheet of each part before you power it up.