Using MicroPython for real-time software development

-September 28, 2015

While languages such as Ada and C++ have gained some adoption in certain circles, for the most part embedded software is still dominated by the procedural and dangerous C programming language. A few years ago, though, an interesting movement began to port Python for use on microcontrollers. Recently the Python 3.5 port, known as MicroPython, has been gaining popularity and adoption amongst hobbyists and professional developers alike.

The MicroPython organization describes MicroPython as “a lean and fast implementation of the Python 3 programming language that is optimized to run on a microcontroller.” MicroPython runs on the bare metal and uses a custom class known as pyb to access the low level peripherals of the host microcontroller. The pyb class provides developers with abstracted access to interrupts, timers, LEDs, ADC, PWM, I2C, SPI, and CAN to name a few peripherals. For developers that feel these objects don’t provide enough control over the microcontroller, there also exists a C API that can be used to wade through the abstractions and directly access the microcontroller registers.

Many different ports of MicroPython already exist but the primary flagship runs on the PyBoard development platform. The PyBoard is based on an STM32F405RG microcontroller that contains an ARM Cortex-M4 core running at 168 MHz with a hardware floating point accelerator. The microcontroller has 1MB of Flash and 192 kB of RAM. Where 1 MB of flash space isn’t enough, developers can use an external high density micro SD card to store Python scripts.

MicroPython offers developers with an easy to use, human readable programming language that abstracts out the hardware layer and allows developers to focus on the application. Python has long been a popular and portable programming language well known for its interpretive language features and its easy learning curve. The demand for Python developers has been steadily growing and the language has found uses across many industries and technical sectors. The portability of Python itself allows algorithms to be developed and tested long before target hardware is available to test.

Developers also shouldn’t forget that Python is not just a scripting language but also an object orienting programming language. Object Oriented Programming techniques can be used to create software that is portable, maintainable, modular, and extensible. Dust off those old OOP textbooks because inheritance and polymorphism are back on the table!

Seasoned engineers may be hesitant to consider MicroPython as an option. After all, between garbage collection and other background tasks can MicroPython really offer real-time response? Over the past several months I’ve been testing and probing the real-time behavior of MicroPython as it behaves on the PyBoard and so far it has help up really well. The hardware floating point accelerator ensures fast and accurate floating point calculations with little to no impact on real-time performance.

The use of proper design techniques through the use of interrupts and timers allow most real-time deadlines to be met with ease. I even designed a scheduler and loaded it up with tasks to read analog sensors, drive a motor, and read SPI and I2C sensors, all while calculating a complex algorithm. Yes, I even implemented try/except/finally error handling, error and data logging while transmitting data off board via UART to perform real-time plotting! So far there hasn’t been so much has a hiccup.

Another sticky point one may consider concerning MicroPython is that the PyBoard is running a pretty sophisticated and costly microcontroller. The Digi-Key pricing for the STM32F405RGT6 puts it in the $7.57 range for volumes of 1k. Most of the processing power is undoubtedly “wasted” running MicroPython and a MCU of half the cost could probably be used especially in medium to high volume applications.

What’s interesting about MicroPython is that it follows the MIT software license and is freely available on github for ports and modification. Selecting a slightly less capable MCU such as the STM32F401RE would halve the cost (along with clock speed and a few other features) while still maintaining the integrity of the MicroPython system. The software development costs in C for a cheaper MCU might not be up to the challenge of matching MicroPython.

Skeptical? I was too until I installed MicroPython to a STM32F4401RE Nucleo board running at 84 MHz. Using proper design techniques, I still found that MicroPython was performing in real-time and meeting all of the deadlines I was throwing at it. Perhaps the kitchen sink still hasn’t been thrown at it, but it has survived a basic shake down thus far. The process of installing MicroPython on the Nucleo Board took no more than a few hours thanks to stepping stones and great documentation left by a contributor.

Can MicroPython compete with or usurp C? There is certainly a strong case for moving up the ladder to a higher level, object oriented programming language. Overall development costs are still a gray area, though, especially for high volume projects.

MicroPython will undoubtedly find a home amongst hobbyists and professionals looking to rapid prototype an embedded system. MicroPython may even find a home amongst low to medium volume production systems. But one thing is for certain: embedded software developers are in dire need of new tools to help them navigate the quagmire of embedded system design in the 21st century. MicroPython might just be one of the tools developers have been looking for.

Going further

Check out the MicroPython website and documentation here.

Jacob is putting together a series of videos on MicroPython that can be seen on his YouTube channel here.

Jacob Beningo is a Certified Software Development Professional (CSDP) whose expertise is in embedded software. He works with companies to decrease costs and time to market while maintaining a quality and robust product. Feel free to contact him at jacob@beningo.com, at his website www.beningo.com, and sign-up for his monthly Embedded Bytes Newsletter here.

Also see:

Loading comments...

Write a Comment

To comment please Log In

FEATURED RESOURCES