Control the Raspberry Pi Pico WH Onboard LED From Your Smartphone Using MicroPython

by sss2022 in Circuits > Raspberry Pi

96 Views, 4 Favorites, 0 Comments

Control the Raspberry Pi Pico WH Onboard LED From Your Smartphone Using MicroPython

20260330_094824.jpg

The Raspberry Pi Pico WH is a powerful and affordable microcontroller that includes built-in Wi-Fi capabilities thanks to the RP2040 chip and the CYW43439 wireless module. This makes it possible to control hardware remotely using a smartphone, computer, or other network devices.

In this project, we will learn how to control the onboard LED of the Raspberry Pi Pico WH using a smartphone through a simple Wi-Fi web interface. The Pico WH will connect to your local Wi-Fi network and run a small web server written in MicroPython. By opening a web page on your smartphone, you will be able to turn the LED ON or OFF instantly.

This beginner-friendly project is a great introduction to MicroPython, Wi-Fi communication, and basic web control for embedded systems. By the end of this tutorial, you will understand how to connect the Pico WH to Wi-Fi and control its GPIO pins remotely from your phone.

Supplies

  1. Raspberry Pi Pico WH development board
  2. USB-A to Micro-USB cable
  3. Smartphone

Install MicroPython on the Raspberry Pi Pico WH

micropython_raspberry.png
raspberry_RP2.png

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

thonny.png
thonny_tools.png
thonny_options.png
thonny_options2.png

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.

Line-by-Line Explanation of the MicroPython Code

code1.png
code2.png
code3.png
coe4.png

Now that the development environment is ready, we can write a MicroPython program to control the onboard LED of the Raspberry Pi Pico WH using a smartphone.

To begin 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, allowing us to control hardware with simple and readable code. In this project, the Raspberry Pi Pico WH will connect to a Wi-Fi network and create a small web server.

By entering the Pico’s IP address in a smartphone browser, a webpage will appear with buttons that allow us to turn the onboard LED ON or OFF remotely. This example demonstrates how the Pico WH can be used for basic IoT (Internet of Things) applications by controlling hardware through a web interface.

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.

Importing the required libraries

import network

This library allows the Raspberry Pi Pico WH to manage network connections, including connecting to a Wi-Fi network.

import socket

The socket module is used to create a web server so the Pico can communicate with devices such as a computer or smartphone through a web browser.

from machine import Pin

This line imports the Pin class from the machine module. It allows us to control the GPIO pins of the Pico, including the onboard LED.

import time

The time module provides functions related to timing, such as delays using sleep().

Wi-Fi credentials

ssid = "YOUR_WIFI"
password = "YOUR_PASSWORD"

These two variables store the Wi-Fi network name (SSID) and the password required for the Pico WH to connect to the wireless network.

LED configuration

led = Pin("LED", Pin.OUT)

This line configures the onboard LED of the Pico WH as an output pin, which allows the program to turn it on or off.

Connecting to Wi-Fi

wifi = network.WLAN(network.STA_IF)

This creates a Wi-Fi object in station mode, meaning the Pico will connect to an existing Wi-Fi network (like a phone or computer).

wifi.active(True)

This line activates the Wi-Fi interface.

wifi.connect(ssid, password)

The Pico attempts to connect to the Wi-Fi network using the SSID and password defined earlier.

Waiting until the connection is established

while not wifi.isconnected():
time.sleep(1)

This loop keeps checking whether the Pico is connected to Wi-Fi.

If it is not yet connected, the program waits 1 second before checking again.

Displaying the IP address

print("Connected! IP address:", wifi.ifconfig()[0])

Once the connection is successful, the Pico prints its IP address in the Thonny console.

This IP address is used to access the web server from a browser.

Creating the web server

addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]

This line prepares the server address.

0.0.0.0 means the server will accept connections from any device on the network, and port 80 is the standard port used for web servers.

server = socket.socket()

This creates a socket object, which will be used to manage network communication.

server.bind(addr)

This attaches the server to the selected address and port.

server.listen(1)

This tells the server to listen for incoming connections from clients (such as a browser).

print("Server is running...")

This message indicates that the web server is ready.

HTML Web Page

html = """

This variable contains the HTML code for the webpage that will be sent to the browser.

The webpage includes:

  1. A Raspberry Pi logo
  2. A title
  3. Two buttons
<button>LED ON</button>

This button sends a request to the Pico to turn the LED on.

<button>LED OFF</button>

This button sends a request to turn the LED off.

The page also includes CSS styling to make the buttons larger and easier to use on mobile devices.

Main server loop

while True:

This infinite loop keeps the web server running continuously.

client, addr = server.accept()

The server waits for a client (browser) to connect.

request = client.recv(1024)

This line receives the HTTP request sent by the browser.

Checking the requested command

if b'/on' in request:
led.on()

If the request contains /on, the Pico turns the LED on.

if b'/off' in request:
led.off()

If the request contains /off, the Pico turns the LED off.

Sending the webpage to the browser

client.send('HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n')

This line sends an HTTP response header indicating that the request was successful and that the content is HTML.

client.send(html)

The server sends the HTML webpage to the browser.

client.close()

Finally, the connection with the client is closed, and the server waits for the next request.

Save Your Program

thonny_saveprogram2.png

When saving a Python file in Thonny while working with the Raspberry Pi Pico WH, the editor will ask where you want to store the file. You can choose to save it either on the Raspberry Pi Pico itself or on your computer. Saving the file on the Pico means the script is stored directly in the board’s internal memory and can run automatically when the board starts if it is named main.py. This option is useful for standalone projects that need to run without being connected to a computer. On the other hand, saving the file on the computer is often more convenient during development because it allows you to easily edit, organize, and keep backup copies of your code. For this tutorial, it is recommended to save the file on the computer, since it makes it easier to modify and test the program before deploying it to the Pico.

Upload and Run the Program on the Raspberry Pi Pico WH

adresse_ip.png
192.png
20260330_094824.jpg

After saving the code, you can run the program on the Raspberry Pi Pico WH directly from Thonny. To do this, simply click the Run (or Execute) button in the Thonny toolbar.

Thonny will automatically upload the script to the Pico and start executing it on the board. If everything is configured correctly, the Pico WH will connect to your Wi-Fi network and start its small web server. In the Thonny console, you will see the IP address assigned to the board. To access the control page, open a web browser on your smartphone or computer and type http:// followed by the IP address of the Pico. For example, if the address displayed is 192.168.1.25, you should enter http://192.168.1.25 in the browser.

A webpage will then appear with two buttons that allow you to turn the onboard LED ON or OFF remotely, confirming that the program is running correctly and that the Pico WH is successfully communicating over the Wi-Fi network.

Video

Control the Raspberry Pi Pico WH Onboard LED From Your Smartphone Using MicroPython (Thonny IDE)