Croatia - Flag Croatia

Incoterms:DDP
All prices include duty and customs fees on select shipping methods.

Please confirm your currency selection:

Croatian Kuna
Free shipping on most orders over 400 kn (HRK)
Payment accepted in Credit cards only

Euros
Free shipping on most orders over 50 € (EUR)
All payment options available

US Dollars
Free shipping on most orders over $60 (USD)
All payment options available

Bench Talk for Design Engineers

Bench Talk

rss

Bench Talk for Design Engineers | The Official Blog of Mouser Electronics


Introduction to Embedded Python Mike Parks

(Source: dencg/Shutterstock.com)

The chances are that if you are programming embedded devices, that you are writing code in the C/C++ programming language. C was initially created in the early 1970s; however, it wouldn't be until the 1990s that programming embedded devices would shift from assembly language to the higher-level C language. For the next thirty years, C/C++ would reign supreme for embedded applications thanks to its superb performance, both in terms of speed and compactness. Today, another shift is emerging with the rise of embedded-centric implementations of the popular Python programming language.

Python first emerged in the 1990s and has steadily gained popularity among software developers since then. In fact, in 2020, Python finally reached the number one spot on IEEE Spectrum’s annual survey of programming languages popularity. Initially, Python became popular amongst embedded developers as a scripting language to test electronic devices. Slowly it has been moving further down the development stack. There are actually multiple implementations of Python, with CPython being the reference implementation widely used for desktop applications.

  • PyMite and Tiny Python were among the earliest attempts at developing reduced-capability implementations of Python for the express purpose of running on microcontrollers. PyMite includes only a VM and runtime—no compiler. This means that it can only run bytecode first compiled by CPython. Tiny Python did include a bytecode compiler while still only taking up approximately 64 kilobytes of RAM. Both are based on Python 2, and neither is actively developed. Nonetheless, they do represent the earliest attempts to use Python for embedded applications.
     
  • MicroPython is an exciting alternative to the C programming language for embedded development. It brings several popular features of the desktop-based CPython language to various popular microcontroller architectures, including STM32 boards, TI CC3200/WiPy, Teensy boards, Nordic nRF series, SAMD21, SAMD51, ESP8266, ESP32, RISC-V, and even the Lego Mindstorms EV3 platform. Since launching in 2014, MicroPython has seen a steady uptick in popularity, reflecting the increased usage of CPython in general. Another attractive attribute is that MicroPython's source code is offered under an open-source license (MIT License) on GitHub.
     
  • CircuitPython is an educational and maker-friendly Python implementation and is itself a fork of MicroPython. CIrcuitPython development support is provided by Adafruit Industries and is the de facto language for many of their microcontroller products. There are currently over one hundred boards compatible with CircuitPython, most based on Microchip's SAMD21/SAMD51 and Nordic Semiconductor's nRF52840 microcontrollers. One key feature of CircuitPython is the ability to simply drag a source code file (.py) onto a compatible development board, no different than dragging a file onto a USB flash drive. No need to compile code and no need for access to a cumbersome (and sometimes expensive) programmer/debugger. Also, the source code remains accessible on the device and can be opened on any computer. This means one can download, edit, and reprogram a CircuitPython-compatible development board from any computer using a simple text editor.
     
  • Zerynth is a set of tools wrapped up in a Software Development Kit (SDK) that is geared towards developers of Internet of Things (IoT) products. They specialize in simplifying the connection between 32-bit microcontrollers (including ESP32, ESP8266, ARM Cortex M0+, ARM Cortex M3, and ARM Cortex M4) and various IoT cloud service providers (AWS, Azure, Google Cloud Platform, Ubidots, The Things Network). With just a few lines of Python code, data from edge device sensors can be transmitted to the cloud for further analysis, and long-term storage. Among the provided tools, a command line interface (CLI), the Zerynth Studio Integrated Development Environment (IDE), Zerynth Operating System (OS), and the Zerynth Device Manager; all available for Windows, Mac OS, and Linux. The Zerynth VM takes up a small 80KB of flash memory and 5KB of RAM. Programming can be done in Python, or a Python/C hybrid approach should performance requirements necessitate such an arrangement.
     
  • CPython. Single Board Computers (SBCs) are full-blown desktop computers in a credit card-sized form factor (e.g., Raspberry Pi, BeagleBone). They are also so inexpensive that they are increasingly used in embedded applications. Current generation SBCs are fast enough and have enough memory and storage to run the full-blown CPython implementation (or a slightly pared-down implementation made by omitting unneeded modules). There have been other implementations, such as the Python for ARM Linux that tailored Python for ARM-based architectures (as compared to x86/x64 architectures). More recently, additional implementations such as PyPy—based on a Just-In-Time compiler versus the interpreter of CPython—have been ported to Raspberry Pi and offer improvements such as better speed performance.

Developers need to keep in mind that there is no single 'flavor' of the Python programming language. There are many implementations, and some are explicitly built to perform on embedded platforms superbly. The future of Python as a major player in the professional embedded development world hinges on proving that it can meet or exceed the performance of C. We can look to history to provide some insights. After all, the shift from assembly to C did not occur overnight. Assembly is still used for performance-crucial sections of a C program by inserting assembly code inside C macros. Python and C can work together similarly.

Another consideration is that the microcontrollers are lightyears more powerful than the microcontrollers of ten or twenty years ago. Clock cycles have gone from being measured in a few megahertz to over a gigahertz. Flash memory, once measured in dozens of kilobytes, is now measured in several megabytes. Fortunately, the significant increase in performance has not come with a commensurate increase in price. Thus, embedded application developers can write higher-level code without minding every clock cycle or byte of memory necessarily and still turn out responsive, high-quality products.

Conclusion

Programming languages, like spoken languages, evolve and grow to respond to the needs of the times. CPython derivatives that are expressly built for embedded systems, such as MicroPython and CircuitPython, are poised to be key players in the future of embedded development because they democratize the creation process. When combined with some basic knowledge of electronic hardware, embedded Python languages enable people, regardless of skill level, to blend the physical and the digital. That type of capability unlocks possibilities that were unfathomable just years ago. Around the world, accessible, open-source programming languages and embedded platforms let local communities build tools that are localized, specific to their needs, cost-effective, and sustainable. From allowing us to create programs that monitor the environmental effects of transportation in large cities to those that monitor the water quality in a refugee camp, embedded Python is helping to make the world a better place, one line of code at a time.



« Back


Michael Parks, P.E. is the owner of Green Shoe Garage, a custom electronics design studio and technology consultancy located in Southern Maryland. He produces the S.T.E.A.M. Power podcast to help raise public awareness of technical and scientific matters. Michael is also a licensed Professional Engineer in the state of Maryland and holds a Master’s degree in systems engineering from Johns Hopkins University.


All Authors

Show More Show More
View Blogs by Date