▼ Click the card below to follow me
▲ Click the card above to follow me
MicroPython: The Mini Power of Embedded Systems
Is Python getting slim? That’s right, MicroPython is here to do just that. The originally large Python has transformed, putting on a slim suit and fitting into various compact hardware. What is MicroPython? In a nutshell, it is a Python interpreter designed for microcontrollers . Yes, the Python scripts we usually write that run on computers can now also be installed on microcontrollers and development boards (like ESP32, Raspberry Pi Pico) – these “little guys”. This means that even table lamps, fans, and small robots can now run Python.
What can MicroPython do? – Small Size, Big Capability
Although MicroPython is “mini”, it has all the necessary features. Like standard Python, it can define variables , use conditional statements , loops, write functions , create classes , and even perform some advanced operations like list comprehensions and exception handling . Its greatest ability is to directly control hardware. Light bulbs, motors, sensors, buzzers… all can be manipulated.
For example, to light up an LED. After connecting the development board and the LED, the code is written like this:
from machine import Pin
led = Pin(2, Pin.OUT) # Create a Pin object, pin 2, output mode
led.value(1) # Light up the LED
Executing these few lines, the LED lights up with a click. Isn’t it simpler than assembling a circuit board? The magic of Python is that accessible.
Tip : Different development boards may have different pin numbers for the LED. For example, ESP8266 typically uses pin 2, but Raspberry Pi Pico does not use pin 2, so remember to check the manual.
“Smart Little Brain” – Variables and Data Types in MicroPython
MicroPython’s usage is almost identical to standard Python. How to write variables? Just assign them directly:
x = 123
y = "hello"
z = [1, 2, 3]
int , str , list – these common data types are all present. You can also directly use dictionaries , tuples , and booleans . Interestingly, while MicroPython supports many built-in types, some advanced standard libraries (like regular expressions, complex networking libraries) may not be available. After all, the chip’s memory is limited, so we can’t be too greedy.
Let’s do something fun, creating a running light with small LEDs:
import time
from machine import Pin
leds = [Pin(i, Pin.OUT) for i in (2, 4, 5)] # 3 LEDs connected to pins 2, 4, 5
while True:
for led in leds:
led.value(1) # Light up
time.sleep(0.2)
led.value(0) # Turn off
One LED lights up after another, just like a carousel at an amusement park. Three lines of code, endless fun.
Tip : Don’t forget the while True loop; the code will keep running, and you need to stop it manually, so it doesn’t keep flashing in your eyes.
Conditional Statements and Loops – “Either On or Off”
Controlling hardware requires judgment and repetition. MicroPython’s conditional statements and loops follow the same patterns as standard Python. For example, pressing a button turns on the light, and releasing it turns it off:
from machine import Pin
led = Pin(2, Pin.OUT)
btn = Pin(0, Pin.IN)
while True:
if btn.value() == 1:
led.value(1)
else:
led.value(0)
Pressing the button makes the LED respond. Doesn’t it remind you of the electronic building blocks you played with as a child? But remember, the button connections may differ on different development boards, so check the circuit to see if pressing is 1 or 0.
And loops. MicroPython’s for and while loops are just like Python’s. For example, to make a buzzer beep three times:
from machine import Pin
import time
buzzer = Pin(15, Pin.OUT)
for _ in range(3):
buzzer.value(1)
time.sleep(0.2)
buzzer.value(0)
time.sleep(0.2)
A series of short “beeps” can be easily produced.
Functions and Modules – “Small Parts, Big Projects”
You’ll often find that many operations in hardware code are repeated, like turning on lights or reading sensors. Tired of typing them out each time? Just use functions . For example, encapsulating a function to turn on the light:
def blink(led, times):
for _ in range(times):
led.value(1)
time.sleep(0.1)
led.value(0)
time.sleep(0.1)
blink(Pin(2, Pin.OUT), 5) # Make the LED on pin 2 blink 5 times
Once the function is written, it can be reused for any light. If you want to be more advanced, you can also create modules to save commonly used code separately and import it for convenience.
Tip : MicroPython’s import works like Python’s, but some standard libraries may not be available, such as the random module, which has some functions “streamlined”. If you encounter ImportError, check the official documentation first; if that doesn’t work, implement it yourself.
File Operations and Interaction – “Can the Board Keep a Diary?”
Don’t think that development boards can’t store files just because they don’t have hard drives; MicroPython supports file operations as well. As long as the board has storage space (like the Flash memory on ESP32), you can read and write text files just like operating a USB drive.
with open("log.txt", "a") as f:
f.write("Hello, MicroPython!\n")
Each time it runs, a new line is added to the file. You can also use read and readlines to read content, making it possible to record data or analyze logs; the board can even act as a “mini server”.
Storage space is limited, so don’t expect to store large files of hundreds of megabytes; keep your logging reasonable, as the chip can’t handle too much.
Tip : File operations can easily go wrong, so remember to use the with statement to ensure files are closed properly. Otherwise, data may be lost, and you won’t have time to cry.
Working with Sensors – “The Tentacles that Perceive the World”
The most fascinating aspect of MicroPython is that it allows a bunch of sensors to communicate. Temperature, light, acceleration, humidity… as long as the sensors can connect to the board, they can all be “greeted” with Python.
For example, to read an analog temperature sensor, the code looks like this:
from machine import ADC
adc = ADC(0) # ADC0 channel
value = adc.read() # Read the analog value
print("Temperature sensor output:", value)
As the temperature rises, the value changes accordingly. Watching the numbers jump on the screen is quite satisfying. Different development boards may have different ADC interfaces, so don’t just copy the code; remember to check the manual.
Common Pitfalls and Tips – Avoid Mistakes, Save Worry
Learning MicroPython inevitably involves stumbling. Sometimes the code is correct, but it just won’t run. It could be a wrong pin number, a missing import, or even incorrect indentation. When encountering inexplicable errors, don’t panic; try a few more times, check the board’s pinout, and see if the code is too “Pythonic” for the board’s limited memory.
Learning Tips : Write more code, experiment with hardware, and don’t be afraid to burn out the board (after all, it’s cheap). If you encounter strange bugs, try restarting the development board or swapping out the data cable; amazing things can happen.
Tip : MicroPython’s interactive REPL (command line) is super useful; just plug in the board and you can directly input code, making debugging very convenient. When facing small issues, testing directly in REPL is much faster than writing scripts.
MicroPython makes the “magic” of Python easily accessible in the hardware world. Even with just a few tens of K of memory and a small chip, it can run smoothly. Variables, data types, conditional statements, loops, functions, modules, file operations, and various hardware interactions offer plenty of possibilities. As long as you’re willing to get hands-on, even if your hands shake a bit, MicroPython can take you on an exciting journey through the embedded world.

Like and Share

Let money and love flow to you