The Bosch BME280 is a low-cost ($2) temperature, and pressure sensor that has an I2C interface. This is an ideal way to learn how to use an I2C interface.
Note mbe280 is different from the BMP280 and does not read humidity. The default address is Hex x76 or decimal 118.
Circuit
The BME280 has a standard I2C interface with four wires:
GND - connect to any of the GND pins
VCC - connect this to the 3.3V output of the Pico
SCL - clock
SDA - data
I2C Scanner
After you have connected your sensor you can check the connection by running a quick "I2C Scanner" to find the address of the sensor.
12345
importmachinesda=machine.Pin(0)# This is GP0 on row one of our standard Pico breadboard with the USB on topscl=machine.Pin(1)# Row two of our standard Pico breadboardi2c=machine.I2C(0,sda=sda,scl=scl,freq=400000)print("Device found at decimal",i2c.scan())
Results:
1
[118]
This is decimal of hex 0x76
If the scanner does not return a number then your connections might not be working.
BME280 Driver
You should be able to find the BME280 driver by using the Thonny Tool -> Manage Packages... menu. If that does not work you can try a github search:
importmachinefromutimeimportsleepimportBME280sda=machine.Pin(0)# This is GP0 on row one of our standard Pico breadboard with the USB on topscl=machine.Pin(1)# Row two of our standard Pico breadboardi2c=machine.I2C(0,sda=sda,scl=scl,freq=400000)# initialize the bme class using the default addressbme=BME280()(chip_id,chip_version)=bme.getID()print("Chip ID:",chip_id)print("Version:",chip_version)whileTrue():# get new data from the temperature,pressure,humidity=bme.getData()#adj_bar = bme.adj_baro(pressure, temperature)print("Adj {}".format(bme.adj_baro(pressure,temperature)))print("Temperature: {}C".format(temperature))print("Pressure: {}hpa, {}In".format(pressure,round(pressure*0.02953,2)))sleep(1)
Code 2
1 2 3 4 5 6 7 8 91011121314151617181920212223
frommachineimportI2CimportBME280fromtimeimportsleepsda=machine.Pin(16)scl=machine.Pin(17)i2c=machine.I2C(0,sda=sda,scl=scl,freq=400000)bme=BME280.BME280(i2c=i2c)# print(i2c.scan())whileTrue:temp=bme.temperaturehum=bme.humiditypres=bme.pressure# uncomment for temperature in Fahrenheittemp=(bme.read_temperature()/100)*(9/5)+32#temp = str(round(temp, 2)) + 'F'print('Temperature: ',temp,end='')print(' Humidity:',hum,end='')print(' Pressure:',pres)sleep(5)