Developer(s) | Damien P. George |
---|---|
Initial release | 3 May 2014 |
Stable release | |
Repository | |
Written in | C |
Platform | ARM Cortex-M, STM32, ESP8266, ESP32, 16-bit PIC, Unix, Microsoft Windows, Zephyr, JavaScript, RP2040 |
License | MIT license [2] |
Website | micropython |
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]
MicroPython does have an inline assembler, which lets the code run at full speed, but it is not portable across different microcontrollers.
The source code for the project is available on GitHub under the MIT License. [6]
MicroPython was originally created by the Australian programmer 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 [9] boards, RP2040 boards, TI CC3200/WiPy, Teensy boards, Nordic nRF series, SAMD21 and SAMD51), ESP8266, ESP32, [10] 16-bit PIC, Unix, Windows, Zephyr, and JavaScript. [11] Also, there are many forks for a variety of systems and hardware platforms not supported in the mainline. [12]
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. [13]
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. [14]
In 2017, Microsemi made a MicroPython port for RISC-V (RV32 and RV64) architecture. [15]
In April 2019, a version of MicroPython for the Lego Mindstorms EV3 was created. [16]
In January 2021, a MicroPython port for the RP2040 (ARM Cortex-M0+, on Raspberry Pi Pico and others) was created. [17]
MicroPython has the ability to run Python, allowing users to create simple and easy-to-understand programs. [18] MicroPython supports many standard Python libraries, supporting more than 80% of the features of Python's most used libraries. [18] MicroPython was designed specifically to support the typical performance gap between microcontrollers and Python. [19] 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. [20]
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. [21]
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. [21]
The read–eval–print loop (REPL) allows a developer to enter individual lines of code and have them run immediately on a terminal. [22] 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. [18]
Helpful REPL commands (once connected to a serial console): [22]
Although MicroPython fully implements Python language version 3.4 and much of 3.5, it does not implement all language features introduced from 3.5 onwards, [23] though some new syntax from 3.6 and more recent features from later versions, e.g. from 3.8 (assignment expressions) and 3.9. It includes a subset of the standard library. [24]
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. [19] MicroPython does not include an integrated development environment (IDE) or specific editor unlike other platforms. [19]
MicroPython's syntax is adopted from Python, due to its clear and easy-to-understand style and power. [25] Unlike most other programming languages less punctuation is used with fewer syntactical machinations in order to prioritise readability. [18]
MicroPython adopts Python's code block style, with code specific to a particular function, condition or loop being indented. [18] This differs from most other languages which typically use symbols or keywords to delimit blocks. [18] 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. [18]
A colon (:) is the key symbol used to indicate the ending of a condition statement. [18] The indent size is equivalent to one tab or 4 spaces.
MicroPython has the ability to perform various mathematical operations using primitive and logical operations. [20]
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 | variable1 | variable2 | |
^ | bitwise exclusive or | variable1 ^ variable2 | |
~ | bitwise complement | ~variable1 | |
and | logical and | variable1 and variable2 | |
or | logical or | variable1 or variable2 |
MicroPython is a lean and efficient implementation of Python with libraries similar to those in Python. [26] Some standard Python libraries have an equivalent library in MicroPython renamed to distinguish between the two. MicroPython libraries are smaller with less popular features removed or modified to save memory. [20]
The three types of libraries in MicroPython: [20]
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. [20]
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 |
binascii | functions for converting between binary and ASCII |
collections | operations for collections and container types that hold various objects |
errno | provides access to error codes |
hashlib | operations for binary hash algorithms |
heapq | operations to implement heap queue algorithm |
io | operations for handling input/output streams |
json | handles conversion between JSON documents and Python objects |
os | functions for filesystem access and basic operating system functions |
re | implements regular expression matching operations |
select | functions for handling events on multiple streams |
socket | connecting to sockets (networks), providing access to socket interface |
struct | performs conversions to Python objects by packing and unpacking primitive data types |
time | provides time and date function, including measuring time intervals and implementing delays |
zlib | operations to decompress binary data |
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 |
ctypes | access binary data structures |
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. [21]
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. [20]
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. [20] Installing MicroPython on microcontrollers is well documented and user-friendly. [21] 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. [18]
The two types of boards used to run MicroPython: [20]
To move a 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. [20]
The pyboard is the official MicroPython microcontroller board which fully supports MicroPython's software features. The pyboard's hardware features include: [5]
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]
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]
Hello world program:
# print to serial consoleprint('Hello, World!')
Importing + turning on a LED:
importpyb# turn LED onpyb.LED(1).on()
Reading a file + loop:
importos# open and read a filewithopen('/readme.txt')asf:print(f.read())
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.
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. [21]
The main uses of MicroPython can be generalised into 3 categories: [21]
Implementation of MicroPython can differ depending on the availability of standard and supporting libraries and the microcontroller's flash memory and RAM size. [21]
Bytecode is a form of instruction set designed for efficient execution by a software interpreter. Unlike human-readable source code, bytecodes are compact numeric codes, constants, and references that encode the result of compiler parsing and performing semantic analysis of things like type, scope, and nesting depths of program objects.
AVR is a family of microcontrollers developed since 1996 by Atmel, acquired by Microchip Technology in 2016. These are modified Harvard architecture 8-bit RISC single-chip microcontrollers. AVR was one of the first microcontroller families to use on-chip flash memory for program storage, as opposed to one-time programmable ROM, EPROM, or EEPROM used by other microcontrollers at the time.
Unified Extensible Firmware Interface is a specification that defines the architecture of the platform firmware used for booting the computer hardware and its interface for interaction with the operating system. Examples of firmware that implement the specification are AMI Aptio, Phoenix SecureCore, TianoCore EDK II, InsydeH2O. UEFI replaces the BIOS which was present in the boot ROM of all personal computers that are IBM PC compatible, although it can provide backwards compatibility with the BIOS using CSM booting. Intel developed the original Extensible Firmware Interface (EFI) specification. Some of the EFI's practices and data formats mirror those of Microsoft Windows. In 2005, UEFI deprecated EFI 1.10.
Trusted Platform Module (TPM) is an international standard for a secure cryptoprocessor, a dedicated microcontroller designed to secure hardware through integrated cryptographic keys. The term can also refer to a chip conforming to the standard ISO/IEC 11889.
The boot ROM is a type of ROM that is used for booting a computer system. There are two types: a mask boot ROM that cannot be changed afterwards and a boot EEPROM, which can contain an UEFI implementation.
The Parallax P8X32A Propeller is a multi-core processor parallel computer architecture microcontroller chip with eight 32-bit reduced instruction set computer (RISC) central processing unit (CPU) cores. Introduced in 2006, it is designed and sold by Parallax, Inc.
Arduino is an Italian open-source hardware and software company, project, and user community that designs and manufactures single-board microcontrollers and microcontroller kits for building digital devices. Its hardware products are licensed under a CC BY-SA license, while the software is licensed under the GNU Lesser General Public License (LGPL) or the GNU General Public License (GPL), permitting the manufacture of Arduino boards and software distribution by anyone. Arduino boards are available commercially from the official website or through authorized distributors.
The .NET Micro Framework (NETMF) is a .NET Framework platform for resource-constrained devices with at least 512 kB of flash and 256 kB of random-access memory (RAM). It includes a small version of the .NET Common Language Runtime (CLR) and supports development in C#, Visual Basic .NET, and debugging using Microsoft Visual Studio. NETMF features a subset of the .NET base class libraries, an implementation of Windows Communication Foundation (WCF), a GUI framework loosely based on Windows Presentation Foundation (WPF), and a Web Services stack based on Simple Object Access Protocol (SOAP) and Web Services Description Language (WSDL). NETMF also features added libraries specific to embedded applications. It is free and open-source software released under Apache License 2.0.
Adafruit Industries is an open-source hardware company based in New York, United States. It was founded by Limor Fried in 2005. The company designs, manufactures and sells electronics products, electronics components, tools, and accessories. It also produces learning resources, including live and recorded videos about electronics, technology, and programming.
XMC is a family of microcontroller ICs by Infineon. The XMC microcontrollers use the 32-bit RISC ARM processor cores from ARM Holdings, such as Cortex-M4F and Cortex-M0. XMC stands for "cross-market microcontrollers", meaning that this family can cover due to compatibility and configuration options, a wide range in industrial applications. The family supports three essential trends in the industry: It increases the energy efficiency of the systems, supports a variety of communication standards and reduces software complexity in the development of the application's software environment with the parallel released eclipse-based software tool DAVE.
LEGO Mindstorms EV3 is the third generation robotics kit in LEGO's Mindstorms line. It is the successor to the second generation LEGO Mindstorms NXT kit. The "EV" designation refers to the "evolution" of the Mindstorms product line. "3" refers to the fact that it is the third generation of computer modules - first was the RCX and the second is the NXT. It was officially announced on January 4, 2013, and was released in stores on September 1, 2013. The education edition was released on August 1, 2013. There are many competitions using this set, including the FIRST LEGO League Challenge and the World Robot Olympiad, sponsored by LEGO.
The Micro Bit is an open source hardware ARM-based embedded system designed by the BBC for use in computer education in the United Kingdom. It was first announced on the launch of BBC's Make It Digital campaign on 12 March 2015 with the intent of delivering 1 million devices to pupils in the UK. The final device design and features were unveiled on 6 July 2015 whereas actual delivery of devices, initially planned for September 2015 to schools and October 2015 to general public, began on 10 February 2016.
NodeMCU is a low-cost open source IoT platform. It initially included firmware which runs on the ESP8266 Wi-Fi SoC from Espressif Systems, and hardware which was based on the ESP-12 module. Later, support for the ESP32 32-bit MCU was added.
The ESP8266 is a low-cost Wi-Fi microchip, with built-in TCP/IP networking software, and microcontroller capability, produced by Espressif Systems in Shanghai, China.
GoWarrior is an open-source and community-supported computing platform. GoWarrior is designed for the world of makers, hackers, educators, hobbyists, and newbies to build electronics projects. It offers a complete package of hardware, software and cloud service.
Apache Mynewt is a modular real-time operating system for connected Internet of things (IoT) devices that must operate for long times under power, memory, and storage constraints. It is free and open-source software incubating under the Apache Software Foundation, with source code distributed under the Apache License 2.0, a permissive license that is conducive to commercial adoption of open-source software.
The Soundart Chameleon was a hardware synthesizer module, designed by the Spanish company Soundart. The name Chameleon comes from the fact that the machine was able to change its "skins", which are different sound engines. The Chameleons were produced from 2002 to 2004, until the company went bankrupt.
CircuitPython is an open-source derivative of the MicroPython programming language targeted toward students and beginners. Development of CircuitPython is supported by Adafruit Industries. It is a software implementation of the Python 3 programming language, written in C. It has been ported to run on several modern microcontrollers.
Espruino is an open-source JavaScript interpreter for single board microcontrollers. It is designed for devices with small amounts of RAM.
RP2040 is a 32-bit dual ARM Cortex-M0+ microcontroller integrated circuit by Raspberry Pi Ltd. In January 2021, it was released as part of the Raspberry Pi Pico board.