In this lesson, you’ll be introduced to the fundamental concepts of MicroPython. We’ll explore what MicroPython is, its history and development, and compare it with other microcontroller programming languages. This foundational knowledge will set the stage for understanding how to program microcontrollers using MicroPython, leading to hands-on applications with the Raspberry Pi Pico W.
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.
MicroPython is packed full of advanced features such as an interactive prompt, arbitrary precision integers, closures, list comprehension, generators, exception handling and more. Yet it is compact enough to fit and run within just 256k of code space and 16k of RAM.
MicroPython strives to be as compatible as possible with normal Python (known as CPython) so that if you know Python you already know MicroPython. On the other hand, the more you learn about MicroPython the better you become at Python.
Purpose of MicroPython: Designed to bring the ease and versatility of Python to microcontroller programming, allowing developers to write clean and simple code for embedded systems.
Arduino (C/C++):
Example (Blinking LED in Arduino vs. MicroPython):
Arduino (C/C++):
// Arduino code to blink an LED
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off
delay(1000); // wait for a second
}
MicroPython:
# MicroPython code to blink an LED
import machine
import time
led = machine.Pin(25, machine.Pin.OUT)
while True:
led.toggle()
time.sleep(1)
Feature | MicroPython | Arduino (C/C++) | Traditional C for Embedded Systems |
Ease of Use | Very user-friendly, especially for beginners due to its simple and readable syntax. | Moderate; more complex syntax can be challenging for beginners. | Challenging; requires deep understanding of hardware and low-level programming. |
Syntax | High-level, similar to Python; easy to read and write. | Low-level; more verbose and complex than Python. | Low-level; very detailed and hardware-specific. |
Development Speed | Fast; interactive REPL (Read-Eval-Print Loop) allows quick testing and debugging. | Slower; requires compilation before testing. | Slower; requires extensive setup and debugging. |
Libraries | Extensive; includes many built-in libraries for common tasks and hardware. | Extensive; large community support and many libraries available. | Limited; libraries are often custom and hardware-specific. |
Community Support | Growing; strong support due to the popularity of Python. | Large; established community with extensive resources. | Moderate; specialized communities based on specific hardware. |
Performance | Good for many applications; some performance trade-offs due to high-level nature. | Excellent for performance-critical applications; optimized for speed. | Excellent; highly optimized for specific hardware. |
Memory Usage | Higher than C/C++ due to high-level abstractions. | Lower; more control over memory management. | Lower; very efficient use of memory. |
Hardware Control | Good; sufficient for most applications but less control over low-level hardware specifics. | Excellent; direct access to hardware and low-level control. | Excellent; maximum control over hardware and system resources. |
Portability | High; code can be easily ported to different MicroPython-supported devices. | Moderate; requires adjustments for different hardware platforms. | Low; code is highly specific to the hardware it’s written for. |
Error Handling | High-level error handling; more user-friendly. | Low-level error handling; can be more challenging to debug. | Low-level error handling; requires extensive debugging skills. |
Learning Curve | Gentle; ideal for beginners and those familiar with Python. | Steeper; requires understanding of more complex concepts and syntax. | Steep; requires knowledge of hardware and low-level programming concepts. |