One of the great things about Linux systems is that each of the subsystems are stored as separate devices and so you can tell if the I2C or SPI device is available by looking in the /dev/ folder. If your I2C or SPI devices are not showing up, make sure you followed the steps on the Installing CircuitPython Libraries on Raspberry Pi page.

In order to maintain a certain level of compatibility with CircuitPython, busio was written to attempt to automatically detect which pins you had your I2C or SPI device set up to use. However, with the flexibility that the Raspberry Pi provides and the staggering number of possible pin combinations, there are definitely cases where it fails to detect it properly. This is why we wrote the Python Extended Bus library, which allows you to specify the bus and device ID so you can tell it exactly which device you want to use.

Installing the Library

Installing the library is easy once you already have Blinka setup. Just use the following command to install:

pip3 install adafruit-extended-bus

That's it!

I2C Devices

To use an I2C device, you first need to know the device file name and from that, you can get the ID number. For instance, if you wanted to use /dev/i2c-1, the ID number would be 1.

You would then pass that ID into the Extended I2C Constructor. Here's an example of how to use /dev/i2c-1 with the BME280 sensor instead of the built-in busio.I2C module:

"""
This exmaple demonstrates how to instantiate the
Adafruit BME280 Sensor using this library and just
the I2C bus number.
"""

import adafruit_bme280
from adafruit_extended_bus import ExtendedI2C as I2C

# Create library object using our Extended Bus I2C port
i2c = I2C(1)  # Device is /dev/i2c-1
bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)
print(f"\nTemperature: {bme280.temperature:0.1f} C")

SPI Devices

This is less useful than I2C if you only have the first SPI port enabled because in most cases, you can already use any GPIO pin as a Chip Enable line. However, if you have multiple SPI buses enabled, then it becomes much more useful.

To use SPI Devices with this library, it is similar to I2C, but you have a bus and Chip Enable number to determine. The first number is the Bus ID and the second number is the Chip Enable ID. So for instance, if you have a SPI device named /dev/spidev1.0 that you would like to use, then the Bus ID would be 1 and the Chip Enable ID would be 0.

You would then pass that IDs into the Extended SPI Constructor. Here's an example of how to use /dev/spidev1.0 with the BME280 sensor instead of the built-in busio.SPI module. We are using GPIO 5 for the actual Chip Enable in this example.

"""
This exmaple demonstrates how to instantiate the
Adafruit BME280 Sensor using this library and just
the SPI bus and chip enable numbers.

Please note that Linux will mess with the system CE pins, so
we are using an alternate pin for the Chip Enable line. This
library is more useful for using a SPI Device on a Bus other
than 0
"""

import board
import digitalio
import adafruit_bme280
from adafruit_extended_bus import ExtendedSPI as SPI

# Create library object using our Extended Bus I2C port
spi = SPI(1, 0)  # Device is /dev/spidev1.0
cs = digitalio.DigitalInOut(board.D5)
bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, cs)
print(f"\nTemperature: {bme280.temperature:0.1f} C")

This guide was first published on Jun 30, 2018. It was last updated on Mar 18, 2024.

This page (Using I2C or SPI by Device ID) was last updated on Mar 18, 2024.

Text editor powered by tinymce.