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
CircuitPython & RasPi
CircuitPython Libraries on Linux & Raspberry Pi
The next obvious step is to bring CircuitPythonback to 'desktop Python'. We've got tons of projects, libraries and example code for CircuitPython on microcontrollers, and thanks to the flexibility and power of Python it's pretty easy to get that code working with micro-computers like Raspberry Pi or other 'Linux with GPIO pins available' single board computers.
We are not running the CircuitPython interpreter itself on the Linux machine. But weare running Python code written to use the CircuitPython hardware API (busio.I2C
,busio.SPI
, etc.)
We'll use a special library calledadafruit_blinka (named after Blinka, the CircuitPython mascot) to provide the layer that translates the CircuitPython hardware API to whatever library the Linux board provides. For example, on Raspberry Pi we use the pythonRPi.GPIO library. For any I2C interfacing we'll use ioctl messages to the/dev/i2c
device. For SPI we'll use thespidev
python library, etc. These details don't matter so much because they all happen underneath theadafruit_blinka layer.
The upshot is that any code we have for CircuitPython will be instantly and easily runnable on Linux computers like Raspberry Pi.
In particular, we'll be able to use all of our device drivers - the sensors, led controllers, motor drivers, HATs, bonnets, etc. And nearly all of these use I2C or SPI!
Wait, isn't there already something that does this - GPIO Zero?
Yes! We like and use GPIO Zero a lot, its an excellent hardware interfacing library for Raspberry Pi. It's great for digital in/out, analog inputs, servos, some basic sensors, etc. In particular, one cool thing it does is thread/event management so you can have code run, say, when a button is pressed.
GPIO Zero excels at that, but doesn't cover SPI/I2C sensors or drivers, which is where we got stuck: for each sensor we had we'd write a driver in C/C++ for Arduino, CircuitPython using our hardware API, and then Python using smbus or similar.
By letting you use CircuitPython on Raspberry Pi viaadafruit_blinka, you can unlock all of the drivers and example code we wrote!And you can keep using GPIO Zero for pins, buttons and LEDs. We save time and effort so we can focus on getting code that works in one place, and you get to reuse all the code we've written already.
What about other Linux SBCs?
Plus, we're adapting and extendingadafruit_blinka tosupportotherboards such as Allwinners and BeagleBone, even some smaller linux boards like Onion.io will be able to run CircuitPython code.
If you have a board you'd like to adaptcheck out the adafruit_blinka code on github, pull requests are welcome as there's aton of different Linux boards out there!You'll need to add a detection element so we can tell what board you're running on, then the pin definitions intoadafruit_blinka above. As long as you're running a modern kernel, you'll have libgpiod for GPIO, smbus for I2C and spidev for SPI all ready to go.
Page last edited March 08, 2024
Text editor powered bytinymce.