Code Microcontrollers in Python using MicroPython

Spread the love
  • 1

Python is one of the most used programming languages today. If you consider 2017 developer survey by StackOverflow, Python was among the top 5 most preferred language for coding. Python is used in many areas like scripting, web development, machine learning and more. But I had never seen python being used in micro-controllers apart from some coding in raspberry pi which is again on top of a Linux environment.

In 2014, Damien George, Ph.D. in theoretical physics from University of Melbourne and an electronic enthusiast from childhood, created a very successful Kickstarter campaign around this microcontroller language called MicroPython. He currently works full-time maintaining the MicroPython codebase and ecosystem.

MicroPython is a lean and fast implementation of very popular Python language that is optimized to run on a micro-controller. Since it is built around python, MicroPython also supports powerful scripting which is very easy to learn and highly expressive in nature. Running on a small microcontroller, MicroPython allows us to effortlessly blink LEDs, read voltages, make motors and servos move, play sounds, write data to SD cards, communicate wirelessly, and be the brains of our robot, among countless other things.

More About MicroPython

MicroPython is completely written from scratch using ANSI C and it includes a complete parser, compiler, virtual machine, runtime system, garbage collector and support libraries to run on a microcontroller. The compiler compiles the code to either bytecode or native machine code, selectable per function using a function decorator. It also supports inline assembler. All compilation happens on the chip, so there is no need for any software on your PC.

MicroPython currently supports 32-bit ARM processors with the Thumb v2 instruction set, such as the Cortex-M range used in low-cost microcontrollers. It has been tested on an STM32F405 chip.

MicroPython has the following features:

  • Full implementation of the Python 3 grammar (but not yet all of Python’s standard libraries).
  • Implements a lexer, parser, compiler, virtual machine, and runtime.
  • Can execute files, and also has a command line interface (a read-evaluate-print-loop, or REPL).
  • Python code is compiled to a compressed bytecode that runs on the built-in virtual machine.
  • Memory usage is minimized by storing objects in a very efficient ways. Integers that fit in 31-bits do not allocate an object on the heap, and so require memory only on the stack.
  • Using Python decorators, functions can be optionally compiled to native machine code, which takes more memory but runs around 2 times faster than bytecode. Such functions still implement the complete Python language.
  • A function can also be optionally compiled to use native machine integers as numbers, instead of Python objects. Such code runs at close to the speed of an equivalent C function, and can still be called from Python, and can still call Python.
  • These functions can be used to perform time-critical procedures, such as interrupts.
  • An implementation of inline assembler allows complete access to the underlying machine. Inline assembler functions can be called from Python as though they were a normal function.
  • Memory is managed using a simple and fast mark-sweep garbage collector. It takes less than 4ms to perform a full collection. A lot of functions can be written to use no heap memory at all and therefore require no garbage collection.

Not only the language, Damien George also built a MicroPython enabled electronic development board which is based on STM32F405 microcontroller.

The board has a built-in USB interface which can either be used as a serial communication interface or as a removable storage device (MSC). While using as a serial communication interface you can interact with the board through a Python command line. You can also copy your Python scripts to the board’s local filesystem or SD card, and they are then executed independently of the PC.

The board contains a Micro SD slot for data storage, 2 LEDs, a switch, a real-time clock, an accelerometer, and 30 GPIO pins (5 USARTs, 2SPIs, 2 I2C buses, 14 ADC pins, 2 DAC pins, and 4 servo ports with power).


After the first release of Damien George MicroPython, many other companies have built their own MicroPython enabled boards like Adafruit’s CircuitPython, and long range of popular new age boards from Pycom.


Spread the love
  • 1

You may also like...