Let's talk to a SPI sensor.

The Pico has two SPI ports. Remember that you can attach multiple sensors to a single port as long as each has a separate chip select (CS) pin.

Here we use a BME280 sensor on the secondary SPI port.

SPI0 is the default port used by board.SPI() and MOSI/MISO/SCLK pins.

Install the BME280 Library

To install the BME280 library, run the following:

sudo pip3 install adafruit-circuitpython-bme280

Note that this step is the same as shown in the main BME280 guide. You would do the same thing for any other sensor.

Run Example

Here's is the example code to run:

import time
import board
import busio
import digitalio
import adafruit_bme280

spi = busio.SPI(board.SCK1, board.MOSI1, board.MISO1)
cs = digitalio.DigitalInOut(board.GP13)

bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, cs)

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)

Save this as something like bme280_test.py and run it with:

python3 bme280_test.py

and you should see it print out sensor readings over and over:

This guide was first published on May 01, 2021. It was last updated on May 01, 2021.

This page (SPI) was last updated on May 03, 2021.

Text editor powered by tinymce.