Projects / Environment display on a Pi

Environment display on a Pi

Beginner1 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. Pi 4

    Enable I2C

    Run sudo raspi-config, Interface Options, I2C, and reboot.

  2. Pi 4

    Wire the devices

    Both share pins 3 and 5 with 3.3 V and ground.

  3. Pi 4

    Scan the bus

    Run i2cdetect -y 1 and look for 76 and 3c.

  4. Pi 4

    Install the libraries

    Make a virtual environment and install the three packages.

  5. Pi 414.3 mASSD1306

    Run the script

    python3 env.py. Readings show on the OLED.

  6. Pi 4

    Make it start at boot

    Add a systemd service with Restart=always and enable it.

  7. Pi 4

    Watch the trend

    After three hours the arrow shows whether pressure is rising or falling.

In short

How do you build an environment display on a Pi?

A BME280 and OLED on a Pi show live readings and a trend arrow. You need Raspberry Pi 4 Model B (4 GB), BME280 Environmental Sensor, SSD1306 OLED Display, Jumper wires female to female (40 pcs), SanDisk microSD card 32 GB (A1, Class 10) and Raspberry Pi 27 W USB-C power supply. It takes 7 steps and about 60 minutes.

Level
Beginner
Time
About 60 min
Parts
6

Parts list

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

Wiring

FromToWhy
BME280 VINPi 3.3V (pin 1)Supply
BME280 GNDPi GND (pin 6)Ground
BME280 SDAPi GPIO 2 (pin 3)I2C data
BME280 SCLPi GPIO 3 (pin 5)I2C clock
OLED VCCPi 3.3V (pin 17)Supply
OLED GNDPi GND (pin 9)Ground
OLED SDA and SCLPi pins 3 and 5Shared with the BME280

Code

Raspberry Pi OS (Bookworm or newer) with Python 3. Enable I2C in raspi-config. Install in a virtual environment: pip install adafruit-circuitpython-bme280 adafruit-circuitpython-ssd1306 pillow. To run at boot, create a systemd service whose ExecStart points to the virtual environment's python and the script path, with Restart=always. The pressure trend needs three hours of history to be meaningful.

import time
from collections import deque
import board
import busio
from adafruit_bme280 import basic as adafruit_bme280
import adafruit_ssd1306
from PIL import Image, ImageDraw, ImageFont

i2c = busio.I2C(board.SCL, board.SDA)
bme = adafruit_bme280.Adafruit_BME280_I2C(i2c, address=0x76)     # 0x77 on some modules
oled = adafruit_ssd1306.SSD1306_I2C(128, 64, i2c, addr=0x3C)
font = ImageFont.load_default()
history = deque(maxlen=18)        # one reading every 10 minutes is three hours
last_saved = 0

def trend():
    if len(history) < 2:
        return "-"
    d = history[-1] - history[0]
    if d > 1.0:
        return "^"                 # rising
    if d < -1.0:
        return "v"                 # falling
    return "="

while True:
    now = time.time()
    if now - last_saved > 600:
        history.append(bme.pressure)
        last_saved = now
    image = Image.new("1", (oled.width, oled.height))
    draw = ImageDraw.Draw(image)
    draw.text((0, 0),  f"Temp  {bme.temperature:5.1f} C", font=font, fill=255)
    draw.text((0, 14), f"Humid {bme.relative_humidity:5.0f} %", font=font, fill=255)
    draw.text((0, 28), f"Press {bme.pressure:6.1f} hPa {trend()}", font=font, fill=255)
    draw.text((0, 46), time.strftime("%H:%M:%S"), font=font, fill=255)
    oled.image(image)
    oled.show()
    time.sleep(2)

Test checklist

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

CheckExpected
i2cdetectShows 76 and 3c
Breathe on the sensorHumidity jumps and recovers
Compare temperatureWithin 1 to 2 °C of a thermometer
Reboot the PiThe display comes back by itself

If it does not work

i2cdetect shows nothing

I2C is not enabled or SDA and SCL are swapped.

OSError on start

The BME280 address is 0x77 on your module. Change it in the script.

Display blank

The OLED address is not 0x3C or it is the wrong controller.

Temperature reads high

The Pi's heat warms the sensor. Mount it on a short lead away from the board.

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

Ask Lark AI

Common questions

What parts do I need for the environment display on a Pi?

Raspberry Pi 4 Model B (4 GB), BME280 Environmental Sensor, SSD1306 OLED Display, Jumper wires female to female (40 pcs), SanDisk microSD card 32 GB (A1, Class 10) and Raspberry Pi 27 W USB-C power supply.

How long does the environment display on a Pi take to build?

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

How do I know the environment display on a Pi works?

i2cdetect: Shows 76 and 3c. Breathe on the sensor: Humidity jumps and recovers. Compare temperature: Within 1 to 2 °C of a thermometer.

i2cdetect shows nothing: what should I check?

I2C is not enabled or SDA and SCL are swapped.

OSError on start: what should I check?

The BME280 address is 0x77 on your module. Change it in the script.

Display blank: what should I check?

The OLED address is not 0x3C or it is the wrong controller.

Temperature reads high: what should I check?

The Pi's heat warms the sensor. Mount it on a short lead away from the board.