For this example, we're going to use the Adafruit BME280 sensor, which uses I2C since there are so many sensors that use I2C.

Wiring

  • Connect the red wire from the STEMMA QT connector on the BME280 to pin 36 or 3.3V out on the Pico.
  • Connect the black wire from the STEMMA QT connector on the BME280 to pin 38 or Gnd on the Pico.
  • Connect the blue wire or SDA from the STEMMA QT connector on the BME280 to pin 1 or GP0 on the Pico.
  • Connect the yellow wire or SCL from the STEMMA QT connector on the BME280 to pin 2 or GP1 on the Pico.

Example Code

Go ahead and click Download Project Bundle to download the examples and dependent libraries. Unzip the bundle and upload the libraries as described on the Install Blinka and Libraries page.

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

import time
import board
import busio
from adafruit_bme280 import basic as adafruit_bme280

# Create sensor object, using the board's default I2C bus.
i2c = busio.I2C(board.GP1, board.GP0)  # SCL, SDA
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)

# OR create sensor object, using the board's default SPI bus.
# spi = busio.SPI(board.GP2, MISO=board.GP0, MOSI=board.GP3)
# bme_cs = digitalio.DigitalInOut(board.GP1)
# bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, bme_cs)

# change this to match the location's pressure (hPa) at sea level
bme280.sea_level_pressure = 1013.25

while True:
    print("\nTemperature: %0.1f C" % bme280.temperature)
    print("Humidity: %0.1f %%" % bme280.relative_humidity)
    print("Pressure: %0.1f hPa" % bme280.pressure)
    print("Altitude = %0.2f meters" % bme280.altitude)
    time.sleep(2)

Here's what the files on your Raspberry Pi Pico should look like with the libraries added in.

Press the green play button to run the script from the editor.

You can also copy the code onto the pico using Thonny, save it onto the Raspberry Pi Pico as main.py in the root folder if you would like it to run each time. You will need to press Control+D to perform a soft restart if you save it to the Pico.

It will show you the temperature and humidity information in the Shell pane.

This guide was first published on May 19, 2021. It was last updated on 2021-05-19 13:43:19 -0400.

This page (BME280 Library Example) was last updated on Sep 21, 2021.

Text editor powered by tinymce.