Blink the Onboard LED on Raspberry Pi Pico WH With MicroPython
by sss2022 in Circuits > Raspberry Pi
422 Views, 1 Favorites, 0 Comments
Blink the Onboard LED on Raspberry Pi Pico WH With MicroPython
When starting with a new microcontroller board, one of the first and most common tests is blinking an LED. This simple project allows you to verify that the board is working properly and that your programming environment is correctly configured. In this tutorial, we will learn step by step how to blink the onboard LED of the Raspberry Pi Pico WH using MicroPython. The Raspberry Pi Pico WH is a powerful and affordable microcontroller board based on the RP2040 microcontroller and it also includes built-in Wi-Fi capabilities, which makes it suitable for many embedded systems and IoT projects. This guide is designed for beginners and will walk you through the basic process of writing a simple MicroPython program and running it on the Pico WH. By the end of this tutorial, you will be able to control the onboard LED and you will have a solid starting point for developing more advanced projects with the Raspberry Pi Pico WH.
Supplies
- Raspberry Pi Pico WH development board
- USB-A to Micro-USB cable
Install MicroPython on the Raspberry Pi Pico WH
Before we can program the Raspberry Pi Pico WH using Python, we first need to install the MicroPython firmware on the board. MicroPython is a lightweight implementation of Python designed specifically for microcontrollers, allowing us to write simple and powerful programs to control hardware.
To install MicroPython, start by downloading the latest MicroPython firmware file for the Raspberry Pi Pico WH from the official MicroPython website (https://micropython.org/download/RPI_PICO_W/). Once the file is downloaded, press and hold the BOOTSEL button on the Pico WH while connecting the board to your computer using a USB cable. The board will appear as a removable storage device named RPI-RP2. Simply drag and drop the downloaded .uf2 firmware file into this drive. After the file is copied, the board will automatically reboot and MicroPython will be installed on the Raspberry Pi Pico WH, making it ready to be programmed using a Python editor such as Thonny.
Install Thonny IDE
To write and upload MicroPython programs to the Raspberry Pi Pico WH, we need a programming environment. One of the easiest and most recommended tools for beginners is Thonny IDE. Thonny is a lightweight and user-friendly Python editor that comes with built-in support for MicroPython, making it very convenient for programming microcontroller boards like the Raspberry Pi Pico.
Start by downloading Thonny from the official Thonny website (https://thonny.org/) and install it on your computer by following the installation instructions for your operating system.
After installing Thonny, the next step is to configure it so that it can communicate with the Raspberry Pi Pico WH. To do this, we need to select the correct interpreter. Open Thonny and connect your Pico WH to your computer using a USB cable. Then click on Tools in the top menu and select Options.
In the window that appears, go to the Interpreter tab. From the interpreter list, choose MicroPython (Raspberry Pi Pico). Thonny will automatically detect the connected board and select the appropriate port. Once this setting is applied, Thonny will be able to send MicroPython programs directly to the Raspberry Pi Pico WH, allowing you to run and test your code easily.
Write the First MicroPython Program
Now that the development environment is ready, we can write our first MicroPython program to control the onboard LED of the Raspberry Pi Pico WH.
To start writing the code in Thonny, click on File and then select New File to open a new script window.
MicroPython is a lightweight version of the Python programming language designed to run on microcontrollers, making it possible to control hardware with simple and readable code. In this project, we will use a short MicroPython script to turn the onboard LED on and off repeatedly, creating a blinking effect. This simple example is commonly used as a first test when working with a new microcontroller because it confirms that the board, the firmware, and the programming environment are all working correctly.
Below is the code used in this tutorial, and in the next step we will explain it line by line to better understand how it works.
1️⃣
👉 We import the Pin class from the machine module.
- machine = MicroPython library used to control the hardware
- Pin = allows us to control the pins (GPIO)
2️⃣
👉 We import the time module in order to use delays (sleep).
3️⃣
👉 This is the most important line.
- "LED" → a special name (not a number)
- Pin.OUT → configures the pin as an output
So here:
➡️ We create an object called led that controls the LED.
On the Raspberry Pi Pico WH:
👉 The LED is not connected directly to a regular GPIO pin.
- It is connected to the Wi-Fi chip (CYW43).
- MicroPython provides a special alias: "LED".
So when you write:
➡️ MicroPython automatically knows where the LED is.
➡️ Internally it redirects the command to the correct hardware.
🔹 Classic Pico (without W)
👉 Here you must give the GPIO number.
🔹 Pico W / WH
👉 A special alias is used, which makes things simpler.
4️⃣
👉 This is an infinite loop, meaning the program runs continuously.
5️⃣
👉 Sets the output to 1 (HIGH)
➡️ The LED turns ON
6️⃣
👉 Waits for 1 second
7️⃣
👉 Sets the output to 0 (LOW)
➡️ The LED turns OFF
8️⃣
👉 Waits another second
Save Your Program
When saving a program in Thonny while working with the Raspberry Pi Pico WH, the software will ask where you want to store the file. You will usually have two options: saving the file directly on the Raspberry Pi Pico or saving it on your computer. Saving the file on the board allows the program to run automatically when the Pico starts, but in this tutorial we will save the file on the computer. This option is often more convenient for beginners because it makes it easier to organize, modify, and keep backup copies of your code. Once the program is saved on the computer, it can still be executed and uploaded to the Pico whenever needed.
Upload and Run the Program on the Raspberry Pi Pico WH
After saving the code, you can upload and run it on the Raspberry Pi Pico WH directly from Thonny. To do this, simply click on the Run (or Execute) button in the Thonny toolbar. Thonny will automatically send the program to the Pico and execute it on the board. If everything is configured correctly, you should immediately see the onboard LED starting to blink. The LED will turn on and off every second, confirming that the program is running successfully on the Raspberry Pi Pico WH.
Video