programming
MicroPython is a lean and efficient implementation of the Python 3 programming language that is optimised to run on a microcontroller. These tiny electronic components are at the heart of many modern gadgets such as sensors, motors and screens, or even more complex things like robots and smart devices.
Unlike Python, a high-level programming language used for general-purpose software development, MicroPython has been tailored specifically for microcontrollers. This means we benefit from having a powerful language like Python in environments where resources are limited and efficiency is key.
We find the roots of MicroPython in an effort to empower hardware enthusiasts and professionals alike to program microcontroller units (MCUs) with ease and flexibility. Typically, programming microcontrollers requires working with low-level languages like C or C++, but MicroPython simplifies this process.
It brings with it a subset of the Python standard library, which means we can use many of the familiar Python functions and modules, albeit within the constraints imposed by the hardware.
By adopting MicroPython, we can significantly reduce development time and effort. It supports interactive prompts, which means we can execute commands and see the results immediately, similar to working with Python on a PC or server.
This feature is exceptionally useful for rapid prototyping, debugging, and learning how the hardware interacts with the software. Moreover, MicroPython's syntax and libraries are designed to interoperate seamlessly with the hardware, giving us near-direct control over the functionality of MCU peripherals.
We explore the genesis of MicroPython, from its initial Kickstarter campaign to the distinctions that set it apart from CPython. Our journey unpacks the development of this lean interpreter through pivotal milestones and its unique position in the programming landscape.
In 2013, Damien George launched a Kickstarter campaign to fund the development of MicroPython, a new version of Python designed to run on microcontrollers. The campaign was a success, raising over £97,000, significantly surpassing its initial goal.
George, with a background in theoretical physics and software engineering, aimed to bring the ease of Python programming to the hardware world.
The source code of MicroPython was made publicly available on GitHub, under the MIT license, allowing for open collaboration. The project rapidly evolved, gaining contributions from developers globally.
Two central repositories, micropython-lib
and micropython
, form the core of the project, hosting the additional libraries and the main interpreter code respectively.
CPython is the default and most widely-used implementation of the Python programming language. It is written in C and Python, and is the reference implementation that other implementations of Python aim to be compatible with.
MicroPython, though inspired by CPython, is tailored for constrained environments such as microcontrollers. While CPython is the reference implementation of Python in its entirety, MicroPython incorporates just the essentials, aiming for minimal memory footprint and for a better management of hardware constraints.
It is important to note that MicroPython may not include all of the standard libraries or features found in CPython. However, it retains a high-level syntactical similarity, ensuring familiar readability and maintainability.
Exploring MicroPython requires an understanding of its project fundamentals, the firmware and boot process it utilises and the microcontrollers it supports.
As already anticipated, MicroPython is an efficient implementation of Python 3, designed to run on microcontrollers and in constrained environments. At its core, the project's aim is to make programming digital devices as simple and accessible as possible.
The project optimises the Python language to run on hardware with limited processing power and memory. This involves a combination of rewriting core Python libraries to be more compact and devising a cross-compiler that can convert Python scripts into optimised bytecode for various microcontrollers.
MicroPython includes a comprehensive standard library, bringing the joy of Python to smaller devices previously restricted to using traditional languages like C or assembly.
The firmware in MicroPython acts as the intermediary between the software and hardware. We have designed the firmware to be lightweight and highly portable, supporting a wide range of devices.
When you flash this firmware onto a microcontroller, it encapsulates the MicroPython interpreter and a portion of the standard Python library.
Upon device boot-up, the bootloader, a small program stored in the chip's non-volatile memory, initiates the process.
This bootloader is responsible for setting up basic hardware and running the main firmware, which includes the MicroPython interpreter.
The boot process should be fast and reliable and should allow you to start running Python scripts almost immediately after powering up the device.
MicroPython supports a variety of microcontroller architectures, including PIC, STM32, and ESP families.
ESP8266 and ESP32: Espressif's ESP8266 and ESP32 have gained significant traction due to their integrated Wi-Fi capabilities.
Our MicroPython firmware for these devices not only includes standard features but also modules for network communication, allowing for the easy creation of connected gadgets.
We will guide you through installing MicroPython on various operating systems, configuring the environment for code deployment, and understanding the necessary ports and drivers. Our aim is to equip you with the practical steps required for a smooth MicroPython set up.
For installing MicroPython, we need to ensure compatibility with our operating system. We can install it on Linux, macOS, and Windows with specific procedures for each:
python3
and pip3
. Then, install MicroPython using pip3 install micropython
.brew install micropython
.After the installation process, we must then proceed to configure our environment by following the necessary steps:
Identify the board's serial port.
ls /dev/tty.*
.Use a tool such as mpfshell
or rshell
for deployment:
pip3 install mpfshell
(Linux/macOS) or pip install mpfshell
(Windows).mpfshell
followed by put your_script.py
.The correct ports and drivers are critical for communication between our computer and the MicroPython board:
Ensure you have the necessary drivers installed:
We'll explore the essential hardware components of MicroPython, focusing on the primary boards available, the microcontrollers they employ, and how they compare with other popular platforms such as the Arduino and the Raspberry Pi Pico.
PyBoard is the official MicroPython development board, designed to provide a low-level hardware interface for running MicroPython scripts. It comes in a range of variants, for example:
MicroPython supports a variety of microcontrollers, each differing in capacities and capabilities:
Board | Microcontroller | RAM |
---|---|---|
PyBoard v1.1 | STM32F405RG | 192 KB |
ESP8266-based boards | Tensilica L106 | 96 KB |
ESP32-based boards | Tensilica Xtensa LX6 | 520 KB |
RAM is crucial because it determines how complex the scripts and programs can be. The more RAM available, the more sophisticated the applications can be.
While MicroPython can run on a variety of boards:
In this section we are going to explore the basics of working with this programming language, highlighting key features like its syntax, the interactive REPL environment, as well as how it integrates with modules, libraries, and version control systems like Git.
MicroPython is fundamentally similar to Python 3, which means that it shares the same basic syntax and programming paradigms.
Variables, control structures, functions, and classes all work as expected in Python. For example, creating a variable is as straightforward as counter = 10
and writing a loop is done through the familiar for item in range(counter):
syntax.
Python’s inherent readability and conciseness are retained, making it possible for anyone with Python 3 experience to transition to MicroPython with relative ease.
REPL stands for Read-Eval-Print-Loop, and it is a dynamic programming environment included within MicroPython.
It allows us to execute Python commands one at a time and see immediate results. We can engage with REPL directly on the microcontroller by connecting to it via a serial interface or over the network.
>>> print("Hello, MicroPython!")
Hello, MicroPython!
The above interaction in the REPL is quintessential in demonstrating how immediate feedback in programming tasks can speed up the learning and debugging processes.
MicroPython supports modules and libraries which can be imported to extend its functionalities.
This is vital as it allows us to repurpose and include complex functionalities without writing them from scratch. For instance, using a networking module with import network
allows us to connect a device to the internet.
To manage and share code effectively, MicroPython integrates with Git comfortably.
This means we can collaborate on projects by pushing and pulling code from repositories, tracking changes, and managing versions which are crucial practices for sustainable software development.
MicroPython serves as a catalyst for a wide array of project development initiatives, especially in domains where the Internet of Things (IoT), home automation, and educational projects converge.
As an efficient and streamlined implementation of Python 3, MicroPython is tailored specifically for microcontrollers and compact system on chips (SoCs), enabling us not only to write clean and simple code but also to integrate various functionalities with ease and precision.
We harness MicroPython for developing Internet of Things (IoT) products as it offers a seamless experience for coding connected devices. Our projects typically involve:
For home automation and robotics, MicroPython stands out as an ideal choice due to its versatility:
In these applications, our project development focuses on real-time response and reliability.
MicroPython is well-suited for STEM education projects with its straightforward syntax and fast learning curve. It's possible to design both educational kits and interactive experiments, such as:
MicroPython allows experienced developers to push the boundaries beyond simple scripting. It's possible to customise behaviour, optimise performance and maintain custom versions of MicroPython using advanced features like compiling custom firmware and manipulating bytecode.
In MicroPython, inline assembler gives us the capability to write assembly language within our Python code.
# Example of inline assembler to add two integers
@micropython.asm_thumb
def add(r0, r1):
add(r0, r0, r1)
This harnesses the processor's full speed and control, a critical feature when exact timing or specific processor instructions are necessary.
Managing bytecode lets us fine-tune the Python virtual machine. For instance, we can inspect, modify, or extend bytecode operations for performance gains or custom functionalities.
# Example of bytecode manipulation in MicroPython
import micropython
micropython.bytecode()
We can compile MicroPython ourselves to build custom firmware.
This process allows the inclusion or removal of modules, optimisation for specific hardware, or even integration of new features.
The steps to build custom firmware typically involve:
# Cloning the MicroPython repository
git clone https://github.com/micropython/micropython.git
# Compiling the source on a Unix-based system
make -C mpy-cross && make -C unix
To maintain a personalised version of MicroPython, it's enough to fork the official repository on GitHub.
This allows to experiment with changes, track them and potentially contribute back to the MicroPython community.
Our workflow for managing a MicroPython fork includes:
# Syncing our fork with the original repository
git fetch upstream
git checkout master
git merge upstream/master
git push
MicroPython facilitates connectivity and the interfacing of a wide array of peripherals, making it an ideal choice for developing Internet of Things (IoT) applications with ease.
Robust support is provided for Wifi and Bluetooth connectivity in MicroPython, unlocking the potential for a myriad of IoT applications.
The WiFi stack allows seamless connection to wireless networks, enabling MicroPython devices to communicate over the internet.
Utilising the network
module, we can scan for available networks, establish connections, and even set up MicroPython as an access point.
For Bluetooth, the bluetooth
module available in MicroPython provides an interface to configure Bluetooth Low Energy (BLE) connections, enabling devices to interact with BLE-enabled sensors and peripherals.
WiFi
Bluetooth
As for IoT protocols, MicroPython supports MQTT (Message Queuing Telemetry Transport) out of the box – a lightweight messaging protocol ideal for small sensors and mobile devices.
Interfacing with sensors, actuators, and displays is made straightforward through MicroPython's extensive library support.
The machine
and pyb
modules enable to communicate with various electronic components via GPIO (General Purpose Input/Output) pins, PWM (Pulse Width Modulation), ADC (Analog to Digital Converter), and more.
Sensors
Actuators
Displays
This ensures that projects involving real-time monitoring and control, such as smart thermostats or robotic arms, can be efficiently executed with concise code.
MicroPython excels at facilitating serial communication and networking.
Through the uos
module, it provides functionalities to access the UART (Universal Asynchronous Receiver/Transmitter) for serial communications, a fundamental capability for projects that require data transmission between devices.
Serial Connections
In addition to UART, MicroPython supports higher-level networking protocols, offering TCP/IP and UDP through the usocket
module, enabling robust network communication for MicroPython devices.
Networking
This becomes pivotal in complex IoT systems where multiple devices need to exchange data efficiently and reliably.
It's possible to significantly enhance the functionalities of MicroPython systems by incorporating additional storage, power management solutions, and a variety of accessories.
SD Cards: the storage capacity of MicroPython devices can be expanded by inserting SD cards. This is particularly useful for data logging applications where we need to store large amounts of data.
Flash Memory: many MicroPython boards are equipped with onboard flash memory. If further memory is needed, you can consider adding additional flash chips to the available footprints on some boards.
Battery Management: efficient power management is crucial for portable MicroPython projects. You can easily integrate charge controllers and power management ICs to ensure a steady energy supply and battery longevity.
Battery Connectivity: many boards support connections to LiPo batteries, enabling untethered operations. Careful consideration of battery specifications and connectivity is necessary to match the device's power requirements.
You can find some support in the MicroPython community when it comes to overcoming obstacles encountered while programming with MicroPython. Whether you are grappling with common issues or seeking extensive knowledge, the forums and wiki are invaluable assets for support. Here below we'll present to you the three most common issues and the suggested method for fixing them quickly.
Here are some frequent problems and their solutions:
Would you like to receive special insights on industrial electronics?