▼ Click the card below to follow me
▲ Click the card above to follow me
A USB cable, a small development board, plug it into the computer, open the IDE, and a string of “Hello, world!” lights up quietly on the screen. MicroPython, a name that sounds extraordinary, is the “mini” version of Python, designed specifically for microcontrollers. Don’t underestimate its size; its power is significant. Using Python to control microcontrollers makes coding as easy as writing poetry, whether it’s lighting up LEDs, handling sensors, or connecting to the internet. Embedded development is no longer just the domain of electronics experts; with just a few keystrokes, you can make hardware dance. Today, let’s talk about MicroPython and see just how powerful it can be.
What is MicroPython? Small but Capable of Great Things
MicroPython is essentially a Python interpreter tailored for microcontrollers (like ESP32, ESP8266, STM32, and other small devices). Compared to standard Python (which we call CPython), MicroPython is incredibly compact, yet it can accomplish a lot. It is optimized for resource-constrained hardware environments, such as chips with only a few dozen KB of memory and clock speeds as slow as a snail.
Once MicroPython is installed, the development board can run Python code directly. What does cool mean? This is what cool looks like. Previously, embedded development often required writing in C or C++, compiling, flashing, and debugging, which was a tedious process. MicroPython allows you to use Python syntax, instantly transforming you into a rapid developer.
Note: MicroPython is not a “shrunk version” of Python; it doesn’t have fewer features but has been “slimmed down” for embedded hardware. Some standard libraries may not be directly usable, similar to a mini pony; while it may not have the power of a full-sized horse, it is agile.
Lighting Up LEDs and Button Control Made Simple
What does “lighting up” mean? It’s not about your living room; it’s about LEDs. Controlling an LED to blink is almost every embedded developer’s “Hello, world!”. With MicroPython, you can achieve this in just a few lines of code:
from machine import Pin
import time
led = Pin(2, Pin.OUT) # Pin 2 is usually connected to the onboard LED
while True:
led.value(1) # Turn on
time.sleep(0.5)
led.value(0) # Turn off
time.sleep(0.5)
This code makes the LED blink, reminiscent of little stars in the night sky. Using the Pin class is as easy as playing with building blocks; you specify the pin, set the mode (output or input), and the rest is just turning the light on and off.
Don’t underestimate this example; it hides two powerful techniques: loops and delays. The loop keeps the light blinking, while time.sleep(0.5) gives the light a rhythmic on-off pattern. In C, you would have to write a long string of register configurations, but here, Python does it in one line.
Note: The pin numbers on the development board are not arbitrary; check the pinout diagram for your specific board to avoid mistakenly connecting to the wrong pin and burning out the LED; your boss won’t cover for you.
Sensor Connections and Data Collection Made Easy
In embedded development, sensors are essential. Temperature, humidity, light intensity, acceleration… whatever you want, MicroPython can handle it. For example, the most common DHT11 temperature and humidity sensor has ready-to-use modules in MicroPython, so you can just plug it in and measure.
import dht
from machine import Pin
sensor = dht.DHT11(Pin(4)) # Using pin 4
sensor.measure()
print("Temperature:", sensor.temperature(), "°C")
print("Humidity:", sensor.humidity(), "%")
Measuring temperature and humidity takes just two commands, and the output is easy to understand, even for elementary school students. For sensors like the DHT11, there are plenty of existing libraries in the MicroPython community that you can simply copy and use. Even if there isn’t a ready-made library, implementing it yourself using I2C, SPI, or UART communication protocols is not difficult.
Note: Different sensors use different interfaces; don’t mix up I2C, SPI, and UART, or you might end up unable to read data, leading to existential doubts.
Network Connectivity: Low-Cost IoT is Not a Dream
Boards like ESP32 and ESP8266 come with built-in WiFi modules, and with just a few lines of code, the development board can connect to your home WiFi, transforming into a small server for remote control and cloud data upload.
import network
sta = network.WLAN(network.STA_IF)
sta.active(True)
sta.connect('Your WiFi Name', 'Your Password')
while not sta.isconnected():
pass
print('Successfully connected to WiFi:', sta.ifconfig())
This code connects the development board to WiFi, and ifconfig() can be used to check the IP address. Later, combined with the socket module, you can create a web server that can control the LED on the development board from a mobile browser.
Note: Some boards may connect to WiFi slowly the first time, so don’t rush to restart; wait a few seconds, and it might just connect slowly.
Using Functions and Modules: Code Reuse Made Easy
When writing embedded code, it can easily become chaotic with too many lines. MicroPython supports functions and modules, allowing you to encapsulate repetitive functionalities separately, and when you need them, you can just import them, making it elegant.
For example, you can write the LED blinking code as a function:
def blink(led_pin, times=5):
led = Pin(led_pin, Pin.OUT)
for _ in range(times):
led.value(1)
time.sleep(0.3)
led.value(0)
time.sleep(0.3)
blink(2, 10) # Make the LED on pin 2 blink 10 times
If you write a lot, you can save the functions as .py files, creating your own “magic modules” that you can import next time, saving time and effort.
Note: Having too many functions and modules is not always better; too much fragmentation can make it hard to understand. Proper encapsulation is key; don’t throw a function for every single line of code, or you might get lost.
MicroPython combines the flexibility of Python with the efficiency of embedded systems, making coding enjoyable and hardware interaction exciting. Lighting up LEDs, measuring temperature, connecting to networks, and modularizing code are all within reach. Embedded development is no longer out of reach; anyone can get started. As for more advanced features like asynchronous I/O, file operations, or even custom classes and decorators, we can discuss those next time. Every time you light up an LED, it’s a step towards the future.

Like and share

Let money and love flow to you