0
0
Iot-protocolsHow-ToBeginner · 4 min read

Raspberry Pi Weather Display Project: Simple Guide & Code

A Raspberry Pi weather display project uses a Python script to read data from sensors like a DHT22 or fetch online weather via APIs, then shows it on a screen such as an LCD or OLED. You connect the sensor or use internet data, process it with Python, and display temperature, humidity, and weather info in real time.
📐

Syntax

This project mainly uses Python code to read sensor data or fetch weather from an API, then display it on a screen connected to the Raspberry Pi.

  • Import libraries: Use import to bring in sensor and display modules.
  • Initialize sensor: Set up the sensor pin and type.
  • Read data: Use sensor methods or API calls to get weather info.
  • Display data: Send the data to the screen using display library commands.
python
import Adafruit_DHT
import time
from luma.core.interface.serial import i2c
from luma.oled.device import sh1106

sensor = Adafruit_DHT.DHT22
pin = 4

serial = i2c(port=1, address=0x3C)
device = sh1106(serial)

def display_text(text):
    from PIL import Image, ImageDraw, ImageFont
    image = Image.new('1', (device.width, device.height))
    draw = ImageDraw.Draw(image)
    font = ImageFont.load_default()
    draw.text((0, 0), text, font=font, fill=255)
    device.display(image)

while True:
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    if humidity is not None and temperature is not None:
        display_text(f'Temp: {temperature:.1f}C\nHumidity: {humidity:.1f}%')
    else:
        display_text('Sensor error')
    time.sleep(10)
💻

Example

This example reads temperature and humidity from a DHT22 sensor connected to GPIO pin 4 on the Raspberry Pi and shows it on a small OLED screen. It updates every 10 seconds.

python
import Adafruit_DHT
import time
from luma.core.interface.serial import i2c
from luma.oled.device import sh1106

sensor = Adafruit_DHT.DHT22
pin = 4

serial = i2c(port=1, address=0x3C)
device = sh1106(serial)

def display_text(text):
    from PIL import Image, ImageDraw, ImageFont
    image = Image.new('1', (device.width, device.height))
    draw = ImageDraw.Draw(image)
    font = ImageFont.load_default()
    draw.text((0, 0), text, font=font, fill=255)
    device.display(image)

while True:
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
    if humidity is not None and temperature is not None:
        display_text(f'Temp: {temperature:.1f}C\nHumidity: {humidity:.1f}%')
    else:
        display_text('Sensor error')
    time.sleep(10)
Output
Temp: 22.5C Humidity: 55.3%
⚠️

Common Pitfalls

Sensor connection errors: Wiring the sensor incorrectly or using the wrong GPIO pin causes no data or errors.

Missing libraries: Forgetting to install Adafruit_DHT or luma.oled will cause import errors.

Display issues: Wrong I2C address or no I2C enabled on Raspberry Pi disables the screen.

Handling sensor read failures: The sensor may sometimes fail to read; always check for None values before displaying.

python
import Adafruit_DHT

sensor = Adafruit_DHT.DHT22
pin = 4

# Wrong way: Not checking if data is None
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
print(f'Temp: {temperature}C, Humidity: {humidity}%')  # May print None or crash

# Right way: Check before use
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
    print(f'Temp: {temperature:.1f}C, Humidity: {humidity:.1f}%')
else:
    print('Failed to get reading from sensor')
Output
Failed to get reading from sensor
📊

Quick Reference

  • Use Adafruit_DHT library for DHT sensors.
  • Use luma.oled for OLED display control.
  • Enable I2C on Raspberry Pi with raspi-config.
  • Check sensor wiring: power, ground, data pin.
  • Handle sensor read failures gracefully.
  • Update display every 10 seconds or as needed.

Key Takeaways

Connect your sensor and display correctly and enable I2C on Raspberry Pi before coding.
Use Python libraries like Adafruit_DHT and luma.oled to read and show weather data.
Always check sensor readings for errors before displaying to avoid crashes.
Update the display regularly to show fresh weather information.
Test wiring and library installations to prevent common setup issues.