Raspberry Pi Pico weather display (MicroPython)
Your first MicroPython project: temperature and humidity on an OLED.
Shop by category
Shop all componentsA waterproof ultrasonic sensor under the tank lid measures the distance to the water, and the Pico W turns it into a percentage page. Set EMPTY_CM and FULL_CM from your own tank.
Keep the board and wiring dry: only the waterproof probe goes in the tank area.
Tick anything you already have and the total updates.
Waterproof ultrasonic probe on a 2.5 m cable.
Follow the colours: red is power, dark grey is ground, blue is a signal.
main.py# Pico W tank level web page (MicroPython)
import network, socket, time
from machine import Pin, time_pulse_us
SSID = "YOUR_WIFI_NAME"
PASSWORD = "YOUR_WIFI_PASSWORD"
trig = Pin(14, Pin.OUT)
echo = Pin(13, Pin.IN)
EMPTY_CM, FULL_CM = 100, 15 # distance to the water when the tank is empty and full
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
for _ in range(30):
if wlan.isconnected():
break
time.sleep(1)
print("Open http://%s" % wlan.ifconfig()[0])
PAGE = """<!doctype html><html><head><meta name="viewport" content="width=device-width,initial-scale=1">
<meta http-equiv="refresh" content="5"><title>tank level</title></head>
<body style="font-family:sans-serif;background:#0d093d;color:#fff;text-align:center;padding-top:40px">
<p>Tank</p><p style="font-size:64px;margin:0">{:.0f} %</p><p>{:.0f} cm to the water</p><p style="opacity:.6">refreshes every 5 s</p></body></html>"""
def level():
trig.value(0); time.sleep_us(2)
trig.value(1); time.sleep_us(10)
trig.value(0)
t = time_pulse_us(echo, 1, 30000)
if t <= 0:
return -1, -1
d = t * 0.0343 / 2
pct = max(0, min(100, (EMPTY_CM - d) * 100 / (EMPTY_CM - FULL_CM)))
return pct, d
addr = socket.getaddrinfo("0.0.0.0", 80)[0][-1]
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(addr)
s.listen(2)
while True:
cl, _ = s.accept()
try:
cl.recv(512)
cl.send("HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n")
cl.send(PAGE.format(*level()))
except OSError as e:
print("client error", e)
finally:
cl.close()
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.
4 parts: JSN-SR04T-2.0 Waterproof Ultrasonic Sensor with 2.5m Probe, Raspberry Pi Pico WH – RP2040 Dev Board with Wi-Fi & Headers, 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 1 hour. The build on this page has 3 steps.
No. This page has the complete, tested code (main.py).
About ₹1,115 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.
Time-of-flight measurement with sound.
Keep the board and wiring dry: only the waterproof probe goes in the tank area.
Ideas that reuse this build: Pico water tank level OLED and Wi-Fi water tank level. 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.