micropython-hola-mundo

Our first program in MicroPython

  • 3 min

Now that we have everything set up, and the MicroPython firmware installed, it’s time to make our first MicroPython program. A simple “Blink” that will make an LED flash.

In programming, it’s a tradition that the first program we run in a language is a “Hello, World”. It allows us to become familiar with the development environment and the language.

The equivalent in embedded devices is a “Blink”. That is, we will simply turn an LED on and off every second.

It may not be the best program in the world (no, it isn’t). But it’s enough to verify that

  1. The device is compatible and properly connected to the computer
  2. The MicroPython firmware is installed and working
  3. The development environment is correctly configured (like Arduino Lab for MicroPython or Thonny)

In short, we can send and execute code on the board, and everything will go smoothly.

If you’re missing any of these things, check the previous tutorials.

Connect the board to the computer

Let’s get to it. The first thing is to connect the board to the PC via a USB cable (that’s easy).

Now we open the development environment you chose, and connect it to our device. To do this,

Click on the “connect” icon

micropython-connect-arduino-lab

In the bottom right, there is a dropdown to select the board

micropython-connect-thonny

In either case, you should see that it connects correctly.

Write the “Hello, World” program

Now let’s create our script. In MicroPython, the “Hello, World” program is extremely simple.

We just need to create a new file in the IDE. Then copy and paste this snippet of code.

from machine import Pin
import time

led = Pin("LED", Pin.OUT)  # Configure the LED as output

while True:
    led.on()  # Turn on the LED
    time.sleep(1)  # Wait for 1 second
    led.off()  # Turn off the LED
    time.sleep(1)  # Wait for 1 second
  • The Pin class is essential for interacting with the GPIO pins in MicroPython. It allows us to configure the pins as inputs or outputs, and to read or write values to them. In our case, we configure the pin as an output to control the LED.
  • The time.sleep function introduces a delay in the execution of the program. In our example, we use a delay of 1 second so that the LED remains on or off for that time before changing state.

We will explore each part in depth in the upcoming tutorials. Don’t worry, for now this is just a “hello world”.

Run the code

To run the code, just hit the Play button in the IDE. You should see the LED turning on and off every second.

What’s happening under the hood? When we run the program,

  • The IDE sends the code to the board via USB
  • The MicroPython interpreter receives the program and executes it.

What you’re seeing is the REPL (Read-Eval-Print Loop) in action. An interactive tool that allows us to execute code in real-time on the board (which we will see in the next entry).

But if you unplug the device and plug it back in, you’ll see that the LED doesn’t blink. That is, we haven’t saved the program to the device. It’s the IDE that sends it.

So when you disconnect the device, it “loses” the program. We will see how to upload the script to the device so that it starts autonomously.

We will cover both things (REPL and how to transfer the file to the device) in the next two tutorials