Projects / Weather logger with Sense HAT

Weather logger with Sense HAT

Beginner1.5 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. Pi 4RASPBERR

    Fit the HAT

    With the Pi off; use spacers if you have them.

  2. Pi 4RASPBERR

    Install the library

    sudo apt install sense-hat.

  3. Pi 4

    Run a test

    Show a scrolling message.

  4. Pi 4

    Add the correction

    Compare with a thermometer.

  5. Pi 4

    Log to a file

    Check the CSV after ten minutes.

  6. Pi 4RASPBERR

    Move the HAT away

    Re-tune FACTOR.

  7. Pi 4

    Plot a day

    In a spreadsheet.

In short

How do you build a weather logger with Sense HAT?

A Sense HAT shows temperature, humidity and pressure on its LED matrix and logs to a file. You need Raspberry Pi 4 Model B (4 GB), Raspberry Pi Sense HAT, SanDisk microSD card 32 GB (A1, Class 10) and Raspberry Pi 27 W USB-C power supply. It takes 7 steps and about 90 minutes.

Level
Beginner
Time
About 90 min
Parts
4

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Sense HATPi 40-pin header, on a header extender if you have onePlugs on top (a spacer or extender keeps it away from the CPU heat)
Power supplyPi USB-CPower
SD cardPi microSD slotRaspberry Pi OS

Code

Raspberry Pi OS (Bookworm or newer) with Python 3. Install the library with sudo apt install sense-hat, save as weather.py and run python3 weather.py. The Sense HAT sits right on top of the Pi, so its temperature sensor reads high because of the CPU heat. The best fix is to move it away on a header extender, and the next best is the correction in the script, which subtracts a share of the difference between the CPU and the sensor. Tune FACTOR until it agrees with a trusted thermometer in the same room. Humidity also reads low when the sensor is warm. Log every minute, then plot the CSV file in a spreadsheet. Pressure is for the place you are, not sea level, so do not expect it to match a forecast exactly.

from sense_hat import SenseHat
import csv, time, datetime

sense = SenseHat()
FACTOR = 5.466            # CPU heat correction factor; tune it against a trusted thermometer

def cpu_temp():
    with open("/sys/class/thermal/thermal_zone0/temp") as f:
        return int(f.read()) / 1000.0

def corrected_temp():
    t = sense.get_temperature()
    return t - (cpu_temp() - t) / FACTOR      # the Pi's heat leaks into the board; this removes part of it

with open("weather.csv", "a", newline="") as f:
    w = csv.writer(f)
    if f.tell() == 0:
        w.writerow(["time", "temp_c", "humidity_pct", "pressure_hpa"])
    while True:
        t, h, p = corrected_temp(), sense.get_humidity(), sense.get_pressure()
        w.writerow([datetime.datetime.now().isoformat(timespec="seconds"), round(t, 1), round(h, 1), round(p, 1)])
        f.flush()
        sense.show_message("%.0fC %.0f%%" % (t, h), scroll_speed=0.06, text_colour=[0, 120, 255])
        time.sleep(55)                            # about one reading a minute

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Script startedMessage scrolls on the matrix
After ten minutesTen lines in weather.csv
Corrected temperatureWithin about 1 C of a thermometer
Breathe near itHumidity rises

If it does not work

Temperature reads high

Use a header extender or raise FACTOR.

No module sense_hat

Install it with apt and run with python3.

Matrix stays dark

Check the HAT is fully seated.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the weather logger with Sense HAT?

Raspberry Pi 4 Model B (4 GB), Raspberry Pi Sense HAT, SanDisk microSD card 32 GB (A1, Class 10) and Raspberry Pi 27 W USB-C power supply.

How long does the weather logger with Sense HAT take to build?

About 90 minutes for a beginner maker, in 7 steps.

How do I know the weather logger with Sense HAT works?

Script started: Message scrolls on the matrix. After ten minutes: Ten lines in weather.csv. Corrected temperature: Within about 1 C of a thermometer.

Temperature reads high: what should I check?

Use a header extender or raise FACTOR.

No module sense_hat: what should I check?

Install it with apt and run with python3.

Matrix stays dark: what should I check?

Check the HAT is fully seated.