MicroPython is a
software
Software consists of computer programs that instruct the Execution (computing), execution of a computer. Software also includes design documents and specifications.
The history of software is closely tied to the development of digital comput ...
implementation of a
programming language
A programming language is a system of notation for writing computer programs.
Programming languages are described in terms of their Syntax (programming languages), syntax (form) and semantics (computer science), semantics (meaning), usually def ...
largely compatible with
Python 3, written in
C, that is optimized to run on a
microcontroller
A microcontroller (MC, uC, or μC) or microcontroller unit (MCU) is a small computer on a single integrated circuit. A microcontroller contains one or more CPUs (processor cores) along with memory and programmable input/output peripherals. Pro ...
.
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.
MicroPython does have an
inline assembler
In computer programming, an inline assembler is a feature of some compilers that allows low-level code written in assembly language to be embedded within a program, among code that otherwise has been compiled from a high-level language, higher-leve ...
, 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
GitHub () is a Proprietary software, proprietary developer platform that allows developers to create, store, manage, and share their code. It uses Git to provide distributed version control and GitHub itself provides access control, bug trackin ...
under the
MIT License
The MIT License is a permissive software license originating at the Massachusetts Institute of Technology (MIT) in the late 1980s. As a permissive license, it puts very few restrictions on reuse and therefore has high license compatibility.
Unl ...
.
History

MicroPython was originally created by the Australian programmer Damien George, after a successful
Kickstarter
Kickstarter, PBC is an American Benefit corporation, public benefit corporation based in Brooklyn, New York City, that maintains a global crowdfunding platform focused on creativity. The company's stated mission is to "help bring creative project ...
-backed campaign in 2013. The original Kickstarter campaign released MicroPython with an
STM32F4-powered development board "pyboard". In the meantime MicroPython has been developed to support a number of
ARM based architectures.
The ports supported in the mainline are ARM Cortex-M (many
STM32
boards,
RP2040 boards, TI CC3200/WiPy, Teensy boards, Nordic nRF series, SAMD21 and SAMD51),
ESP8266,
ESP32,
16-bit PIC, Unix, Windows, Zephyr, and JavaScript.
Also, there are many forks for a variety of systems and hardware platforms not supported in the mainline.
In 2016, a version of MicroPython for the
BBC
The British Broadcasting Corporation (BBC) is a British public service broadcaster headquartered at Broadcasting House in London, England. Originally established in 1922 as the British Broadcasting Company, it evolved into its current sta ...
Micro Bit was created as part of the
Python Software Foundation
The Python Software Foundation (PSF) is an American nonprofit organization devoted to the Python programming language, launched on March 6, 2001. The mission of the foundation is to foster development of the Python community and is responsible fo ...
's contribution to the Micro Bit partnership with the BBC.
In July 2017, MicroPython was forked to create
CircuitPython
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 programming langua ...
, 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.
In 2017,
Microsemi
Microsemi Corporation was an Aliso Viejo, California-based provider of semiconductor and system solutions for aerospace & defense, communications, data center and industrial markets.
In February 2018, it was announced that Chandler, Arizona-ba ...
made a MicroPython port for
RISC-V
RISC-V (pronounced "risk-five") is an open standard instruction set architecture (ISA) based on established reduced instruction set computer (RISC) principles. The project commenced in 2010 at the University of California, Berkeley. It transfer ...
(RV32 and RV64) architecture.
In April 2019, a version of MicroPython for the
Lego Mindstorms EV3 was created.
In January 2021, a MicroPython port for the RP2040 (ARM Cortex-M0+, on
Raspberry Pi Pico and others) was created.
Features
Ability to run Python
MicroPython has the ability to run Python, allowing users to create simple and easy-to-understand programs.
MicroPython supports many standard Python libraries, supporting more than 80% of the features of Python's most used libraries.
MicroPython was designed specifically to support the typical performance gap between microcontrollers and Python.
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
An operating system (OS) is system software that manages computer hardware and software resources, and provides common daemon (computing), services for computer programs.
Time-sharing operating systems scheduler (computing), schedule tasks for ...
.
Code portability
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.
Modules
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
In computing
Computing is any goal-oriented activity requiring, benefiting from, or creating computer, computing machinery. It includes the study and experimentation of algorithmic processes, and the development of both computer hardware, h ...
. 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
In computing, a compiler is a computer program that translates computer code written in one programming language (the ''source'' language) into another language (the ''target'' language). The name "compiler" is primarily used for programs tha ...
and uploading to the microcontroller where the library will be available using Python's import command to be used repeatedly.
Read–eval–print loop
The
read–eval–print loop
A read–eval–print loop (REPL), also termed an interactive toplevel or language shell, is a simple interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user; a program written ...
(REPL) allows a developer to enter individual lines of code and have them run immediately on a
terminal.
Linux
Linux ( ) is a family of open source Unix-like operating systems based on the Linux kernel, an kernel (operating system), operating system kernel first released on September 17, 1991, by Linus Torvalds. Linux is typically package manager, pac ...
-based and
macOS
macOS, previously OS X and originally Mac OS X, is a Unix, Unix-based operating system developed and marketed by Apple Inc., Apple since 2001. It is the current operating system for Apple's Mac (computer), Mac computers. With ...
systems have terminal
emulator
In computing, an emulator is Computer hardware, hardware or software that enables one computer system (called the ''host'') to behave like another computer system (called the ''guest''). An emulator typically enables the host system to run sof ...
s that can be used to create a direct connection to a MicroPython device's REPL using a serial
USB
Universal Serial Bus (USB) is an industry standard, developed by USB Implementers Forum (USB-IF), for digital data transmission and power delivery between many types of electronics. It specifies the architecture, in particular the physical ...
connection. The REPL assists with the immediate testing of parts of an
application as each part of the code can be run and the results visually examined. Once different parts of code are loaded into the REPL, additional REPL features can be used to experiment with that code's functionality.
Helpful REPL commands (once connected to a serial console):
* : keyboard interrupt
* : reload
* : help message
* : lists built-in
modules
* : lists all the pins on your microcontroller board that are available to be used in a program's code
Limitations
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, 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.
MicroPython has more limited hardware support in the microcontroller market than other popular platforms, like
Arduino
Arduino () is an Italian open-source hardware and open-source software, software company, project, and user community that designs and manufactures single-board microcontrollers and microcontroller kits for building digital devices. Its hardwar ...
with a smaller number of microcontroller choices that support the language.
MicroPython does not include an
integrated development environment
An integrated development environment (IDE) is a Application software, software application that provides comprehensive facilities for software development. An IDE normally consists of at least a source-code editor, build automation tools, an ...
(IDE) or specific editor unlike other platforms.
Syntax and semantics
MicroPython's
syntax
In linguistics, syntax ( ) is the study of how words and morphemes combine to form larger units such as phrases and sentences. Central concerns of syntax include word order, grammatical relations, hierarchical sentence structure (constituenc ...
is adopted from Python, due to its clear and easy-to-understand style and power. Unlike most other programming languages less punctuation is used with fewer syntactical machinations in order to prioritise readability.
Code blocks
MicroPython adopts Python's code block style, with code specific to a particular
function, condition or loop being indented.
This differs from most other languages which typically use symbols or keywords to delimit blocks.
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.
A colon (:) is the key symbol used to indicate the ending of a condition statement.
The indent size is equivalent to one tab or 4 spaces.
Operations
MicroPython has the ability to perform various mathematical operations using primitive and logical operations.
Libraries
MicroPython is a lean and efficient implementation of Python with libraries similar to those in Python.
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
Memory is the faculty of the mind by which data or information is encoded, stored, and retrieved when needed. It is the retention of information over time for the purpose of influencing future action. If past events could not be remembe ...
.
The three types of libraries in MicroPython:
* 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
A microcontroller (MC, uC, or μC) or microcontroller unit (MCU) is a small computer on a single integrated circuit. A microcontroller contains one or more CPUs (processor cores) along with memory and programmable input/output peripherals. Pro ...
) and the availability of libraries may differ. Some functions and classes in a module or the entire module may be unavailable or altered.
Custom MicroPython libraries
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.
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.
Supporting hardware
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.
Installing MicroPython on microcontrollers is well documented and user-friendly.
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.
The two types of boards used to run MicroPython:
* 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
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
A computer is a machine that can be Computer programming, programmed to automatically Execution (computing), carry out sequences of arithmetic or logical operations (''computation''). Modern digital electronic computers can perform generic set ...
, 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.
Pyboard
The pyboard is the official MicroPython microcontroller board which fully supports MicroPython's software features. The pyboard's hardware features include:
*
microcontroller
A microcontroller (MC, uC, or μC) or microcontroller unit (MCU) is a small computer on a single integrated circuit. A microcontroller contains one or more CPUs (processor cores) along with memory and programmable input/output peripherals. Pro ...
(MCU consisting of
CPU, flash
ROM
Rom, or ROM may refer to:
Biomechanics and medicine
* Risk of mortality, a medical classification to estimate the likelihood of death for a patient
* Rupture of membranes, a term used during pregnancy to describe a rupture of the amniotic sac
* ...
and
RAM)
*
microUSB connector
*
micro-SD
Secure Digital (SD) is a proprietary, non-volatile, flash memory card format developed by the SD Association (SDA). Owing to their compact size, SD cards have been widely adopted in a variety of portable consumer electronics, including digi ...
card slot
* IO pins
*
switch
In electrical engineering, a switch is an electrical component that can disconnect or connect the conducting path in an electrical circuit, interrupting the electric current or diverting it from one conductor to another. The most common type o ...
es,
LEDs,
servo ports,
real time clock,
accelerometer
An accelerometer is a device that measures the proper acceleration of an object. Proper acceleration is the acceleration (the rate of change (mathematics), rate of change of velocity) of the object relative to an observer who is in free fall (tha ...
The booting process
The pyboard contains an internal drive with 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 is stored on the board flash memory to skip the SD card for the booting process.
Boot modes
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.
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.
the boot modes are:
* 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
* if red and green LEDs flash alternatively then the python script has an error, and you must use the REPL to
debug
In engineering, debugging is the process of finding the root cause, workarounds, and possible fixes for bugs.
For software, debugging tactics can involve interactive debugging, control flow analysis, log file analysis, monitoring at the ap ...
.
* if all 4 LEDs cycle on and off then there is a hard fault which cannot be recovered from and requires a hard reset.
Programming examples
Source:
Hello world program:
# Print to serial console
print("Hello, World!")
Importing + turning on a LED:
import pyb
# Turn LED on
pyb.LED(1).on()
Reading a file + loop:
import os
# Open and read a file
with open("/readme.txt") as f:
print(f.read())
Bytecode
MicroPython includes a
cross compiler which generates MicroPython
bytecode
Bytecode (also called portable code or p-code) 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 (normal ...
(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
In computing, a virtual machine (VM) is the virtualization or emulator, emulation of a computer system. Virtual machines are based on computer architectures and provide the functionality of a physical computer. Their implementations may involve ...
which can run the precompiled ''mpy'' programs.
Implementation and uses
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.
The main uses of MicroPython can be generalised into 3 categories:
* 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.
See also
*
Espruino
*
CircuitPython
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 programming langua ...
References
External links
*
*
GOTO 2016 • MicroPython & the Internet of Things • Damien Georgeon
YouTube
YouTube is an American social media and online video sharing platform owned by Google. YouTube was founded on February 14, 2005, by Steve Chen, Chad Hurley, and Jawed Karim who were three former employees of PayPal. Headquartered in ...
* • Tutorials by Tony DiCola /
Adafruit
{{Python (programming language)
BBC computer literacy projects
Free software programmed in C
Microcontroller software
Python (programming language)
Software using the MIT license