Micropython Basics: Writing Your First Micropython Program
Table of Contents
Now that you understand the basics of MicroPython’s syntax, it’s time to create your very first real-world project: blinking an LED. This is the "Hello, World!" of hardware programming — it verifies that your board, firmware, and IDE setup are all working correctly.
You’ll learn how to interact with the board’s GPIO (General Purpose Input/Output) pins, configure them as outputs, and control an LED by turning it on and off in a timed loop.
Understanding the Hardware Setup
Most development boards such as the ESP32, ESP8266, or Raspberry Pi Pico have one or more built-in LEDs that can be controlled through a GPIO pin. These LEDs are perfect for quick testing. However, you can also connect an external LED for better visibility.
- Built-in LED: Typically connected to pin
2
on the ESP32 or25
on the Pico. - External LED: Connect the LED’s anode (longer leg) to a GPIO pin (e.g., GPIO15) through a 220Ω resistor,
and the cathode (shorter leg) to
GND
.
Using a resistor prevents excessive current from damaging the LED or your microcontroller’s pin.
GPIO2
; for Pico, LED
is already predefined.
Setting Up GPIO Control
In MicroPython, controlling hardware pins is simple thanks to the machine
module.
This module provides classes and functions to manage GPIOs, timers, PWM signals, and more.
Here’s a quick breakdown of what we’ll use:
Pin
— used to configure a GPIO pin as either input or output.time.sleep()
— pauses execution for a specific number of seconds.
from machine import Pin
import time
led = Pin(2, Pin.OUT) # Create LED object on GPIO2
while True:
led.value(1) # Turn LED ON
time.sleep(1)
led.value(0) # Turn LED OFF
time.sleep(1)
When this script runs, your LED will blink on and off every second — congratulations, you’ve just built your first hardware project with MicroPython!
Step-by-Step Explanation
Let’s go through the code line by line to understand what’s happening:
- Import Modules:
machine
lets you control the board’s hardware components, andtime
gives access to delays. - Create a Pin Object:
Pin(2, Pin.OUT)
tells MicroPython that GPIO2 will be used as an output pin for the LED. - Control the LED:
led.value(1)
turns it ON (logic HIGH), andled.value(0)
turns it OFF (logic LOW). - Loop Forever:
The
while True:
loop repeats indefinitely, making the LED blink continuously. - Delay Between States:
time.sleep(1)
pauses execution for one second between each toggle.
This loop will continue until you manually stop the program in Thonny. You can adjust the delay value (e.g., 0.5 or 2) to change the blink rate.
Running the Script in Thonny
Running your code in Thonny is straightforward:
- Connect your board to your computer using a USB cable.
- Open Thonny and make sure the interpreter is set to your MicroPython device.
- Copy and paste the blinking LED code into a new script.
- Click the green Run button or press F5.
The LED should start blinking immediately. If not, check that you’re using the correct GPIO pin number and that your board is properly connected.
Saving and Auto-Running on Boot
To make your code run automatically each time the board powers on, save the file as main.py
directly to the device’s storage.
Thonny makes this simple:
- Go to File → Save As…
- Select MicroPython Device
- Name the file
main.py
and click OK
Now, whenever your microcontroller resets or is powered on, it will automatically execute this script — blinking the LED right away.