HackSpace magazine

Python on hardware

By Drew Fustini. Posted

Python is an extremely popular programming language used for all sorts of things, from web and app development, to programming single-board computers and microcontrollers. Python is what most people use to program their Raspberry Pi, MicroPython is the text-based language of choice for the micro:bit, and Adafruit is investing heavily in CircuitPython, which is now available on many of their boards. Until recently, most maker-oriented microcontrollers, including Arduino, used languages based on C and C++. So why is this changing? And what difference does it actually make?

Let’s start with the fundamental differences. C and C++ are compiled languages. When you upload code to an Arduino, your computer compiles your code into binary instructions, and then sends those instructions to your Arduino. Python is an interpreted language. When you save MicroPython code to a micro:bit, it does not get translated into binary by your computer. Instead, we install MicroPython on the micro:bit, which is then able to interpret and execute the code you send it without compiling and reflashing.

This means that Python-based boards have a much quicker prototyping cycle: as soon as you press save, your code will start running. Python is also more readable, making it seem less daunting to people who aren’t already programmers. For organisations such as micro:bit and Adafruit, this is key: making hardware welcoming to beginners will increase the size of their community.

There are two main trade-offs: speed and availability of libraries. Compiled languages are faster, and there are currently far more libraries available for Arduino C++ than any other hobbyist language.

The two most popular implementations of Python for microcontrollers are MicroPython – created by Damien George for STM32, and now used by micro:bit and ESP32 – and CircuitPython – a fork of MicroPython that Adafruit manages, focussed on the Atmel ARM SAMD family and a Nordic BLE microcontroller. They are similar, but CircuitPython requires the microcontroller to support USB, and has sacrificed some functionality in favour of simplicity.

Another exciting development is that Python can also be used to actually design hardware, thanks to open-source projects such as Migen and LiteX. Migen is a Python framework for describing digital circuitry at a higher level than Verilog. LiteX builds upon Migen to enable you to build a fully functioning system on a chip (SoC), including a processor core, that can be loaded into an FPGA. Leveraging the accessibility of Python for chip design is a fascinating idea – I can’t wait to see how it evolves!


https://twitter.com/pdp7

Subscribe