CircuitPython Libraries on Linux & Raspberry Pi

The next obvious step is to bring CircuitPython back 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 we are running Python code written to use the CircuitPython hardware API (busio.I2C, busio.SPI, etc.)

We'll use a special library called adafruit_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 python RPi.GPIO library. For any I2C interfacing we'll use ioctl messages to the /dev/i2c device. For SPI we'll use the spidev python library, etc. These details don't matter so much because they all happen underneath the adafruit_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 via adafruit_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 extending adafruit_blinka to support other boards 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 adapt check out the adafruit_blinka code on github, pull requests are welcome as there's a ton 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 into adafruit_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.

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

This page (CircuitPython & RasPi) was last updated on Mar 08, 2024.

Text editor powered by tinymce.