📁

spi.py

Description

⚙ Hardware Needed


📚 External Libraries Needed


(optional) pycubed.py

(optional) adafruit_bme280

📑 Code


Using only built-in libraries:

import board
import busio
import digitalio
from adafruit_bme280 import basic as adafruit_bme280

# Setup the SPI connection
cs = digitalio.DigitalInOut(board.xSDCS)
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)

# Setup the SPI device
bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, cs)

print("\nTemperature: %0.1f C" % bme280.temperature)
print("Humidity: %0.1f %%" % bme280.humidity)
print("Pressure: %0.1f hPa" % bme280.pressure)

Using the pycubed.py helper library and an external sensor breakout board:

from pycubed import cubesat
from adafruit_bme280 import basic as adafruit_bme280

# Setup the SPI device
bme280 = adafruit_bme280.Adafruit_BME280_SPI(cubesat.spi, cs)

print("\nTemperature: %0.1f C" % bme280.temperature)
print("Humidity: %0.1f %%" % bme280.humidity)
print("Pressure: %0.1f hPa" % bme280.pressure)

Details


SPI is a three-wire communication protocol. Read more here and here

  • PyCubed has lots of pins that can be configured for SPI. The default are shown to the right.
  • default pin names are:
    board.MOSI
    board.SCK
    board.MISO

to learn more about configuring pins for different hardware functions, see: 🎹Pin Muxing

📖CircuitPython SPI documentation