Thanks to the success of Python as a programming language, today many developers have chosen this language in their projects and more and more people are familiar with and experience this language. So why microcontrollers are still programmed in C?
Well, things seem to be taking another direction. MicroPython has been released, a development software that allows the programming of microcontrollers using exclusively Python as a programming language. In this article we will see in detail MicroPython. We will also talk about PyBoard, a microcontroller board specifically designed to be programmed into Python.
MicroPython is an application based entirely on Python 3. This application allows all developers to program microcontrollers using some Python libraries that have been optimized to work on microprocessors normally mounted on microcontrollers.
The development on MicroPython is really simple. It does not require any installation, just open the corresponding web page (see here) on the official micropython.org website. In fact MicroPython is an application that works online, and so instead of installing an application on your computer you can work directly from the browser.
MicroPython has therefore been designed and designed specifically for programming on MicroControllers by adapting the standard Python libraries. In fact, once a program is written in Python, MicroPython compiles the code in a compact enough to be able to occupy a space limit of 256kB and run on devices with 16kB of RAM.
In addition, MicroPython works with highly standard Python code, so everything that is developed on this environment is also working in other development environments in Python, and vice versa.
Compiler with hardware-specific modules
The power of MicroPython is right in the compiler. In fact, this software has within it a series of hardware-specific modules so you can compile the code in Python appropriately depending on the microcontroller used.
But it is not limited to this. The other programming languages, such as C for Arduino for example, are generally used to write a program in its entirety, and once corrected, it is compiled and sent to the microcontroller to be executed (generally in loop). Whilst programming in Python, he knows very well that Python is a language that uses an interpreter. So you can use it to write programs or to work in runtime, launching one command at a time. Well, with MicroPython you can write one command at a time from the console, which is compiled in real time and sent to the microcontroller.
In fact, MicroPython has an interactive prompt (REPL) to execute the commands one at a time interactively, which are executed immediately. The REPL has a history, tab completion, auto-indent and copy-cut-paste functionality, all to make code development easy and simple.
Finally, MicroPython, in addition to the optimized standard library, has a series of modules (for example machine) that allows low-level access to the hardware.
For those interested in furthering the topic, you can find all the documentation on this site.
Otherwise there is currently a book full of examples for sale, which covers the whole subject in a comprehensive manner.
|Python for Microcontrollers: Getting Started with MicroPython|
The MicroPython project
MicroPython, developed in the C99 language, is an entirely opensource project. The code is licensed under the MIT license, and is available to everyone on GitHub. Anyone is free to download the code for study, or to contribute to the improvement and improvement of this project.
Previously you have seen that the most important aspects of this application are the advanced coding techniques that have been used to maintain the same features of Python, even though they have compiled small ones, in order to adapt to the scarce resources of microcontrollers.
Here are some features that have been published on the official website.
- highly configurable due to many compile-time configuration options
- support for many architectures (x86, x86-64, ARM, ARM Thumb, Xtensa)
- extensive test suite with over 590 tests, and more than 18,500 individual testcases
- code coverage at 98.4% for the core and at 96.3% for the core plus extended modules
- fast start-up time from boot to loading of first script (150 microseconds to get to boot.py, on PYBv1.1 running at 168MHz)
- a simple, fast and robust mark-sweep garbage collector for heap memory
- a MemoryError exception is raised if the heap is exhausted
- a RuntimeError exception is raised if the stack limit is reached
- support for running Python code on a hard interrupt with minimal latency
- errors have a backtrace and report the line number of the source code
- constant folding in the parser/compiler
- pointer tagging to fit small integers, strings and objects in a machine word
- transparent transition from small integers to big integers
- support for 64-bit NaN boxing object model
- support for 30-bit stuffed floats, which don’t require heap memory
- a cross-compiler and frozen bytecode, to have pre-compiled scripts that don’t take any RAM (except for any dynamic objects they create)
- multithreading via the “_thread” module, with an optional global-interpreter-lock (still work in progress, only available on selected ports)
- a native emitter that targets machine code directly rather than the bytecode virtual machine
- inline assembler (currently Thumb and Xtensa instruction sets only)
However, MicroPython has been developed taking into account a reference card, also developed primarily for working with Python. The name of this board is PyBoard.
This board is a completely open hardware project and therefore the electronic schemes are accessible to everyone. So you can make it and modify it at your convenience, but if you prefer to buy it is available online at a very reasonable price.
The specifications of this board can be summarized in the following list:
- STM32F405RG microcontroller
- 168 MHz Cortex M4 CPU with hardware floating point
- 1024KiB flash ROM and 192KiB RAM
- Micro USB connector for power and serial communication
- Micro SD card slot, supporting standard and high capacity SD cards
- 3-axis accelerometer (MMA7660)
- Real time clock with optional battery backup
- 24 GPIO on left and right edges and 5 GPIO on bottom row, plus LED and switch GPIO available on bottom row
- 3x 12-bit analog to digital converters, available on 16 pins, 4 with analog ground shielding
- 2x 12-bit digital to analog (DAC) converters, available on pins X5 and X6
- 4 LEDs (red, green, yellow and blue)
- 1 reset and 1 user switch
- On-board 3.3V LDO voltage regulator, capable of supplying up to 250mA, input voltage range 3.6V to 16V
- DFU bootloader in ROM for easy upgrading of firmware
The board is equipped with a micro USB port for connection to the computer and for feeding. There is also a slot for inserting a microSD as a memory. It also has some very interesting integrated chips, such as an accelerometer, some analog-digital converters and 4 multicolour LEDs.
As you can see from the images of the pyboard, we can immediately notice an extensive pinout. The following figure shows in detail the function of each PIN present.
In this short article the MicroPython application was shown, a software that allows Python programming for microcontrollers. You have seen in detail the features of this totally opensource project and in particular of the compiler that is able to produce hardware-specific compiled code according to microcontrollers and small size. You also took a quick look at the pyBoard, a microcontroller developed specifically to be programmed into Python. In the next articles some tutorials on programming in Python will be proposed in this development environment.