Learn electronics, coding, and projects — step by step.

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 or 25 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.

💡 Tip: If you’re unsure which pin your onboard LED uses, check the documentation of your board model. For ESP32, it’s usually 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:

  1. Import Modules: machine lets you control the board’s hardware components, and time gives access to delays.
  2. Create a Pin Object: Pin(2, Pin.OUT) tells MicroPython that GPIO2 will be used as an output pin for the LED.
  3. Control the LED: led.value(1) turns it ON (logic HIGH), and led.value(0) turns it OFF (logic LOW).
  4. Loop Forever: The while True: loop repeats indefinitely, making the LED blink continuously.
  5. 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.

⚙️ Tip: Try experimenting by changing the pin number to control different GPIOs or the delay time to speed up or slow down the blinking.

Running the Script in Thonny

Running your code in Thonny is straightforward:

  1. Connect your board to your computer using a USB cable.
  2. Open Thonny and make sure the interpreter is set to your MicroPython device.
  3. Copy and paste the blinking LED code into a new script.
  4. 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:

  1. Go to File → Save As…
  2. Select MicroPython Device
  3. 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.

✅ Congratulations! You’ve just written, uploaded, and run your first MicroPython program. This simple project is the foundation for many future experiments — controlling sensors, motors, or even Wi-Fi connections.
×



Related Articles: (by Series)