Python on hardware
By Drew Fustini. Posted
This article was originally published as part of HackSpace magazine, which has since been incorporated into Raspberry Pi Official Magazine.
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.
Advertisement
Christmas is coming – keep Santa on the right track with our Raspberry Pi gift guide!
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!
Drew Fustini is a hardware designer and embedded Linux developer. He is the Vice President of the Open Source Hardware Association, and a board member of the BeagleBoard.org Foundation.
Subscribe to Raspberry Pi Official Magazine
Save up to 37% off the cover price and get a FREE Raspberry Pi Pico 2 W with a subscription to Raspberry Pi Official Magazine.
More articles
Christmas Gift Guide in Raspberry Pi Official Magazine 160
There’s a ton of great stuff in issue 160, including the incredible motion scanner, inspired by the film Aliens. It’s beautiful, it uses Raspberry Pi technology and makes a satisfying ping sound, and the best thing about it is that it actually works, thanks to a DreamHAT+ radar board. Yes, you too can join the […]
Read more →
Win a Raspberry Pi 500+ and Raspberry Pi Monitor!
Fancy getting the most powerful Raspberry Pi desktop setup? Raspberry Pi 500+ and Raspberry Pi monitor pair perfectly together for a portable – and fixed – desktop computer, powered by Raspberry Pi. We have a set to give away, and you can enter below. A Raspberry Pi 500+ & Raspberry Pi Monitor
Read more →
All right all right!! Artificial Intelligence, Hollywood style
When we get AI right, odds on it’ll be thanks to small firms, motivated individuals, and Raspberry Pi
Read more →
Sign up to the newsletter
Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.