MicroPython

From Wikipedia, the free encyclopedia
Jump to navigation Jump to search

MicroPython
MicroPython new logo.svg
Developer(s)Damien P. George
Initial release3 May 2014; 8 years ago (2014-05-03)
Stable release
1.18[1] Edit this on Wikidata / 16 January 2022; 4 months ago (16 January 2022)
Repository
Written inC
PlatformARM Cortex-M, STM32, ESP8266, ESP32, 16bit PIC, Unix, Microsoft Windows, Zephyr, JavaScript, RP2040
LicenseMIT license[2]
Websitemicropython.org

MicroPython is a software implementation of a programming language largely compatible with Python 3, written in C, that is optimized to run on a microcontroller.[3][4]

MicroPython consists of a Python compiler to bytecode and a runtime interpreter of that bytecode. The user is presented with an interactive prompt (the REPL) to execute supported commands immediately. Included are a selection of core Python libraries; MicroPython includes modules which give the programmer access to low-level hardware.[5]

The source code for the project is available on GitHub under the MIT License.[6]

History[edit]

MicroPython was originally created by the Australian programmer and theoretical physicist Damien George, after a successful Kickstarter backed campaign in 2013.[7] While the original Kickstarter campaign released MicroPython with an STM32F4-powered development board "pyboard", MicroPython supports a number of ARM based architectures.[8] The ports supported in the mainline are ARM Cortex-M (many STM32 boards, TI CC3200/WiPy, Teensy boards, Nordic nRF series, SAMD21 and SAMD51), ESP8266, ESP32, 16bit PIC, Unix, Windows, Zephyr, and JavaScript.[9] Also, there are many forks for a variety of systems and hardware platforms not supported in the mainline.[10]

In 2016, a version of MicroPython for the BBC Micro Bit was created as part of the Python Software Foundation's contribution to the Micro Bit partnership with the BBC.[11]

In July 2017, MicroPython was forked to create CircuitPython, a version of MicroPython with emphasis on education and ease of use. MicroPython and CircuitPython support somewhat different sets of hardware (e.g. CircuitPython supports Atmel SAM D21 and D51 boards, but dropped support for ESP8266). As of version 4.0, CircuitPython is based on MicroPython version 1.9.4.[12]

In 2017, Microsemi made a MicroPython port for RISC-V (RV32 and RV64) architecture.[13]

In April 2019, a version of MicroPython for the Lego Mindstorms EV3 was created.[14]

In January 2021, a MicroPython port for the RP2040 (ARM Cortex-M0+, on Raspberry Pi Pico and others) was created.[15]

Features[edit]

Ability to Run Python[edit]

MicroPython has the ability to run Python, allowing users to create simple and easy-to-understand programs.[16] MicroPython supports many standard Python libraries, supporting more than 80% of the features of Python's most used libraries.[16] MicroPython was designed specifically to support the typical performance gap between microcontrollers and Python.[17] Python code is able to directly access and interact with hardware, with increased hardware possibilities that are not available using a normal Python application that is run on an operating system.[18]

Code Portability[edit]

MicroPython's utilisation of Hardware Abstraction Layer (HAL) technology allows developed code to be portable among different microcontrollers within the same family or platform and on devices that support and can download MicroPython. Programs are often developed and tested on high-performance microcontrollers and distributed with the final application used on lower-performance microcontrollers.[19]

Modules[edit]

MicroPython offers functionality, once new code has been written, to create a frozen module and use it as a library which can be a part of developed firmware. This feature assists with avoiding repetitive downloading of the same, already error-free, tested code into a MicroPython environment. This type of module will be saved to a microcontroller's modules directory for compiling and uploading to the microcontroller where the library will be available using Python's import command to be used repeatedly.[19]

Read-Eval-Print Loop[edit]

The Read-Eval-Print Loop (REPL) allows a developer to enter individual lines of code and have them run immediately on a terminal.[20] Linux-based and macOS systems have terminal emulators that can be used to create a direct connection to a MicroPython device's REPL using a serial USB connection. The REPL assists with the immediate testing of parts of an application as you can run each part of the code and visually see the results. Once different parts of your code is loaded into the REPL you can use additional REPL features to experiment with your code's functionality.[16]

Helpful REPL commands (once connected to a serial console):[20]

  • CTRL + C: KeyBoardInterrupt
  • CTRL + D: Reloading
  • help(): help message
  • help("modules"): lists built-in modules
  • import board [ENTER] dir(board): lists all the pins on your microcontroller board that are available to be used in a program's code

Limitations[edit]

MicroPython's major limitation is the code's simplistic style, adopted from Python, as code is interpreted by the system at runtime.[16] MicroPython programs require higher precision when using data and accessing connections and may be slowed due to Python's simplicity.[16]

MicroPython has more limited hardware support in the microcontroller market than other popular platforms, like Arduino with a smaller number of microcontroller choices that support the language.[17] MicroPython does not include an integrated development environment (IDE) or specific editor unlike other platforms.[17]

Syntax and Semantics[edit]

MicroPython's syntax is adopted from Python, due to its clear and easy-to-understand style and power.[21] Unlike most other programming languages less punctuation is used with fewer syntactical machinations in order to prioritise readability.[16]

Code Blocks[edit]

MicroPython adopts Python's code block style, with code specific to a particular function, condition or loop being indented, meaning certain expressions will only be evaluated once the condition expression is evaluated correctly.[16] This differs from most other languages which typically use symbols or keywords to delimit blocks.[16]

This assists with the readability of MicroPython code as the visual structure mirrors the semantic structure. This key feature is simple but important as misused indentation can result in code executing under a wrong condition or an overall error from the interpreter.[16]

A colon (:) is the key symbol used to indicate the ending of a condition statement and signals to the interpreter that the statement must be evaluated and the indented body that follows to be executed.[16] The indent size is equivalent to one tab or 4 spaces.

Operations[edit]

MicroPython has the ability to perform various mathematical operations using primitive and logical operations.[18]

Supported Operations[18]
Type Operator Name Example
Arithmetic + Addition variable + 1
- Subtraction variable - 1
* Multiplication variable * 4
/ Division variable / 4
% Modulo Division variable % 4
Comparison == Equals expression1 == expression2
!= Not equal expression1 != expression2
< Less than expression1 < expression2
> Greater than expression1 > expression2
<= Less than or equals expression1 <= expression2
>= Greater than or equals expression1 >= expression2
Logical & bitwise and variable1 & variable2
bitwise or variable2
^ bitwise exclusive or variable1 ^ variable2
~ bitwise complement ~variable1
and logical and variable1 and variable2
or logical or variable1 or variable2

Libraries[edit]

MicroPython is a lean and efficient implementation of Python with libraries similar to those in Python.[22] Some standard Python libraries have an equivalent library in MicroPython renamed to distinguish between the two. MicroPython libraries are smaller with not less popular features removed or modified to save memory.[18]

The three types of libraries in MicroPython:[18]

  • derived from a standard Python library (built-in libraries)
  • specific MicroPython libraries
  • specific libraries to assist with hardware functionality

MicroPython is highly customisable and configurable, with language differing between each board (microcontroller) and the availability of libraries may differ. Some functions and classes in a module or the entire module may be unavailable or altered.[18]

Standard Python Libraries in MicroPython[5]
Library Name Description
array operations on arrays
cmath provides math functions for complex numbers
gc garbage collector
math provides basic math operations for floating-point numbers
sys system-level functions; provides access to variables used by the interpreter
ubinascii functions for converting between binary and ASCII
ucollections operations for collections and container types that hold various objects
uerrno provides access to error codes
uhashlib operations for binary hash algorithms
uheapq operations to implement heap queue algorithm
uio operations for handling input/output streams
ujson handles conversion between JSON documents and Python objects
uos functions for filesystem access and basic operating system functions
ure implements regular expression matching operations
uselect functions for handling events on multiple streams
usocket connecting to sockets (networks), providing access to socket interface
ustruct performs conversions to Python objects by packing and unpacking primitive data types
utime provides time and date function, including measuring time intervals and implementing delays
uzlib operations to decompress binary data
MicroPython-Specific Libraries[5]
Library Name Description
framebuf provides a frame buffer that can be used to create bitmap images to be sent to a display
machine functions assisting with accessing and interacting with hardware blocks
micropython access and control of MicroPython internals
network assists with installing network driver, allowing interactions through networks
uctypes access binary data structures

Custom MicroPython Libraries[edit]

When developers begin to create a new application, standard MicroPython libraries and drivers may not meet the requirements, with insufficient operations or calculations. Similar to Python, there is the possibility of extending MicroPython's functionality with custom libraries which extend the ability of the existing libraries and firmware.[19]

In MicroPython, files ending with .py take preference over other library aliases which allows users to extend the use and implementation of the existing libraries.[18]

Supporting Hardware[edit]

As MicroPython's implementation and popularity continues to grow, more boards have the ability to run MicroPython. Many developers are building processor specific versions that can be downloaded onto different microcontrollers.[18] Installing MicroPython on microcontrollers is well documented and user-friendly.[19] MicroPython allows interactions between microcontroller hardware and applications to be simple, allowing access to a range of functionality while working in a resource constrained environment, with a strong level of responsiveness.[16]

The two types of boards used to run MicroPython:[18]

  • MicroPython loaded when manufactured, meaning only MicroPython can be run.
  • boards that have firmware that allows MicroPython to be installed to the board.

Executing Code[edit]

To move a coding program onto a MicroPython board, create a file and copy it onto the microcontroller in order to execute. With the hardware connected to a device, such as a computer, the board's flash drive will appear on the device allowing files to be moved to the flash drive. There will be two existing python files, boot.py and main.py that are typically not modified, main.py may be modified if you wish to run the program every time the microcontroller is booted, otherwise, programs will be run using the REPL console.[18]

Pyboard[edit]

The pyboard is the official MicroPython microcontroller board which fully supports MicroPython's software features. The pyboard's hardware features include:[5]

The Booting Process[edit]

The pyboard contains an internal drive (filesystem) named /flash which is stored within the board's flash memory, additionally, a microSD card can be inserted into a slot and is accessible through /sd. When booted up, a pyboard must select a filesystem to boot from either /flash or /sd with the current directory being set to either /flash or /sd. By default, if an SD card is inserted, /sd will be used, if not, /flash is used. If needed, the use of the SD card for the booting process can be avoided by creating an empty file called /flash/SKIPSD which will remain on the board and exist when the pyboard is booted up and will skip the SD card for the booting process.[5]

Boot Modes[edit]

When the pyboard is powered up normally or the reset button is pressed then the pyboard is booted in a standard mode, meaning that the boot.py file is executed, then the USB configured and finally the python program will run.[5]

There is an ability to override the standard boot sequence through holding down the user switch whilst the board is in the booting process and then pressing reset as you continue to hold the user switch. The pyboard's LEDs will flick between modes and once the LEDs have reached the mode wanted by the user, they can let go of the user switch and the board will boot in the specific mode.[5]

the boot modes are:[5]

  • standard boot: green LED only (runs boot.py then python program)
  • safe boot: orange LED only (does not run any scripts during boot-up)
  • filesystem reset: green and orange LED together (resets flash drive to factory state and boots in safe mode)
  • used as a fix when filesystem is corrupted

Errors[edit]

  • if red and green LEDs flash alternatively then the python script has an error, and you must use the REPL to debug.
  • if all 4 LEDs cycle on and off then there is a hard fault which cannot be recovered from and requires a hard reset.[5]

Programming Examples[18][edit]

Example One: Hello world program:

# print to serial console

print('Hello, World!')

Example Two: Importing + Turning on a LED

import pyb

# turn LED on

pyb.LED(1).on()

Example Three: Reading a File + Loop

import os

# open and read a file

with open(/readme.txt) as f:
	print(f.read())

Bytecode[edit]

MicroPython includes a cross compiler which generates MicroPython bytecode (file extension .mpy). The Python code can be compiled into the bytecode either directly on a microcontroller or it can be precompiled elsewhere.

MicroPython firmware can be built without the compiler, leaving only the virtual machine which can run the precompiled mpy programs.

Implementation + Uses[edit]

MicroPython is utilised through firmware being loaded by standard software onto a particular microcontroller into flash memory, communicating using a terminal application loaded onto a computer that emulates a serial interface.[19]

The main uses of MicroPython can be generalised into 3 categories:[19]

  • educational purposes: using MicroPython's Read – Eval – Print Loop (REPL) to interact with a microcontroller, it is possible to visually explain the concepts of data processing and communicating with boards in a simpler way than more complicated programming languages.
  • developing and testing device and sensor designs: MicroPython offers verified, bug-free, and thoroughly tested reference implementations of interfaces used in microcontrollers solving a common developer's task of implementing peripheral communication setup and control. MicroPython offers direct and interactive accessibility to device registers which makes it easy to verify functionality and develop and test hardware parts and devices and algorithms for control and acquiring data from a device.
  • monitoring and configuring tool for design of complex applications: certain applications require specific applications on high performing microcontrollers. MicroPython is able to assist with state monitoring and set-up of system parameters.

Implementation of MicroPython can differ depending on the availability of standard and supporting libraries and the microcontroller's flash memory and RAM size.[19]

References[edit]

  1. ^ "Release 1.18". 16 January 2022. Retrieved 27 February 2022.
  2. ^ George, Damien P. (4 May 2014). "micropython/LICENSE at master · micropython/micropython". GitHub. Retrieved 11 February 2017.
  3. ^ Venkataramanan, Madhumita (6 December 2013). "Micro Python: more powerful than Arduino, simpler than the Raspberry Pi". Wired. Retrieved 15 December 2016.
  4. ^ Yegulalp, Serdar (5 July 2014). "Micro Python's tiny circuits: Python variant targets microcontrollers". InfoWorld. Retrieved 15 December 2016.
  5. ^ a b c d e f g h i "MicroPython - Python for microcontrollers". micropython.org. Retrieved 12 August 2017.
  6. ^ "MicroPython on GitHub". GitHub. 7 February 2022.
  7. ^ "Micro Python: Python for microcontrollers". Kickstarter. Kickstarter. Retrieved 15 December 2016.
  8. ^ Beningo, Jacob (11 July 2016). "Prototype to production: MicroPython under the hood". EDN Network. Retrieved 15 December 2016.
  9. ^ George, Damien P. "micropython/ports at master · micropython/micropython". GitHub. Retrieved 22 October 2019.
  10. ^ Sokolovsky, Paul. "Awesome MicroPython". GitHub. Retrieved 22 October 2019.
  11. ^ Williams, Alun (7 July 2015). "Hands on with the BBC Micro-Bit user interface". ElectronicsWeekly.com. Retrieved 8 July 2015.
  12. ^ Shawcroft, Scott (22 May 2019). "CircuitPython 4.0.1 released!". Adafruit Blog. Adafruit Industries. Retrieved 11 June 2019.
  13. ^ "RISC-V Poster Preview — 7th RISC-V Workshop" (PDF). 28 November 2017. Retrieved 17 December 2018.
  14. ^ "LEGO releases MicroPython for EV3 based on ev3dev and Pybricks". www.ev3dev.org. Retrieved 21 April 2020.
  15. ^ "Meet Raspberry Silicon: Raspberry Pi Pico now on sale at $4". www.raspberrypi.org. 21 January 2021. Retrieved 21 January 2021.
  16. ^ a b c d e f g h i j k Alsabbagh, Marwan (2019). MicroPython Cookbook. Birmingham, UK: Packt Publishing.
  17. ^ a b c Bruno, P. "An Introduction to MicroPython". All3DP. All3DP. Retrieved 9 May 2022.
  18. ^ a b c d e f g h i j k Bell, Charles (2017). MicroPython for the Internet of Things. Berkeley, USA: Apress.
  19. ^ a b c d e f g Gaspar, G.; Kuba, P.; Flochova, J.; Dudak, J.; Florkova, Z. (2020). Development of IoT applications based on the MicroPython platform for Industry 4.0 implementation. 2020 19th International Conference on Mechatronics – Mechatronika (ME). pp. 1–7.
  20. ^ a b Rembor, K. "The REPL | Welcome to CircuitPython!". Adafruit Learning System. Adafruit Learning System. Retrieved 9 May 2022.
  21. ^ Wang, L.; Li, Y.; Zhang, H.; Han, Q.; Chen, L. (2021). An Efficient Control-flow based Obfuscator for Micropython Bytecode. 2021 7th International Symposium on System and Software Reliability (ISSSR). pp. 54–63.
  22. ^ Khamphroo, M.; Kwankeo, N.; Kaemarungsi, K.; Fukawa, K. (2017). MicroPython-based educational mobile robot for computer coding learning. 2017 8th International Conference of Information and Communication Technology for Embedded Systems (IC-ICTES). pp. 1–6.

External links[edit]