Raspberry Pi Pico weather display (MicroPython)
Your first MicroPython project: temperature and humidity on an OLED.
Shop by category
Shop all componentsThe official Sense HAT plugs onto the Pi's 40-pin header and carries humidity, pressure and motion sensors, an 8x8 RGB matrix and a five-way joystick. The sense_hat Python library that comes with Raspberry Pi OS reads them in a few lines. This script scrolls the chosen reading and colours it blue, green or red for cold, comfortable or hot, and logs every reading to a CSV file.
Tick anything you already have and the total updates.
Follow the colours: red is power, dark grey is ground, blue is a signal.
Official USB-C supply. The Pi warms the HAT, so temperature reads a few degrees high; the script subtracts an offset.
weather.pyfrom sense_hat import SenseHat
from datetime import datetime
import time
sense = SenseHat()
sense.low_light = True
OFFSET = 4.0 # the Pi heats the HAT; compare with a room thermometer and adjust
modes = ["temp", "humidity", "pressure"]
mode = 0
last_log = 0
def colour(t):
return (0, 0, 255) if t < 20 else (0, 255, 0) if t < 28 else (255, 0, 0)
while True:
for e in sense.stick.get_events():
if e.action == "pressed" and e.direction in ("left", "right"):
mode = (mode + (1 if e.direction == "right" else -1)) % 3
t = sense.get_temperature_from_humidity() - OFFSET
h = sense.get_humidity()
p = sense.get_pressure()
if time.time() - last_log > 300:
with open("/home/pi/weather.csv", "a") as f:
f.write(f"{datetime.now():%Y-%m-%d %H:%M},{t:.1f},{h:.0f},{p:.0f}\n")
last_log = time.time()
if modes[mode] == "temp":
sense.show_message(f"{t:.1f}C", text_colour=colour(t), scroll_speed=0.07)
elif modes[mode] == "humidity":
sense.show_message(f"{h:.0f}%", text_colour=(0, 200, 200), scroll_speed=0.07)
else:
sense.show_message(f"{p:.0f}hPa", text_colour=(200, 200, 0), scroll_speed=0.07)
The Pi's heat reaches the HAT; increase OFFSET or use a 40-pin ribbon cable to move the HAT away.
Reseat the HAT with the Pi off and check that I2C is enabled in raspi-config.
Keep sense.low_light = True.
Yes, the Sense HAT works on Pi 4 and Pi 5 with current Raspberry Pi OS.
Open weather.csv in LibreOffice Calc and insert a line chart.
Yes, sense.get_accelerometer_raw() reads the onboard IMU.
4 parts: Raspberry Pi 4 Model B 2GB RAM Single Board Computer, Raspberry Pi Sense HAT, Official USB-C 15.3W Power Supply for Raspberry Pi 4 – Black and Official Raspberry Pi 32GB A2 Class Micro SD Card. 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 6 steps.
No. This page has the complete, tested code (weather.py).
About ₹11,589 for the parts at the prices on this page. Quantity price breaks lower the cost when you build several.
Using a HAT on the 40-pin header; The sense_hat Python library; Logging readings to CSV.
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.
Lessons are free to read. Maker (₹499/month) and Pro add quizzes, worked problems and certificates, paid with your Geekobot account. See plans
Learning project. Check every circuit and the datasheet of each part before you power it up.