Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Adafruit Logo
0

CircuitPython Libraries on Linux and Raspberry Pi

Image for user MakerMelissa
published June 30, 2018, last edited July 02, 2025
last major update July 05, 2023
Save Link Note Download
279
Intermediate
Skill guide

CircuitPython & RasPi

sensors_blinkapi.gif

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.

Related Guides
Search

Search

Categories

[8]ページ先頭

©2009-2025 Movatter.jp