What is MicroPython? An Introduction to Simplified Python-3 for Microcontrollers

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.

MicroPython is a small, lightweight programming language. It runs on microcontrollers and is used for embedded systems and Internet of Things (IoT) devices

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.

Origins of MicroPython

A computer screen displays the code for MicroPython, surrounded by electronic components and circuitry, symbolizing the origins of the programming language

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.

Damien George and the KickStarter Campaign

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 Evolution of MicroPython Source Code

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.

MicroPython vs CPython: Understanding the Differences

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.

Core Concepts of MicroPython

A circuit board with MicroPython logo, surrounded by electronic components and code snippets

Exploring MicroPython requires an understanding of its project fundamentals, the firmware and boot process it utilises and the microcontrollers it supports.

MicroPython Project Fundamentals

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.

Firmware and Boot Process

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.

Microcontroller Basics: PIC, STM32, and ESP

MicroPython supports a variety of microcontroller architectures, including PIC, STM32, and ESP families.

  • PIC: Microchip's PIC microcontrollers are popular due to their simplicity and affordability.
  • STM32: STMicroelectronics' STM32 family offers high performance with a range of options that provide a balance of power consumption and processing capability.
  • 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.

Setting Up MicroPython

A desk with a computer, USB cable, and microcontroller board. On the screen, code for setting up MicroPython is displayed

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.

Installation on Multiple Operating Systems

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:

  • Linux: Use the package manager to install python3 and pip3. Then, install MicroPython using pip3 install micropython.
  • macOS: Install Homebrew if it's not already on your system, then use brew install micropython.
  • Windows: Download the Windows installer from the MicroPython website and follow the setup wizard.

Configuring the Environment and Code Deployment

After the installation process, we must then proceed to configure our environment by following the necessary steps:

  1. Connect the MicroPython board to our computer.
  2. Identify the board's serial port.

    • Linux & macOS: Check available ports using ls /dev/tty.*.
    • Windows: Look in Device Manager under 'Ports (COM & LPT)'.
  3. Use a tool such as mpfshell or rshell for deployment:

    • Install: pip3 install mpfshell (Linux/macOS) or pip install mpfshell (Windows).
    • Deploy code: Navigate to your project directory and run mpfshell followed by put your_script.py.

Understanding Ports and Drivers

The correct ports and drivers are critical for communication between our computer and the MicroPython board:

  • Identify the microcontroller board's port as mentioned above.
  • Ensure you have the necessary drivers installed:

    • CP210x or FTDI drivers are common for MicroPython boards.
    • You can download the drivers from the manufacturers' websites.

MicroPython Boards and Hardware

A MicroPython board and hardware are arranged on a workbench, with a computer screen displaying code and a soldering iron nearby

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.

Official PyBoard and Its Variants

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:

  • PyBoard v1.1: Includes a STM32F405RG microcontroller with 192 KB RAM.
  • PyBoard D-series: Enhanced versions with connectivity options such as Wi-Fi and Bluetooth.

Supported Microcontrollers and RAM Considerations

MicroPython supports a variety of microcontrollers, each differing in capacities and capabilities:

BoardMicrocontrollerRAM
PyBoard v1.1STM32F405RG192 KB
ESP8266-based boardsTensilica L10696 KB
ESP32-based boardsTensilica Xtensa LX6520 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.

Compatibility with Other Boards like Arduino and Raspberry Pi Pico

While MicroPython can run on a variety of boards:

  • Arduino: MicroPython isn't natively supported; however, there are workarounds via special firmware such as uPyCraft.
  • Raspberry Pi Pico: It has direct support for MicroPython, featuring an RP2040 microcontroller designed by the Raspberry Pi Foundation with 264 KB of SRAM.

Programming with MicroPython

A circuit board with MicroPython code displayed on a computer screen, surrounded by various electronic components and a soldering iron

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.

Basic Syntax and Python 3 Features

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.

Interactive Programming with REPL

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.

Modules, Libraries, and Git Integration

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.

Project 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.

Internet of Things and Connected Devices

We harness MicroPython for developing Internet of Things (IoT) products as it offers a seamless experience for coding connected devices. Our projects typically involve:

  • Sensor Integration: utilising digital and analogue sensors to capture environmental data.
  • Data Processing: interfacing MicroPython with the device's core to process and interpret sensor data.
  • Communication: implementing networking protocols such as MQTT or HTTP for data transmission.

Home Automation and Robotics Projects

For home automation and robotics, MicroPython stands out as an ideal choice due to its versatility:

  • Controller Logic: writing scripts to manage devices like smart thermostats or lighting systems.
  • Robotics: programming movement and tasks for robots using actuators and motor controllers.

In these applications, our project development focuses on real-time response and reliability.

How to build STEM Education Projects

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:

  • Interactive Tools: creating devices that demonstrate scientific principles.
  • Learning Platforms: building platforms that help learners write and execute their own MicroPython code.

Advanced Features and Customisation

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.

Inline Assembler and Bytecode Manipulation

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()

Compilation and Building Custom Firmware

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:

  1. Cloning the MicroPython repository from GitHub.
  2. Configuring the build options.
  3. Compiling the source code.
  4. Flashing the compiled firmware onto our hardware.
# 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

Creating and Managing a MicroPython Fork on GitHub

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:

  • Keeping our fork updated with the upstream repository
  • Branching for new features or bug fixes
  • Submitting pull requests for our contributions
# Syncing our fork with the original repository
git fetch upstream
git checkout master
git merge upstream/master
git push

Connectivity and Peripherals

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.

Working with Wifi, Bluetooth, and IoT Protocols

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

    • Network Scanning
    • Connection Handling
    • Access Point Configuration
  • Bluetooth

    • BLE Advertising
    • BLE Services and Characteristics
    • Secure Pairing

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 Sensors, Actuators, and Displays

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

    • Data Reading (e.g., temperature, humidity)
    • Interfacing ADC, I2C, SPI
  • Actuators

    • Controlling Motors
    • Driving LEDs and Relays
  • Displays

    • OLED and LCD Integration
    • Graphical and Text-based Output

This ensures that projects involving real-time monitoring and control, such as smart thermostats or robotic arms, can be efficiently executed with concise code.

Serial Communication and Networking

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

    • UART Configuration
    • Data Sending and Receiving

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

    • Socket Creation
    • TCP/IP and UDP Protocols
    • Client-Server Communication

This becomes pivotal in complex IoT systems where multiple devices need to exchange data efficiently and reliably.

Expansion and External Resources

It's possible to significantly enhance the functionalities of MicroPython systems by incorporating additional storage, power management solutions, and a variety of accessories.

Adding Storage with SD Card and Flash Memory

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.

Power Management and Battery Connectivity

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.

Troubleshooting and Community Support

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.

Common Issues and How to Solve Them

Here are some frequent problems and their solutions:

  1. Module Import Errors: at times, importing modules can return errors. To resolve this, you should check if the module name is correct and verify that it's included in the MicroPython firmware.
  2. Serial Connection Issues: issues with serial communication are typical. You can troubleshoot by ensuring that the correct port and baud rate are selected, and the cables are secure.
  3. Memory Allocation Errors: when running out of memory, streamlining code and managing resources efficiently by using iteration instead of recursion where possible, and freeing up memory, can help.

Cookie Settings

This website uses cookies. You can choose to allow or reject certain types hereunder. More information about their use can be found in ourprivacy policy.

They allow core website functionality. The website won’t work without them.

They serve to collect usage statistics, with anonymised IP, that help us improve the website.

Would you like to receive special insights on industrial electronics?

We protect your privacy and handle your data safely, according to the GDPR regulation. By checking here, you agree to the terms contained in our Privacy Policy
Contact Us