Let's talk to a SPI sensor.

We can use the BME280 again, since it supports both I2C and SPI. Here's the SPI wiring:

If you have the new version of the board, make sure the I2C MODE switch is OFF for SPI usage.

Install 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

We can run the simple test example from the library. It is set up to use I2C by default, so we need to comment out the I2C stuff and uncomment the SPI stuff.

We also add an import for digitalio (since it wasn't in there) and change the CS pin to the one we are using with the FT232H - C0.

Here's the complete code:

import time

import board
import busio
import digitalio
import adafruit_bme280

# Create library object using our Bus I2C port
#i2c = busio.I2C(board.SCL, board.SDA)
#bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)

# OR create library object using our Bus SPI port
spi = busio.SPI(board.SCK, board.MOSI, board.MISO)
bme_cs = digitalio.DigitalInOut(board.C0)
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.humidity)
    print("Pressure: %0.1f hPa" % bme280.pressure)
    print("Altitude = %0.2f meters" % bme280.altitude)

Copy and save that code and then run it with:

python3 bme280_simpletest.py

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

This guide was first published on Sep 29, 2019. It was last updated on Jun 19, 2024.

This page (SPI) was last updated on Mar 08, 2024.

Text editor powered by tinymce.