Troubleshooting BME280 Sensor Data Reading Issue on Raspberry Pi Pico with MicroPython

Hello, I am trying to interface a BME280 sensor with a Raspberry Pi Pico using MicroPython over I2C to monitor environmental conditions such as temperature, humidity, and pressure. However, I am encountering an issue with reading the sensor data, I have referred to the BME280 sensor documentation and the MicroPython library documentation to confirm the correct method for reading sensor data, I have examined the
bme280
library code to verify the methods available for reading data from the sensor, I reinstalled the
bme280
library to ensure it was correctly installed, I ran a simple I2C scan script to ensure that the BME280 sensor is properly connected and detected by the Raspberry Pi Pico.

i2c = I2C(0, scl=Pin(17), sda=Pin(16), freq=100000)
devices = i2c.scan()
print("I2C devices found:", devices)

But still getting the error
AttributeError: 'BME280' object has no attribute 'read_compensated_data'

This is my code

from machine import I2C, Pin
import time
import bme280

# Initialize I2C
i2c = I2C(0, scl=Pin(17), sda=Pin(16), freq=100000)

# Create BME280 object
bme = bme280.BME280(i2c=i2c)

while True:
    temperature, pressure, humidity = bme.read_compensated_data()
    print(f"Temperature: {temperature/100:.2f} °C")
    print(f"Pressure: {pressure/256:.2f} hPa")
    print(f"Humidity: {humidity/1024:.2f} %")
    time.sleep(1)

@Middleware & OS
Solution
Hello @Enthernet Code

read_compensated_data()
method doesn't exist in your BME280 library. Try adding this:

while True:
    print(f"Temperature: {bme.temperature:.2f} °C")
    print(f"Pressure: {bme.pressure:.2f} hPa")
    print(f"Humidity: {bme.humidity:.2f} %")
    time.sleep(1)

If this doesn't work, check your BME280 library's version
Was this page helpful?