Definition: MicroPython is a lightweight implementation of Python 3 designed to run on microcontrollers and resource-constrained devices, enabling easy programming of hardware for tasks like controlling sensors or LEDs.
Key Use Cases: Prototyping IoT devices, controlling hardware in embedded systems, and educational projects with microcontrollers.
Prerequisites: Basic understanding of Python programming and familiarity with microcontrollers.
What: MicroPython is a version of Python optimized for microcontrollers, allowing users to write Python scripts to control hardware like LEDs, sensors, or motors.
Why: It simplifies embedded programming with Python’s easy syntax, enabling rapid prototyping and reducing the complexity of traditional C-based development.
Where: Used in IoT devices, robotics, educational platforms, and hobbyist projects for tasks like home automation or environmental monitoring.
MicroPython runs a Python interpreter on microcontrollers, executing scripts to interact with hardware components like GPIO pins, sensors, or displays.
It provides a subset of Python 3 features, optimized for limited memory and processing power (e.g., 16-512 KB RAM, 8-32 MHz CPUs).
Scripts control hardware via libraries that interface with pins, timers, or communication protocols (e.g., I2C, SPI).
Key Components:
Microcontroller: A small computer (e.g., ESP32, Raspberry Pi Pico) with CPU, memory, and GPIO pins for hardware control.
MicroPython Interpreter: A compact runtime that executes Python code on the microcontroller, including a REPL (Read-Eval-Print Loop) for interactive coding.
GPIO (General Purpose Input/Output): Pins on the microcontroller used to send or receive signals (e.g., turn an LED on/off).
Libraries: Modules like machine and utime for hardware control and timing.
Common Misconceptions:
Misconception: MicroPython is the same as desktop Python.
Reality: It’s a lightweight version with fewer features, tailored for microcontrollers.
Misconception: MicroPython requires internet connectivity.
Reality: It runs offline, though some boards support Wi-Fi for IoT tasks.
graph TD
A[Python Script <br> (e.g., Blink LED)] --> B[MicroPython Interpreter <br> (Running on Microcontroller)]
B --> C[Hardware Interaction <br> (e.g., GPIO Pins)]
C --> D[Output <br> (e.g., LED On/Off)]
- System Overview: The diagram shows a Python script executed by the MicroPython interpreter on a microcontroller, interacting with hardware to produce an output.
- Component Relationships: The script drives the interpreter, which controls hardware via GPIO, resulting in observable actions.
# MicroPython script to blink an LED on an ESP32 or Raspberry Pi PicofrommachineimportPinimportutime# Configure GPIO pin for LEDled=Pin(2,Pin.OUT)# Pin 2 (adjust for your board, e.g., GP25 for Pico)# Blink LED in a loopwhileTrue:led.value(1)# Turn LED onutime.sleep(0.5)# Wait 0.5 secondsled.value(0)# Turn LED offutime.sleep(0.5)# Wait 0.5 seconds
- Step-by-Step Setup:
1. Hardware: Get a MicroPython-compatible microcontroller (e.g., ESP32, Raspberry Pi Pico) and connect an LED (with a 220Ω resistor) to GPIO pin 2 (or GP25 for Pico) and GND.
2. Install MicroPython:
- Download the MicroPython firmware (.uf2 or .bin) for your board from micropython.org.
- Flash the firmware (e.g., for Pico, hold BOOTSEL, drag .uf2 to the drive; for ESP32, use esptool.py).
3. Install Tools: Install a serial terminal (e.g., PuTTY, minicom) or IDE like Thonny (thonny.org).
4. Save Code: Save the code as main.py on your computer.
5. Upload and Run:
- Connect the board via USB.
- Use Thonny to upload main.py to the board’s filesystem.
- Reset the board to run the script (LED should blink).
- Code Walkthrough:
- Imports machine.Pin for GPIO control and utime for delays.
- Configures pin 2 as an output for the LED.
- Loops to toggle the LED on/off every 0.5 seconds using led.value and utime.sleep.
- Common Pitfalls:
- Incorrect pin number (check board pinout diagram).
- Missing resistor causing LED damage or unreliable behavior.
- Firmware not flashed correctly, preventing MicroPython from running.