CircuitPython Libraries on Linux and Raspberry Pi
last major update July 05, 2023
- Overview
- Running CircuitPython Code without CircuitPython
- CircuitPython & RasPi
- Installing Blinka on Raspberry Pi
- Digital I/O
- I2C Sensors & Devices
- SPI Sensors & Devices
- Using I2C or SPI by Device ID
- UART / Serial
- PWM Outputs & Servos
- Using NeoPixels on the Pi 5 and Pi 500
- More To Come!
- CircuitPython & OrangePi
- CircuitPython & Jetson Nano
- FAQ & Troubleshooting
Using I2C or SPI by Device ID
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 theInstalling 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:
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 be1.
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-inbusio.I2C
module:
"""This exmaple demonstrates how to instantiate theAdafruit BME280 Sensor using this library and justthe I2C bus number."""import adafruit_bme280from adafruit_extended_bus import ExtendedI2C as I2C# Create library object using our Extended Bus I2C porti2c = I2C(1) # Device is /dev/i2c-1bme280 = adafruit_bme280.Adafruit_BME280_I2C(i2c)print(f"\nTemperature: {bme280.temperature:0.1f} C")
"""This exmaple demonstrates how to instantiate theAdafruit BME280 Sensor using this library and justthe I2C bus number."""import adafruit_bme280from adafruit_extended_bus import ExtendedI2C as I2C# Create library object using our Extended Bus I2C porti2c = I2C(1) # Device is /dev/i2c-1bme280 = 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 theBus ID would be1 and theChip Enable ID would be0.
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-inbusio.SPI
module. We are using GPIO 5 for the actual Chip Enable in this example.
"""This exmaple demonstrates how to instantiate theAdafruit BME280 Sensor using this library and justthe SPI bus and chip enable numbers.Please note that Linux will mess with the system CE pins, sowe are using an alternate pin for the Chip Enable line. Thislibrary is more useful for using a SPI Device on a Bus otherthan 0"""import boardimport digitalioimport adafruit_bme280from adafruit_extended_bus import ExtendedSPI as SPI# Create library object using our Extended Bus I2C portspi = SPI(1, 0) # Device is /dev/spidev1.0cs = digitalio.DigitalInOut(board.D5)bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, cs)print(f"\nTemperature: {bme280.temperature:0.1f} C")
"""This exmaple demonstrates how to instantiate theAdafruit BME280 Sensor using this library and justthe SPI bus and chip enable numbers.Please note that Linux will mess with the system CE pins, sowe are using an alternate pin for the Chip Enable line. Thislibrary is more useful for using a SPI Device on a Bus otherthan 0"""import boardimport digitalioimport adafruit_bme280from adafruit_extended_bus import ExtendedSPI as SPI# Create library object using our Extended Bus I2C portspi = SPI(1, 0) # Device is /dev/spidev1.0cs = digitalio.DigitalInOut(board.D5)bme280 = adafruit_bme280.Adafruit_BME280_SPI(spi, cs)print(f"\nTemperature: {bme280.temperature:0.1f} C")
Page last edited January 22, 2025
Text editor powered bytinymce.