DEV Community

Ankush Sharma
Ankush Sharma

Posted on

Building a Smart Weather Station with Raspberry Pi Zero W

So I've been wanting to do something with my Raspberry Pi Zero W that's been sitting in my drawer for months, and I finally got around to it last week. The idea was simple: build a little weather station that shows temperature and humidity on a tiny OLED screen. Sounds easy, right? Well, turns out there were a few gotchas along the way, but that's what made it fun.

What I Built

The setup is pretty straightforward - a DHT22 sensor (those blue ones you see everywhere) hooked up to a Pi Zero W, with a small 128x64 OLED display. The sensor reads temperature and humidity, the OLED shows the current readings along with some stats, and everything gets logged to CSV files for later analysis. Oh, and I added a web dashboard because why not.

Project Overview

Hardware Shopping List

Here's what you'll need if you want to build this yourself:

  • Raspberry Pi Zero W - The tiny one with WiFi built-in
  • DHT22 sensor - About INR 200-300 online. There's also a DHT11 version but the 22 is more accurate
  • SSD1306 OLED display - 128x64 pixels, I2C version. Got mine for around INR 300
  • Jumper wires - Female-to-female works best, ewhile buying I did not get these so I had to buy them saperately
  • MicroSD card - 8GB minimum, but I used a 16GB I had lying around

Total cost if you already have the Pi? Maybe $15-20. Not bad.

The Wiring Part

I'm not gonna lie, I always get nervous about wiring stuff wrong and frying components. But this one's pretty forgiving:

For the DHT22:

  • Red wire (VCC) → Pin 1 (3.3V)
  • Yellow wire (Data) → Pin 7 (GPIO4)
  • Black wire (Ground) → Pin 6 (Ground)

For the OLED:

  • VCC → Pin 1 (3.3V)
  • GND → Pin 6 (Ground)
  • SCL → Pin 5 (GPIO3)
  • SDA → Pin 3 (GPIO2)

Pro tip: Double-check your pin numbers before powering on. I mixed up GPIO numbering with physical pin numbers once and spent an hour debugging. Not my proudest moment.

The Software Side

This is where it got interesting. I started with the basic idea of reading the sensor and updating the display, but quickly realized the DHT22 is... let's say "temperamental." Sometimes it takes 2-5 seconds to respond, sometimes it just doesn't feel like working.

The Threading Challenge

My first version was single-threaded and it showed. The clock on the OLED would freeze every time the sensor was being read. Super annoying. So I split things into two threads:

  1. Sensor thread - Runs in the background, reads the DHT22 every 10 seconds, logs to CSV
  2. Display thread - Updates the OLED every second with the latest data

This way the clock stays smooth and the sensor can take its sweet time without blocking everything.

Dealing with Sensor Noise

The DHT22 can be noisy. Like, really noisy. One reading says 24°C, the next says 26°C, then back to 24°C. After some research, I implemented exponential moving average (EMA) filtering:

def _ema(self, previous, new, alpha):
    """
    Smooth out sensor noise using exponential moving average.
    """
    if previous is None:
        return new
    return alpha * new + (1 - alpha) * previous
Enter fullscreen mode Exit fullscreen mode

Set alpha to 0.2 and boom - smooth, stable readings. Much better.

Features That Made It In

Once I had the basic setup working, I kept adding stuff:

1. Daily Statistics

The display shows min/max temperature and humidity for the day. Resets at midnight automatically.

2. Comfort Metrics

Because reading "23°C, 65%" doesn't tell you much, I added:

  • Heat Index - What it actually feels like
  • Dew Point - For the weather nerds
  • Comfort Zone - Labels like "Comfortable", "Muggy", "Dry", etc.

3. Data Logging

Every reading gets saved to a CSV file named by date (like 2026-05-16.csv). This means I can go back and see trends over weeks or months.

4. Web Dashboard

Built a simple Flask app that serves a web dashboard. You can check the current readings from your phone while you're in another room. Added some charts using Chart.js to visualize the historical data.

Access it at http://<your-pi-ip>:5050

The OLED Display

The little OLED screen cycles through different pages:

  • Page 1: Current temp, humidity, and time
  • Page 2: Daily min/max stats
  • Page 3: Comfort metrics
  • Page 4: Sensor health status
  • Page 5: Custom notes (yes, you can add text notes through the web interface)

It auto-rotates every 8 seconds, or you can configure it to stay on one page.

Preventing Burn-in

Fun fact: OLED screens can get burn-in if you display the same thing forever. So I added a subtle pixel shift every 5 minutes. The whole display moves by 1 pixel in a random direction. Barely noticeable but helps extend the screen's life.

Lessons Learned

  1. The DHT22 is slow - Budget 2-5 seconds per reading. Plan your code accordingly.

  2. I2C can be finicky - Make sure I2C is enabled in raspi-config. Run i2cdetect -y 1 to verify your devices show up.

  3. Threading is your friend - For anything real-time with sensors, separate your concerns.

  4. Error handling matters - Sensors fail. Network drops. Files get corrupted. Add retries and fallbacks.

  5. Start simple - I tried to do everything at once initially and got overwhelmed. Build it piece by piece.

The Results

It's been running for about a week now and I'm pretty happy with it. The display looks clean, the data is logging consistently, and I can check the web dashboard from anywhere in my house.

Some random observations from the data:

  • My room gets surprisingly humid at night (70%+)
  • Temperature swing is about 3-4°C throughout the day
  • That "stuffy" feeling in the afternoon? Heat index was reading 28°C when actual temp was 25°C

What's Next?

I'm thinking about adding:

  • Push notifications when temperature goes outside a certain range
  • Integration with Home Assistant
  • Maybe a second sensor for another room to compare
  • Cloud sync to Google Sheets or something

But honestly, it does what I need for now.

Code & Resources

The full code is available on GitHub: https://github.com/ankush-ksharma/Temp_Humidty_Raspi

Feel free to clone it, modify it, break it, fix it - whatever works for you. The README has detailed setup instructions.

Key Dependencies:

  • adafruit-circuitpython-dht - For DHT22
  • adafruit-circuitpython-ssd1306 - For OLED
  • Flask - For web interface
  • Pillow - For drawing on the OLED

Final Thoughts

This was a fun weekend project that turned into something actually useful. The Pi Zero W is surprisingly capable for these kinds of tasks, and seeing the data visualized on the little OLED is oddly satisfying.

If you're looking for a beginner-friendly IoT project, this is a solid one to start with. The components are cheap, the wiring is simple, and there's enough complexity to keep it interesting without being overwhelming.

Plus, you end up with something practical. Win-win.


Questions? Suggestions? Found a bug? Drop a comment or open an issue on the repo. I'm always curious to see how others modify these kinds of projects.

Top comments (0)