Convert Figma logo to code with AI

micropython logomicropython

MicroPython - a lean and efficient Python implementation for microcontrollers and constrained systems

19,758
7,898
19,758
1,717

Top Related Projects

CircuitPython - a Python implementation for teaching coding with microcontrollers

14,304

Espressif IoT Development Framework. Official development framework for Espressif SoCs.

Lua based interactive firmware for ESP8266, ESP8285 and ESP32

3,841

Lua + libUV + jIT = pure awesomesauce

Quick Overview

MicroPython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library and is optimized to run on microcontrollers and in constrained environments. It aims to be as compatible with normal Python as possible to allow easy development of applications.

Pros

  • Brings the simplicity and power of Python to embedded systems
  • Supports a wide range of microcontrollers and development boards
  • Includes a comprehensive standard library tailored for embedded systems
  • Active community and regular updates

Cons

  • Performance may be slower compared to low-level languages like C
  • Limited support for some advanced Python features due to resource constraints
  • May require more memory than bare-metal programming
  • Learning curve for developers new to embedded systems

Code Examples

  1. Blinking an LED:
from machine import Pin
import time

led = Pin(2, Pin.OUT)

while True:
    led.on()
    time.sleep(0.5)
    led.off()
    time.sleep(0.5)
  1. Reading an analog sensor:
from machine import ADC
import time

adc = ADC(0)

while True:
    value = adc.read()
    print("Sensor value:", value)
    time.sleep(1)
  1. Connecting to Wi-Fi:
import network

sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect('SSID', 'PASSWORD')

while not sta_if.isconnected():
    pass

print('Network config:', sta_if.ifconfig())

Getting Started

  1. Download the appropriate MicroPython firmware for your board from the official website.
  2. Flash the firmware to your board using the recommended tool (e.g., esptool for ESP32).
  3. Connect to your board's REPL (e.g., using PuTTY or screen).
  4. Start writing and running MicroPython code:
# Hello World
print("Hello, MicroPython!")

# Basic GPIO
from machine import Pin
led = Pin(2, Pin.OUT)
led.on()

For more detailed instructions, refer to the official MicroPython documentation and your board's specific guide.

Competitor Comparisons

CircuitPython - a Python implementation for teaching coding with microcontrollers

Pros of CircuitPython

  • Designed specifically for education and beginners
  • Extensive library support for Adafruit hardware
  • Simpler USB workflow with drag-and-drop file transfer

Cons of CircuitPython

  • Limited to specific microcontroller boards
  • Slower execution compared to MicroPython
  • Less memory-efficient than MicroPython

Code Comparison

MicroPython:

import machine
led = machine.Pin(2, machine.Pin.OUT)
led.on()

CircuitPython:

import board
import digitalio
led = digitalio.DigitalInOut(board.D13)
led.direction = digitalio.Direction.OUTPUT
led.value = True

Both MicroPython and CircuitPython are Python implementations for microcontrollers, but they have different focuses. MicroPython aims to be a lean and efficient implementation, while CircuitPython prioritizes ease of use and beginner-friendliness.

MicroPython supports a wider range of hardware and offers more low-level control, making it suitable for more advanced projects. CircuitPython, on the other hand, provides a more streamlined experience with its simplified API and extensive library support for Adafruit hardware.

The code comparison shows that CircuitPython uses a slightly more verbose syntax, which can be more intuitive for beginners but may require more memory.

14,304

Espressif IoT Development Framework. Official development framework for Espressif SoCs.

Pros of esp-idf

  • More comprehensive and feature-rich for ESP32 development
  • Better performance and lower-level control
  • Extensive documentation and official support from Espressif

Cons of esp-idf

  • Steeper learning curve, especially for beginners
  • Requires more code and setup for basic tasks
  • C/C++ based, which may be less accessible than Python

Code Comparison

MicroPython example:

import machine
import time

led = machine.Pin(2, machine.Pin.OUT)
while True:
    led.on()
    time.sleep(1)
    led.off()
    time.sleep(1)

esp-idf example:

#include "driver/gpio.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"

void app_main() {
    gpio_pad_select_gpio(GPIO_NUM_2);
    gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT);
    while (1) {
        gpio_set_level(GPIO_NUM_2, 1);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
        gpio_set_level(GPIO_NUM_2, 0);
        vTaskDelay(1000 / portTICK_PERIOD_MS);
    }
}

Lua based interactive firmware for ESP8266, ESP8285 and ESP32

Pros of NodeMCU firmware

  • Built-in support for WiFi and networking features
  • Extensive library of modules for various sensors and peripherals
  • Lua scripting language, which is easier for beginners to learn

Cons of NodeMCU firmware

  • Limited to specific ESP8266 and ESP32 hardware
  • Less memory-efficient compared to MicroPython
  • Slower execution speed for complex operations

Code comparison

NodeMCU (Lua):

wifi.setmode(wifi.STATION)
wifi.sta.config({ssid="MyNetwork", pwd="password"})
gpio.mode(4, gpio.OUTPUT)
gpio.write(4, gpio.HIGH)

MicroPython:

import network
import machine

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("MyNetwork", "password")
pin = machine.Pin(4, machine.Pin.OUT)
pin.on()

Both examples demonstrate WiFi connection and GPIO control, showcasing the syntax differences between Lua and Python. MicroPython's code is more Pythonic and may be more familiar to many developers, while NodeMCU's Lua syntax is concise but potentially less intuitive for those with Python experience.

3,841

Lua + libUV + jIT = pure awesomesauce

Pros of Luvit

  • Designed for high-performance networking and asynchronous I/O
  • Combines Lua's simplicity with libuv's power for efficient event-driven programming
  • Supports a wide range of operating systems and architectures

Cons of Luvit

  • Less suitable for microcontroller and embedded systems compared to MicroPython
  • Smaller community and ecosystem than MicroPython
  • Not optimized for constrained environments with limited resources

Code Comparison

MicroPython:

import machine
import time

led = machine.Pin(2, machine.Pin.OUT)
while True:
    led.toggle()
    time.sleep(1)

Luvit:

local timer = require('timer')

local function blink()
  print("Blink!")
end

timer.setInterval(1000, blink)

Summary

MicroPython is tailored for microcontrollers and embedded systems, offering a Python-like experience in resource-constrained environments. Luvit, on the other hand, focuses on high-performance networking and asynchronous I/O using Lua and libuv. While MicroPython excels in embedded applications, Luvit shines in building scalable network applications and services. The choice between them depends on the specific project requirements and target environment.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Unix CI badge STM32 CI badge Docs CI badge codecov

The MicroPython project

MicroPython Logo

This is the MicroPython project, which aims to put an implementation of Python 3.x on microcontrollers and small embedded systems. You can find the official website at micropython.org.

WARNING: this project is in beta stage and is subject to changes of the code-base, including project-wide name changes and API changes.

MicroPython implements the entire Python 3.4 syntax (including exceptions, with, yield from, etc., and additionally async/await keywords from Python 3.5 and some select features from later versions). The following core datatypes are provided: str(including basic Unicode support), bytes, bytearray, tuple, list, dict, set, frozenset, array.array, collections.namedtuple, classes and instances. Builtin modules include os, sys, time, re, and struct, etc. Some ports have support for _thread module (multithreading), socket and ssl for networking, and asyncio. Note that only a subset of Python 3 functionality is implemented for the data types and modules.

MicroPython can execute scripts in textual source form (.py files) or from precompiled bytecode (.mpy files), in both cases either from an on-device filesystem or "frozen" into the MicroPython executable.

MicroPython also provides a set of MicroPython-specific modules to access hardware-specific functionality and peripherals such as GPIO, Timers, ADC, DAC, PWM, SPI, I2C, CAN, Bluetooth, and USB.

Getting started

See the online documentation for the API reference and information about using MicroPython and information about how it is implemented.

We use GitHub Discussions as our forum, and Discord for chat. These are great places to ask questions and advice from the community or to discuss your MicroPython-based projects.

For bugs and feature requests, please raise an issue and follow the templates there.

For information about the MicroPython pyboard, the officially supported board from the original Kickstarter campaign, see the schematics and pinouts and documentation.

Contributing

MicroPython is an open-source project and welcomes contributions. To be productive, please be sure to follow the Contributors' Guidelines and the Code Conventions. Note that MicroPython is licenced under the MIT license, and all contributions should follow this license.

About this repository

This repository contains the following components:

  • py/ -- the core Python implementation, including compiler, runtime, and core library.
  • mpy-cross/ -- the MicroPython cross-compiler which is used to turn scripts into precompiled bytecode.
  • ports/ -- platform-specific code for the various ports and architectures that MicroPython runs on.
  • lib/ -- submodules for external dependencies.
  • tests/ -- test framework and test scripts.
  • docs/ -- user documentation in Sphinx reStructuredText format. This is used to generate the online documentation.
  • extmod/ -- additional (non-core) modules implemented in C.
  • tools/ -- various tools, including the pyboard.py module.
  • examples/ -- a few example Python scripts.

"make" is used to build the components, or "gmake" on BSD-based systems. You will also need bash, gcc, and Python 3.3+ available as the command python3 (if your system only has Python 2.7 then invoke make with the additional option PYTHON=python2). Some ports (rp2 and esp32) additionally use CMake.

Supported platforms & architectures

MicroPython runs on a wide range of microcontrollers, as well as on Unix-like (including Linux, BSD, macOS, WSL) and Windows systems.

Microcontroller targets can be as small as 256kiB flash + 16kiB RAM, although devices with at least 512kiB flash + 128kiB RAM allow a much more full-featured experience.

The Unix and Windows ports allow both development and testing of MicroPython itself, as well as providing lightweight alternative to CPython on these platforms (in particular on embedded Linux systems).

The "minimal" port provides an example of a very basic MicroPython port and can be compiled as both a standalone Linux binary as well as for ARM Cortex M4. Start with this if you want to port MicroPython to another microcontroller. Additionally the "bare-arm" port is an example of the absolute minimum configuration, and is used to keep track of the code size of the core runtime and VM.

In addition, the following ports are provided in this repository:

  • cc3200 -- Texas Instruments CC3200 (including PyCom WiPy).
  • esp32 -- Espressif ESP32 SoC (including ESP32S2, ESP32S3, ESP32C3, ESP32C6).
  • esp8266 -- Espressif ESP8266 SoC.
  • mimxrt -- NXP m.iMX RT (including Teensy 4.x).
  • nrf -- Nordic Semiconductor nRF51 and nRF52.
  • pic16bit -- Microchip PIC 16-bit.
  • powerpc -- IBM PowerPC (including Microwatt)
  • qemu -- QEMU-based emulated target (for testing)
  • renesas-ra -- Renesas RA family.
  • rp2 -- Raspberry Pi RP2040 (including Pico and Pico W).
  • samd -- Microchip (formerly Atmel) SAMD21 and SAMD51.
  • stm32 -- STMicroelectronics STM32 family (including F0, F4, F7, G0, G4, H7, L0, L4, WB)
  • webassembly -- Emscripten port targeting browsers and NodeJS.
  • zephyr -- Zephyr RTOS.

The MicroPython cross-compiler, mpy-cross

Most ports require the MicroPython cross-compiler to be built first. This program, called mpy-cross, is used to pre-compile Python scripts to .mpy files which can then be included (frozen) into the firmware/executable for a port. To build mpy-cross use:

$ cd mpy-cross
$ make

External dependencies

The core MicroPython VM and runtime has no external dependencies, but a given port might depend on third-party drivers or vendor HALs. This repository includes several submodules linking to these external dependencies. Before compiling a given port, use

$ cd ports/name
$ make submodules

to ensure that all required submodules are initialised.