(Source: Mouser.com, courtesy of Adafruit)
“Change is the only constant” is a pearl of wisdom often attributed to the Greek philosopher Heraclitus. Nowhere does this ring truer than in the world of digital electronics. The C language, a product of the early 1970’s computing boom, has dominated embedded development ever since the BASIC language ceded its dominion over microcontroller programming. Today the winds shift yet again, this time towards the ever increasingly popular Python programming language. MicroPython emerged in the past few years to bridge the gap between the desktop oriented CPython reference language and relatively limited hardware specs of microcontrollers.
Still, for the novice embedded developer MicroPython can be a bit of a challenge. Enter CircuitPython, a fork of the MicroPython language that has been tweaked to meet the specific needs of students and others looking to learn modern embedded system development. Hosted by Adafruit Industries, the CircuitPython language has been steadily growing in popularity amongst the maker and educational markers since being first released in the summer of 2017.
In its current iteration, version 6.1.0 as of this article, CircuitPython supports numerous development boards powered by Microchip Technology Atmel SAMD21 and SAMD51 microcontrollers and the Nordic Semiconductor nRF52840 system-on-a-chip (SoC). Previous CircuitPython versions supported the popular ESP8266 microcontroller, but support is deprecated as of version 4. While CircuitPython is similar in many regards to MicroPython, it does strip away complexity and offers a more straightforward development workflow for those just dipping their toes in embedded electronics.
First and foremost, CircuitPython, like MicroPython, requires a special firmware to be uploaded to the development board known as USB Flash version 2 or UF2. Many supported development boards come preloaded with the firmware. However, all development boards offer a mechanism to put the system into bootloader mode, allowing the UF2 firmware to be flashed. The UF2 firmware is specific to each development board, so make sure you download the latest firmware specific to your board. The CircuitPython website makes this a simple task. With the development board plugged into a computer and bootloader mode enabled, the board should appear as a flash drive named BOOT. Simply drag the previously downloaded UF2 file into the root folder of the BOOT drive. After successfully being installed, a new drive will then appear called CIRCUITPY. At this point, the development board is ready for its first project.
While any code editor can be used to edit the CircuitPython source file (.py), it is recommended for beginners to use the Mu editor. After the editor is installed and running, simply click the New button to create a blank Python script file. There is plenty of example code available to meet almost every need. Of course, the world-famous Blinky LED is the Hello, World of the hardware world. Here is one such example of blinking an LED:
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT
led.value = True
led.value = False
Once the code is entered, name the file code.py and save it to the root directory of the CIRCUITPY drive. If you save the file elsewhere, such as the desktop, simply click and drag the file from the desktop to the CIRCUITPY drive icon. Important note, file names are important to CircuitPython as some are reserved for special purposes. The code.py file runs after every reload. To establish any setup functions that only run once at bootup, place that code in a file named boot.py.
A nice feature of CircuitPython is that it detects anytime a file is added or updated and automatically restarts the code. No need to press a reset button or unplug/replug the development board’s USB cable. All the source code remains as user-readable files, so you can connect the development board to any computer and make code changes with zero configuration management effort. In other words, you always have the latest source code right on the device.
CircuitPython makes extending functionality very simple with the use of libraries. Within the root folder of the CIRCUITPY drive, there should be a folder called lib. If not, it is safe to create the folder from scratch using your computer’s file explorer. This is where all the support files needed to add functionality or interact with sensors and actuators are stored. CircuitPython offers a standard bundle of libraries that are popular amongst CircuitPython developers. These library files are explicitly optimized for embedded devices and are given the .mpy file extension. It is crucial to ensure that the version number of the library bundle matches the version number of CircuitPython that you are using. As you upgrade the CircuitPython firmware, do not forget to upgrade the library bundle as well.
Some other key differences between CircuitPython and MicroPython includes:
Another exciting aspect of CircuitPython is Blinka. Whereas CircuitPython runs on microcontrollers, Blinka brings the CircuitPython libraries to tiny form factor personal computers known as single-board computers (SBCs) such as the Raspberry Pi. For those coming from a desktop developer background, Blinka makes MicroPython available as a pip installable Python library that runs in the desktop-oriented version of Python—thus making it possible to interact with various sensors and actuators with an SBC using the same libraries as is used for microcontrollers.
CircuitPython is an exciting entry into the “Python-ization” of embedded system software development. Its simplicity of design, coupled with its robust feature set, makes it a tantalizing programming language for educators and makers alike. CircuitPython also offers a unique opportunity for professionals as well. For embedded engineers, the chance to learn a robust language such as Python can help teach desktop and web application skills. For traditional desktop application software developers, CircuitPython offers a way to migrate their coding skills from the PC to embedded systems.
Regardless of your experience, CircuitPython is a worthy new programming language to the embedded development world. Its unique features make getting started with embedded development a snap while also offering some new concepts that should appeal to veteran programmers as well. CircuitPython is a welcomed new addition to the storied history of embedded development programming languages.
We want to know if you have any experiences with CircuitPython? Connect with us on Twitter and share your CircuitPython-powered projects with us! Interested in getting started with CircuitPython, check out this Adafruit CircuitPython Starter Kit.
Michael Parks, P.E. is the co-founder of Green Shoe Garage, a custom electronics design studio and embedded security research firm located in Western Maryland. He produces the Gears of Resistance Podcast to help raise public awareness of technical and scientific matters. Michael is also a licensed Professional Engineer in the state of Maryland and holds a Master’s degree in systems engineering from Johns Hopkins University.
Privacy Center |
Terms and Conditions
Copyright ©2023 Mouser Electronics, Inc.
Mouser® and Mouser Electronics® are trademarks of Mouser Electronics, Inc.
All other trademarks are the property of their respective owners.
Corporate headquarters and logistics center in Mansfield, Texas USA.