Curriculum
Course: Basics of MicroPython with Raspberry Pi ...
Login
Text lesson

Overview of MicroPython

Overview

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.

1. What is MicroPython?

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.

2. History and Development

  • Origins: MicroPython was created by Damien P. George in 2013. It was first announced on Kickstarter and quickly gained popularity due to its simplicity and power.
  • Evolution: Over the years, MicroPython has evolved with contributions from a growing community, adding support for various microcontrollers and expanding its standard library.
  • Current State: Today, MicroPython supports a wide range of hardware platforms, including the Raspberry Pi Pico W, ESP8266, ESP32, and others, making it a versatile choice for embedded programming.

3. Comparison with Other Microcontroller Programming Languages

Arduino (C/C++):

  • Ease of Use: MicroPython is generally easier to learn for beginners due to its simpler syntax compared to C/C++.
  • Readability: Python code is more readable and concise, making it easier to understand and maintain.
  • Development Speed: Writing and testing code is faster in MicroPython due to its interactive REPL (Read-Eval-Print Loop) and high-level abstractions.

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)

Comparison between MicroPython and Other Microcontroller Programming Languages

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.

Summary:

  • MicroPython: Ideal for beginners due to its simple syntax and ease of use. It provides a quick development cycle with interactive testing and debugging, making it suitable for many applications despite some performance trade-offs.
  • Arduino (C/C++): Offers excellent performance and control over hardware, making it suitable for performance-critical applications. The syntax is more complex, posing a steeper learning curve for beginners.
  • Traditional C for Embedded Systems: Provides the highest level of control and performance, but requires in-depth knowledge of hardware and low-level programming. Suitable for highly specialized and performance-critical applications.
Layer 1