Skip to content

uPesy only delivers currently in France.

Contents Menu Expand Light mode Dark mode Auto light/dark mode

Discover MicroPython on the Raspberry Pi Pico with your first script

One of the easiest things to do on a new board is to flash the built-in LED. This is like the famous Hello World text prompt in the terminal when discovering a new programming language. Here is a script that blinks the built-in LED each second. The purpose here is to show you an overview of programming in MicroPython.

from machine import Pin
 import time

 pin_led = Pin (25, mode = Pin.OUT)

 while True:

     pin_led.on ()
     time.sleep (1)
     pin_led.off ()
     time.sleep (1)

Note

It is recommended that you know the basics of Python syntax before getting started in MicroPython.

Run the MicroPython script on your Pi Pico board

First, ensure that you correctly configured your Pico board’s IDE software. Here is, if necessary, a guide to configure the Raspberry Pi Pico in Thonny IDE .

On Thonny IDE, saving the code on the computer is better than directly on the board. Thonny IDE will send the code directly to the MicroPython interpreter (via the REPL) when running the script. To save the script to the board afterward, just go to File> Save as …

You will notice that the Python script does not run automatically when powering up the Pico board . To fix this, you must save the script on the board with a given name main.py . This is not usually an issue during the prototyping phases, but definitely useful when using a MicroPython script permanently on your Pico.

A detailed description of a MicroPython script

The structure of a MicroPython script is as follows:

MicroPython layout

The layout of a MicroPython script

Each MicroPython script begins with importing the modules required to function properly. It’s like #include in the Arduino IDE code for external libraries: #include <SPI.h> , #include <SD.h>

The difference with MicroPython is that even basic objects must be imported . As a reminder, Python is an object-oriented language: all Python elements are objects. For example, the input/output pins of the Pico will be seen and managed like objects. MicroPython objects can be used easily without being an expert in object-oriented programming.

In the example script, we import the Pin submodule from the machine module. The machine module includes most of the MicroPython specific objects . The Pin module is actually a class that describes the function of an input/output pin (GPIO). We create a pin_led object corresponding to the pin that controls the built-in LED. This pin is defined as a digital output. The Pin object that we created has functions to modify its state: .on() we turn on the LED and .off() we turn off the LED.

Note

The pin number connected to the built-in LED is 2 for the Raspberry Pi Pico and the uPesy RP2040 DevKit.

To check the blinking, add a delay of one second with the sleep() function of the time module.

The code that causes the LED to blink is contained in an infinite loop to blink … endlessly.

If you used to use Arduino code, you would notice that there are no more the setup() and loop() functions: the script is run only once. Therefore, we must not forget to add an infinite loop to have the same behavior as Arduino sketches. A code is available to have the same design as in Arduino code with a setup() function and a loop() function.

We use cookies to make your visit to the site as pleasant as possible. Privacy Policy