Raspberry Pi Pico weather display (MicroPython)
Your first MicroPython project: temperature and humidity on an OLED.
Shop by category
Shop all componentsThe gpiozero DistanceSensor class handles the HC-SR04 timing, so reading distance is a single line. The Pi serves a small Flask web page with a bar that fills as the object gets closer. The HC-SR04 echo pin gives 5 V, which can damage the Pi's 3.3 V GPIO pins, so a resistor divider is used on the echo line. The Pi is not a real-time machine, so readings jitter a little; the code averages several of them.
Tick anything you already have and the total updates.
microSD card for the operating system
Follow the colours: red is power, dark grey is ground, blue is a signal.
Official Pi 4 power supply.
gauge.pyfrom flask import Flask, jsonify
from gpiozero import DistanceSensor
app = Flask(__name__)
sensor = DistanceSensor(echo=24, trigger=23, max_distance=2.0)
EMPTY_CM, FULL_CM = 150.0, 20.0
PAGE = '''<meta name="viewport" content="width=device-width">
<h1>Level</h1>
<div style="width:90%;height:40px;border:2px solid #333"><div id="bar" style="height:100%;width:0;background:#2a7"></div></div>
<h2 id="txt">--</h2>
<script>
async function go() {
const d = await (await fetch('/data')).json();
document.getElementById('bar').style.width = d.pct + '%';
document.getElementById('txt').textContent = d.cm.toFixed(0) + ' cm (' + d.pct.toFixed(0) + ' %)';
}
go(); setInterval(go, 1000);
</script>'''
@app.route("/")
def index():
return PAGE
@app.route("/data")
def data():
cm = sensor.distance * 100.0
pct = max(0, min(100, (EMPTY_CM - cm) * 100 / (EMPTY_CM - FULL_CM)))
return jsonify(cm=cm, pct=pct)
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
No echo is reaching the Pi. Check the divider and the TRIG and ECHO pins.
gpiozero averages several readings internally; mount the sensor firmly and keep soft surfaces out of its view.
Use the Pi's IP address on the same Wi-Fi.
The HC-SR04 echo is 5 V and Pi GPIO pins tolerate only 3.3 V.
Yes, from the lid, in a closed tank. Steam disturbs it.
About a centimetre or two, worse on soft or angled surfaces.
7 parts: Raspberry Pi 4 Model B 2GB RAM Single Board Computer, Official Raspberry Pi 4 USB-C Power Supply – 15.3W, Official Raspberry Pi 32GB A2 Class Micro SD Card, HC-SR04 Ultrasonic Range Finder Sensor Module for Arduino, Resistor Assortment Kit (30 Values), Solderless Breadboard and Dupont Jumper Wires (40 pcs). On this page you can tick the parts you already have and add only the rest to your cart.
It is a intermediate project and takes about 1.5 hours. The build on this page has 6 steps.
No. This page has the complete, tested code (gauge.py).
About ₹8,566 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Reading an HC-SR04 with gpiozero; Protecting a 3.3 V pin with a divider; Serving live data with Flask.
Ideas that reuse this build: ESP32 water tank level web page. 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.