01 - Introduction to MicroPython

# Introduction to MicroPython: Python for Microcontrollers ## What is MicroPython? ![](https://cdn.shopify.com/s/files/1/0331/9994/7908/files/Pasted_image_20250801120923.png?v=1754016863) 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 was created by Damien George and was first released in 2014. MicroPython brings the power and simplicity of Python to microcontrollers, making embedded programming more accessible to developers who are already familiar with Python. It allows you to write Python code that runs directly on microcontrollers like the ESP32, ESP8266, STM32, and many others. ## History and Development ### The Beginning MicroPython was created by Damien George, an Australian physicist and software engineer, who started the project in 2013. The initial goal was to create a Python implementation that could run on microcontrollers with limited resources. ![](https://cdn.shopify.com/s/files/1/0331/9994/7908/files/Pasted_image_20250801121007.png?v=1754016867) ### Key Milestones - **2013**: Project started by Damien George - **2014**: First public release of MicroPython - **2015**: MicroPython becomes an open-source project - **2016**: Kickstarter campaign successfully funded, raising over £97,000 - **2017**: MicroPython Foundation established - **2018-Present**: Continued development with community contributions ### The Kickstarter Success The MicroPython Kickstarter campaign was a turning point for the project. It raised over £97,000 from more than 1,900 backers, demonstrating strong community interest in bringing Python to microcontrollers. This funding helped accelerate development and expand the project's scope. ## Core Features of MicroPython ### 1. Python 3 Compatibility MicroPython implements Python 3 syntax and features, making it familiar to Python developers. It supports: - Basic Python syntax and data types - Functions and classes - List comprehensions - Exception handling - Context managers (with statements) ### 2. Optimized for Microcontrollers - **Small Memory Footprint**: Typically requires 256KB to 1MB of RAM - **Fast Execution**: Optimized bytecode interpreter - **Real-time Capabilities**: Suitable for real-time applications - **Low Power Consumption**: Efficient execution for battery-powered devices ### 3. Hardware Abstraction MicroPython provides built-in modules for hardware interaction: - `machine` module for GPIO, I2C, SPI, UART, PWM, ADC - `network` module for WiFi and network connectivity - `time` module for timing and delays - `os` module for file system operations ### 4. Interactive Development - REPL (Read-Eval-Print Loop) for interactive programming - File system support for storing and loading programs - Over-the-air updates capability ## Differences Between MicroPython and Standard Python ### 1. Memory and Resource Constraints **Standard Python:** - Runs on full operating systems (Windows, macOS, Linux) - Typically requires several MB to GB of RAM - Has access to the full Python standard library - Can use extensive third-party packages **MicroPython:** - Runs on bare metal or minimal RTOS - Requires only 256KB to 1MB of RAM - Implements only a subset of the standard library - Limited third-party package support ### 2. Standard Library Differences **Standard Python Includes:** - Complete standard library (os, sys, datetime, json, etc.) - Extensive third-party ecosystem (pip, virtual environments) - Full file system access - Network libraries (requests, urllib, etc.) **MicroPython Includes:** - Core language features - Essential modules: `machine`, `network`, `time`, `os` - Limited standard library modules - Hardware-specific modules ### 3. Performance Characteristics **Standard Python:** - Interpreted language with some optimizations - Garbage collection with more memory overhead - Slower startup time due to larger runtime **MicroPython:** - Highly optimized bytecode interpreter - Efficient garbage collection for constrained environments - Fast startup time - Real-time capable ### 4. Development Workflow **Standard Python:** - IDE integration (PyCharm, VS Code, etc.) - Package management with pip - Virtual environments - Extensive debugging tools **MicroPython:** - REPL-based development - File-based program storage - Limited debugging capabilities - Hardware-specific development tools ## Popular MicroPython Boards ### 1. ESP32 Series - **ESP32**: Dual-core, WiFi + Bluetooth, 520KB RAM - **ESP32-S3**: Enhanced version with more RAM and features - **ESP32-C3**: Cost-effective single-core option ### 2. ESP8266 - Single-core, WiFi, 80KB RAM - Popular for IoT projects - Cost-effective option ### 3. STM32 Series - Various ARM Cortex-M microcontrollers - Good performance and feature set - Professional-grade options ### 4. Raspberry Pi Pico - RP2040 dual-core ARM Cortex-M0+ - 264KB RAM, 2MB flash - Excellent documentation and community support ## Conclusion MicroPython represents a significant advancement in making embedded programming more accessible. By bringing Python to microcontrollers, it has opened up the world of IoT and embedded systems to a much broader audience of developers. The key differences between MicroPython and standard Python stem from the fundamental constraints of microcontroller environments. While MicroPython sacrifices some features and performance for compatibility and ease of use, it provides an excellent platform for learning, prototyping, and developing IoT applications. For developers new to embedded programming, MicroPython offers a gentle introduction to the world of microcontrollers while leveraging existing Python knowledge. For experienced embedded developers, it provides a rapid prototyping tool that can significantly speed up development cycles. As the IoT ecosystem continues to grow, MicroPython is likely to play an increasingly important role in making embedded programming more accessible and enabling more developers to create innovative IoT solutions.