import machine
import utime
from machine import Pin, I2C
import ssd1306
from bme280 import *
# Pin configuration for I2C
sda = Pin(0)
scl = Pin(1)
# Initialize I2C
i2c = I2C(0, sda=sda, scl=scl, freq=400000)
# Initialize the BME280 sensor
bme280 = BME280(i2c=i2c)
# Initialize the SSD1306 OLED display
display = ssd1306.SSD1306_I2C(128, 64, i2c)
# Function to update the display with sensor data
def update_display():
display.fill(0) # Clear the display
# Fetch sensor data
temperature, pressure, humidity = bme.read_compensated_data()
# Convert pressure to hPa and temperature to Celsius
pressure_hpa = pressure // 25600
temperature_c = temperature // 100
# Display sensor data on the OLED screen
display.text("Temperature: {:.1f} C".format(temperature_c / 100), 0, 0)
display.text("Pressure: {:.1f} hPa".format(pressure_hpa / 100), 0, 16)
display.text("Humidity: {:.1f} %".format(humidity / 1024), 0, 32)
display.show() # Update the display
while True:
update_display()
utime.sleep(2) # Update sensor data and display every 2 seconds
Loading
ssd1306
ssd1306