CircuitPython for Raspberry Pi and MCUs
One for All

© Lead Image © donatas1205, 123rf.com
The CircuitPython run-time environment runs on almost all microcomputers and microcontrollers, making it perfect for cross-platform programming.
CircuitPython is an Adafruit fork for MicroPython. Both are run-time environments for microcontrollers. Thanks to a compatibility layer, CircuitPython now also runs on various single-board computers (SBCs) like the Raspberry Pi.
MCU Scripting Languages
Traditionally, microcontroller units (MCUs), because of their extremely limited resources, where every byte can count, run either Assembler or C/C++. programs. Over the years, however, manufacturers have been improving their products, and powerful SBCs are now available for very little money that have sufficient reserves for interpreted languages.
Moreover, many typical applications do not exhaust an MCU (e.g., in education or home projects). The obvious approach then is to use an easier to learn scripting language instead of C/C++. Luckily, some processor families have Python, Lua, or JavaScript support (Figure 1).

Even small SBCs like Pi Zero have enough RAM and storage available to use scripting languages without problem. Python in particular impresses with innumerable ready-to-use modules for every imaginable purpose.
Like many other distributions, Raspbian is currently in the final transition phase from Python 2 to Python 3. The developers will soon stop maintaining version 2 because parallel support of two Python variants takes up too much time, raising the question as to why another Python dialect seems necessary or useful.
On the Raspberry Pi and other SBCs, CircuitPython does not use its own interpreter but requires Python 3. CircuitPython is not only an interpreter (on MCUs), but also a hardware abstraction layer in the form of a collection of system libraries. Although the Raspberry Pi already has available various libraries that abstract the hardware, they were designed to be Pi-centric so that the corresponding programs run only on Raspberry Pi without modifications. CircuitPython, on the other hand, promises compatibility across hardware boundaries.
Programming Models
The programming model of any microcomputer, no matter how minimal, is fundamentally different from that of a microcontroller. The computer runs an operating system that, simply put, manages the hardware, programs, and users.
The operating system allocates the available CPU(s) in sequence to all active programs. In this way, several programs run virtually in parallel, even if only one processor is available. However, only on real-time operating systems can a program rely on getting computing time reliably within a defined period. On conventional operating systems it simply has to wait its turn.
An MCU, on the other hand, has no operating system; the single-application program also controls all the resources, which makes it easy to implement time-critical applications that need to transmit bits across the wire within microseconds. Typical application programs here consist of three parts. The first initializes the MCU hardware, the second is an infinite loop, and the third deals with interrupts (i.e., interruptions of the normal process, for example by incoming data).
Apart from reliably allocating computing time and other resources, this programming model also exists for many Raspbian programs. Sensors are read and data is written in a regular cycle, which means it is also possible to use CircuitPython with its simpler programming model for these cases.
Blinka
If you want to prepare your Raspberry Pi for CircuitPython, you need the Blinka library, named after CircuitPython's mascot. Its installation, including all dependencies, is handled by the script in Listing 1. Raspbian also has many standard Python packages in the normal repository. However, the script uses the cross-platform Python package installer pip3
. This tool downloads the packages, builds them from the sources, if required, and stores them in the /usr/lib/python3.6/dist-packages/
directory.
Listing 1
Installing Blinka Library
The script also activates the I2C and SPI interfaces immediately, because reading sensors is one of the tailor-made applications for CircuitPython. After a reboot, the interfaces are then available for your applications. If you have already enabled them on the system with raspi-config
, then comment out the lines under the Activate SPI and I2C (as required) comment in Listing 1. You can see from the installation script itself that Blinka uses RPI.GPIO internally – but this only applies to the Raspberry Pi.
Depending on your application, you will need several libraries besides the Blinka layer. The example program presented here is intended to read the BME280 [1] temperature, humidity, and pressure sensor in an infinite loop and output the results on an old-fashioned display with two lines of 16 characters each. For this sensor, the last line of Listing 1 installs another CircuitPython library from Adafruit.
Although Adafruit has published many libraries, they do not cover every electronic component. The cheap LCDs with two to four display lines – you will find them on Ebay or Amazon – often work with the PCF8574T serial converter chip. However, Adafruit uses the MCP230xx for its displays. For this reason, the Adafruit LCD library is not used for the application example, but rather the library from my GitHub site [2]. To proceed, manually download the Python file lib/hd44780.py
in the repository and copy it into the directory of your main program.
On MCUs, the process is slightly different because pip
is missing. Adafruit provides library packages in ZIP format for each platform. After downloading, unpack the archive and copy the relevant libraries to the lib/
subdirectory.
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
News
-
The GNU Project Celebrates Its 40th Birthday
September 27 marks the 40th anniversary of the GNU Project, and it was celebrated with a hacker meeting in Biel/Bienne, Switzerland.
-
Linux Kernel Reducing Long-Term Support
LTS support for the Linux kernel is about to undergo some serious changes that will have a considerable impact on the future.
-
Fedora 39 Beta Now Available for Testing
For fans and users of Fedora Linux, the first beta of release 39 is now available, which is a minor upgrade but does include GNOME 45.
-
Fedora Linux 40 to Drop X11 for KDE Plasma
When Fedora 40 arrives in 2024, there will be a few big changes coming, especially for the KDE Plasma option.
-
Real-Time Ubuntu Available in AWS Marketplace
Anyone looking for a Linux distribution for real-time processing could do a whole lot worse than Real-Time Ubuntu.
-
KSMBD Finally Reaches a Stable State
For those who've been looking forward to the first release of KSMBD, after two years it's no longer considered experimental.
-
Nitrux 3.0.0 Has Been Released
The latest version of Nitrux brings plenty of innovation and fresh apps to the table.
-
Linux From Scratch 12.0 Now Available
If you're looking to roll your own Linux distribution, the latest version of Linux From Scratch is now available with plenty of updates.
-
Linux Kernel 6.5 Has Been Released
The newest Linux kernel, version 6.5, now includes initial support for two very exciting features.
-
UbuntuDDE 23.04 Now Available
A new version of the UbuntuDDE remix has finally arrived with all the updates from the Deepin desktop and everything that comes with the Ubuntu 23.04 base.