Pi Pico W and BME280

I could not get my Pico W to play nicely with my BME280. It just could not read it with the standard micropython drivers. I had some other drivers from Core Electronic and they worked really well. Quite robust and just kicks up an error message instread of crashing your program if a wire comes loose or it gets a few back packages from the sensor. BME280.py , PiicoDev_BME280.py and PiicoDev_Unified.py work together.

This is the example code:

# PiicoDev Atmospheric Sensor BME280 minimal example code
# This program reads Temperature, Pressure and Relative Humididty
# from the PiicoDev Atmospheric Sensor. An altitude reading is also
# available

from PiicoDev_BME280 import PiicoDev_BME280
from PiicoDev_Unified import sleep_ms # cross-platform compatible sleep function

sensor = PiicoDev_BME280() # initialise the sensor
zeroAlt = sensor.altitude() # take an initial altitude reading

while True:
    # Print data
    tempC, presPa, humRH = sensor.values() # read all data from the sensor
    pres_hPa = presPa / 100 # convert air pressurr Pascals -> hPa (or mbar, if you prefer)
    pres_psi=pres_hPa/68.9476 +.22 # add cal factor
    tempF=tempC*9/5+32
    print(str(tempF)+" °F  " + str(pres_psi)+" psi  " + str(humRH)+" %RH")
    
    # Altitude demo
#     print(sensor.altitude() - zeroAlt) # Print the pressure CHANGE since the script began
    sleep_ms(5000)

I'm really surprised the bme280 library didn't work for you.
Here's a simple test script that should work on a Pico-W
You might want to change the SCL and SDA pin-mapping to suit your situation.

from machine import Pin, I2C
import utime
import bme280

# Define I2C pins
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=100000)

# Initialize BME280 sensor
bme = bme280.BME280(i2c=i2c)

while True:
    # Read sensor data
    temperature, pressure, humidity = bme.read_compensated_data()

    # Print sensor data
    print("Temperature: {:.2f}°C".format(temperature))
    print("Pressure: {:.2f} hPa".format(pressure / 25600))
    print("Humidity: {:.2f}%".format(humidity / 1024))

    # Wait for 2 seconds before next reading
    utime.sleep(2)

No Go. Same issue I was having before:

Traceback (most recent call last):
  File "<stdin>", line 9, in <module>
  File "/lib/bme280.py", line 75, in __init__
OSError: [Errno 5] EIO

This is line 9:

bme = bme280.BME280(i2c=i2c)

image

EDIT: I've just looked at the library you showed in your screenshot and believe it is the same or very similar to the one I'm using.

I used a bme280 library I found on the InterWeb. There is a copy on my website at this URL...
https://teamwork-int.com/share_my_projects
Go to the folder labelled 'C_single_sensor_node' and then the folder 'lib' where you should find 'bme280.py'

The error you're encountering, OSError: [Errno 5] EIO, usually indicates a problem with the I2C communication between your Raspberry Pi Pico and the BME280 sensor.

Ensure your BME280 sensor is correctly wired to the Raspberry Pi Pico. The typical wiring configuration is:

  • VCC of BME280 to 3.3V on Pico
  • GND to GND
  • SDA to GPIO 8 (or the SDA pin you're using)
  • SCL to GPIO 9 (or the SCL pin you're using)

Verify the correct address for your module. You can scan for devices on the I2C bus to find out the address of your BME280 sensor:

from machine import I2C, Pin

i2c = I2C(0, scl=Pin(9), sda=Pin(8))
devices = i2c.scan()
print("I2C devices found:", devices)

EDIT: I have some code that handles a loose wire or even a missing BME module. I can share those scripts with you once you have the sensor working reliably.

Just lashed-up a Pico with a BME280 on pins 8 and 9 and can confirm it works fine with the script I posted above and the modified one below. So I suggest you double-check your wiring.

bme_readings

Here's another Micro-Python script (that gets the values directly from the sensor) you could try.

from machine import Pin, I2C
import utime
import bme280

# Define I2C pins
i2c = I2C(0, scl=Pin(9), sda=Pin(8), freq=100000)
# Pin(8) is labelled GP8 on the Pico board and Pin(9) is GP9

# Initialize BME280 sensor
bme = bme280.BME280(i2c=i2c)

while True:
    # Read sensor data
    temp = float(bme.values[0])
    print("\nTemp ", temp)
    
    humidity = float(bme.values[2])
    print("Humidity ", humidity)
    
    pressure = float(bme.values[1])
    print("Pressure ", pressure)

    # Wait for 2 seconds before next reading
    utime.sleep(2)
1 Like

Believe me, I have quadruple checked my wiring. I have a solution that works reliably. I don't know why yours works and mine doesn't. Sometimes the magic works, sometimes the magic doesn't work.
I'll stick with the Core Electronics drivers. If it ain't broke, don't fix it.

Thanks though @dynamicdave

1 Like

Just as a long-shot... have you checked that the wires are good with a continuity tester?.
The other day I had a breadboard wire that looked fine, but was intermittent and was giving similar error reports as what you are seeing.

Yes I have. It doesn't explain why the Core Electronics drivers work flawlessly and kick up errors when I wiggle a wire or disconnect it. I have soldered screw terminals on the Pico and secure connectors on the BME280. The Mouser version is pretty secure:

OK - I'm out of ideas, so I give-up and throw in the towel.

I appreciate the help.