0% found this document useful (0 votes)
3 views8 pages

Micropython Notes

MicroPython is a high-level programming language designed for microcontrollers, offering a Pythonic syntax that is beginner-friendly and ideal for rapid prototyping and IoT projects. In contrast, traditional microcontroller languages like C/C++ and Assembly provide better performance, memory efficiency, and real-time control, making them suitable for industrial applications. The choice between MicroPython and other languages depends on whether the priority is on development speed and simplicity or on optimization and execution speed.

Uploaded by

yadavarti9412
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views8 pages

Micropython Notes

MicroPython is a high-level programming language designed for microcontrollers, offering a Pythonic syntax that is beginner-friendly and ideal for rapid prototyping and IoT projects. In contrast, traditional microcontroller languages like C/C++ and Assembly provide better performance, memory efficiency, and real-time control, making them suitable for industrial applications. The choice between MicroPython and other languages depends on whether the priority is on development speed and simplicity or on optimization and execution speed.

Uploaded by

yadavarti9412
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Differences between MicroPython programming and other common

microcontroller programming languages (like C/C++, Arduino IDE, or


bare-metal assembly):
Aspect MicroPython Other Microcontroller Languages

High-level Python
1. Language Base Typically C, C++, or Assembly
implementation

Pythonic, readable, beginner-


2. Syntax More verbose, lower-level syntax
friendly

C/Assembly are more memory-


3. Memory Footprint Lightweight but larger than C
efficient

Slower execution due to


4. Speed Faster execution (compiled)
interpretation

Interactive REPL (Read-Eval-


5. Development Style Compile-upload-run cycle
Print Loop)

Easier for beginners familiar


6. Ease of Learning Steeper learning curve
with Python

Built-in Python libraries (math, Hardware-specific libraries, less


7. Libraries
time, etc.) general-purpose

Portable across supported


8. Portability Often hardware-specific
boards

9. Debugging Interactive debugging via REPL Requires external debuggers/tools

10. Code Size Larger due to Python overhead Smaller, optimized binaries

11. Real-Time
Limited for strict real-time tasks Better suited for real-time control
Performance

Larger, established embedded


12. Community Growing maker/IoT community
systems community

Integrates with Python


13. Ecosystem Integrates with embedded toolchains
ecosystem

14. Error Handling Python exceptions Manual error handling in C/Assembly

Abstracted APIs (e.g.,


15. Hardware Access Direct register manipulation
[Link])

16. Productivity Faster prototyping Slower but more optimized

17. File System Supports on-device file system Typically no file system
Aspect MicroPython Other Microcontroller Languages

18. Firmware Updates Easy via scripts Requires recompilation and flashing

Python modules reusable


19. Code Reuse Limited portability
across platforms

Makers, hobbyists, IoT


20. Target Audience Professional embedded engineers
developers

Key Takeaways

• MicroPython shines in ease of use, rapid prototyping, and accessibility for beginners
and IoT projects.

• C/C++/Assembly dominate in performance, efficiency, and real-time control, making


them better for industrial-grade applications.

• Choosing between them depends on whether you prioritize speed and optimization or
development speed and simplicity.

Features of MicroPython:
Feature Expanded Description
Implements a lean subset of Python 3, making it familiar yet
1. Python 3 Subset
optimized for microcontrollers.
Provides a live prompt for testing code snippets, debugging, and
2. Interactive REPL
controlling hardware instantly.
3. Lightweight Designed to run on devices with very limited RAM and flash (tens
Runtime of KBs).
4. Hardware Modules like machine simplify access to GPIO, timers, PWM,
Abstraction Layer ADC/DAC, and communication buses.
Includes a simple on-device file system for storing scripts, logs,
5. File System Support
and configuration files.
Python-style try/except makes error management easier
6. Exception Handling
compared to C’s manual checks.
7. Cross-Platform Runs on ESP32, STM32, PyBoard, Raspberry Pi Pico, and more
Portability with minimal code changes.
Ships with math, time, os, and other standard modules, plus
8. Built-in Libraries
hardware-specific ones.
No need for compilation—scripts can be uploaded or edited
9. Rapid Prototyping
directly for fast iteration.
10. Memory Includes garbage collection to manage memory automatically,
Management unlike manual allocation in C.
Feature Expanded Description
11. Networking Provides modules for Wi-Fi, Bluetooth, and sockets, enabling IoT
Support applications.
Supports cooperative multitasking via uasyncio, allowing
12. Concurrency
multiple tasks to run concurrently.
Designed to be approachable for students and hobbyists, lowering
13. Educational Focus
barriers to embedded programming.
Can integrate with C modules for performance-critical tasks while
14. Extensibility
keeping Python ease-of-use.
15. Community Backed by an active open-source community, with tutorials,
Ecosystem libraries, and board support expanding rapidly.

1. Introduction to MicroPython
MicroPython is a lean implementation of Python 3 designed to run on
microcontrollers. Unlike traditional embedded programming in C or Assembly,
MicroPython allows developers to use familiar Python syntax while still interacting
directly with hardware. This makes it ideal for education, IoT prototyping, and
hobbyist projects.
Example: Hello World on MicroPython
print("Hello, MicroPython!")
This simple script runs directly on the board without compilation, demonstrating the
ease of use compared to C-based workflows.

2. Microcontrollers Overview
Microcontrollers are small computers on a chip, typically with limited RAM and flash
memory. MicroPython is optimized to run on these constrained devices, bridging the
gap between high-level programming and low-level hardware control.
Example: Checking system info
import os
print([Link]())
This prints details about the microcontroller, such as firmware version and board type.

3. Getting Started
To begin, you flash MicroPython firmware onto a supported board (e.g., ESP32,
PyBoard, Raspberry Pi Pico). Once installed, you connect via USB or serial and
access the REPL (interactive prompt).
Example: REPL interaction
>>> 2 + 3
5
>>> "MicroPython".upper()
'MICROPYTHON'
This immediate feedback loop makes experimentation fast and intuitive.

4. REPL & Interactive Programming


The REPL (Read-Eval-Print Loop) is one of MicroPython’s most powerful features. It
allows developers to test code snippets live, debug hardware interactions, and iterate
quickly.
Example: Blinking an LED interactively
from machine import Pin
import time

led = Pin(2, [Link]) # onboard LED


while True:
[Link](1)
[Link](0.5)
[Link](0)
[Link](0.5)
You can stop and modify this loop directly from the REPL without recompiling.

5. Python Subset in MicroPython


MicroPython supports most Python 3 features: data types, loops, functions, and
exceptions. However, heavy modules like multiprocessing or numpy are excluded due
to memory constraints.
Example: Using lists and loops
numbers = [1, 2, 3, 4, 5]
for n in numbers:
print(n * n)
This demonstrates Python’s expressive syntax, which is preserved in MicroPython.

6. Hardware Control
MicroPython provides the machine module for hardware access. You can control
GPIO pins, timers, PWM signals, and analog inputs/outputs.
Example: Reading a button press
from machine import Pin

button = Pin(0, [Link])


if [Link]() == 1:
print("Button pressed!")
This abstracts away register-level programming, making hardware control beginner-
friendly.

7. Sensors & Actuators


MicroPython makes it easy to interface with sensors and actuators. For example, you
can read temperature sensors or control motors with minimal code.
Example: Reading a temperature sensor (DS18B20)
import onewire, ds18x20, machine, time

dat = [Link](12)
ds = ds18x20.DS18X20([Link](dat))
roms = [Link]()
while True:
ds.convert_temp()
[Link](1)
for rom in roms:
print(ds.read_temp(rom))

8. File System & Storage


MicroPython supports a simple file system on flash memory. You can store scripts,
logs, and configuration files directly on the device.
Example: Writing to a file
with open("[Link]", "w") as f:
[Link]("Temperature log started\n")

9. Networking & IoT


On boards like ESP8266 or ESP32, MicroPython provides Wi-Fi and socket support,
enabling IoT applications.
Example: Connecting to Wi-Fi
import network

wifi = [Link](network.STA_IF)
[Link](True)
[Link]("MySSID", "MyPassword")

while not [Link]():


pass

print("Connected:", [Link]())

10. Concurrency
MicroPython supports cooperative multitasking via uasyncio. This allows multiple
tasks to run concurrently without blocking.
Example: Running two tasks
import uasyncio as asyncio

async def blink():


while True:
print("Blink")
await [Link](1)

async def read_sensor():


while True:
print("Sensor reading")
await [Link](2)

[Link]([Link](blink(), read_sensor()))
11. Error Handling
MicroPython uses Python’s exception system, making debugging easier compared to
C.
Example: Handling errors
try:
x=1/0
except ZeroDivisionError:
print("Cannot divide by zero!")

12. Extending MicroPython


Performance-critical tasks can be written in C and integrated with MicroPython,
combining efficiency with ease of use.

13. Educational Applications


MicroPython is widely used in classrooms and workshops because of its simplicity.
Students can learn programming concepts while directly interacting with hardware.

14. Rapid Prototyping


MicroPython eliminates the compile-flash-run cycle. You can upload or edit scripts
directly, making it ideal for fast prototyping.

15. Community & Ecosystem


MicroPython has a vibrant open-source community. Tutorials, libraries, and board
support are constantly expanding, making it easier to find help and resources.
Example-1: The classic “Hello World” of embedded programming.
Teaches: GPIO control, timing, infinite loops

Example 2: Read a Button


Detecting input from a push button.

You might also like