Trying to use BME280 on Pi Pico

I’m attaching a BME280 to a Raspberry Pi Pico using an Adafruit PiCowbell Adalogger for Pico - MicroSD, RTC & STEMMA QT. I can talk on the I2C bus successfully to a PiicoDev board, but I’m having trouble with the BME280.

The code examples for the BME280 all require the use of smbus2 module to pull in SMBus class.

I’ve installed smbus2 in Thonny so it’s there as a lib. When I run code on my Raspberry Pi Pico:

import smbus2

I get the following:

MPY: soft reboot
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/smbus2/__init__.py", line 23, in <module>
  File "/lib/smbus2/smbus2.py", line 25, in <module>
ImportError: no module named 'fcntl'

So next, I install micropython-fcntl, and that gets me the following:

MPY: soft reboot
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/lib/smbus2/__init__.py", line 23, in <module>
  File "/lib/smbus2/smbus2.py", line 25, in <module>
  File "/lib/fcntl.py", line 1, in <module>
ImportError: no module named 'ffi'

Installing fcntrl brought in the dependency of ffilib, which is the only thing that comes up searching for micropython-ffi.

If I search for just “ffi”, I get a long list of packages all with different names:

abc @ micropython-lib
<No description>

cmd @ micropython-lib
<No description>

dht @ micropython-lib
DHT11 & DHT22 temperature/humidity sensor driver.

gzip @ micropython-lib
<No description>

io @ micropython-lib
<No description>

mip @ micropython-lib
On-device package installer for network-capable boards

os @ micropython-lib
<No description>

ssl @ micropython-lib
<No description>

uu @ micropython-lib
<No description>

copy @ micropython-lib
<No description>

hmac @ micropython-lib
<No description>

html @ micropython-lib
<No description>

stat @ micropython-lib
<No description>

time @ micropython-lib
<No description>

venv @ micropython-lib
Support for creating MicroPython virtual environments using `micropython -m venv`

Does anyone know what I need to do to get the BME280 talking on a Pi Pico?

1 Like

Hi Politas,

Since we make a PiicoDev breakout for that sensor, you could use our guides and libraries if that helps:

You might just need to specify custom pins (whichever ones you have the sensor plugged into), details here:

1 Like

smbus is a package that is only intended for Raspberry Pi SBC (not RPi Pico) - It looks like the library you’re using is not compatible with Pico.

Have a go at James’ recommendation and keep us up to date with how you go! :smiley:

1 Like

Thanks, James, that has worked nicely using

from PiicoDev_BME280 import PiicoDev_BME280

sensor = PiicoDev_BME280(bus=0,sda=machine.Pin(SDA),scl=machine.Pin(SCL),address=0x76)

SDA and SCL are constants set to 4 and 5, the pins used by the PiCowbell’s I2C bus, and I found the address by trying the different addresses that came up scanning the bus using this code in REPL:

from machine import I2C
i2c = I2C(0)
i2c.scan()
3 Likes