Skip to content

STEMAIDE AFRICA

Project 1.8.1: Wi-Fi LED Web Controller

Beginner Embedded Systems Project Using Raspberry Pi Pico 2 W and MicroPython

Overview

Build a web-controlled LED using the Pico 2 W's Wi-Fi feature.

This project demonstrates how a tiny web server can control hardware from a phone or computer on the same network.

The final result should open a web page where ON and OFF buttons control the LED remotely.

Required Components

Pico 2 W
Raspberry Pi Pico 2 W
LED
LED
Resistor
220Ω Resistor
Breadboard
Breadboard
Jumper Wires
Jumper Wires
2.4 GHz Wi-Fi Network Phone or Computer Browser

Circuit Connections

Component Pin Connects To Pico GPIO / Physical Pin Number Notes
LED anode (+) 220Ω resistor then GPIO 0 GPIO 0 / Physical Pin 1 LED long leg
LED cathode (-) GND Physical Pin 38 LED short leg

Step-by-Step Assembly

Step 1: Place the Raspberry Pi Pico 2 W

Place the Raspberry Pi Pico 2 W on the breadboard so it sits across the center gap.

Keep the USB port facing outward so you can easily connect it to your computer.

Pico Placement


Step 2: Place the LED

Place the LED on the breadboard with its two legs in different rows.

The long leg is the anode (+).

The short leg is the cathode (-).

LED Placement


Step 3: Add the 220Ω Resistor

Connect the LED long leg to one end of the 220Ω resistor.

The resistor protects both the LED and the Pico GPIO pin.

Resistor Wiring


Step 4: Connect the LED to GPIO 0

Connect the free end of the resistor to GPIO 0.

This pin controls the LED from the web page.

GPIO Wiring


Step 5: Connect the LED Cathode to GND

Connect the LED short leg to GND.

Ground Connection


Wiring Check

  • Pico 2W is placed correctly across the breadboard center gap
  • LED long leg connects through a 220Ω resistor to GPIO 0
  • LED short leg connects to GND
  • No loose jumper wires

Testing Individual Components

Before running the full project, test each part separately. This makes it easier to find wiring or code problems.

LED Test

Check that the LED and resistor are wired correctly before adding Wi-Fi code.

from machine import Pin
import time

led = Pin(0, Pin.OUT)

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

Expected Test Result

The LED should turn on briefly and then turn off.


Wi-Fi Connection Test

Check that Pico 2 W can connect to your Wi-Fi and print its IP address.

import network
import time

SSID = 'YOUR_WIFI_NAME'
PASSWORD = 'YOUR_WIFI_PASSWORD'

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)

for _ in range(15):
    if wlan.isconnected():
        break

    print('Connecting...')
    time.sleep(1)

print('Connected:', wlan.isconnected())

if wlan.isconnected():
    print('IP address:', wlan.ifconfig()[0])

Expected Test Result

The Shell should show Connected: True and print an IP address.


Full Project Code

import network
import socket
import time
from machine import Pin

SSID = 'YOUR_WIFI_NAME'
PASSWORD = 'YOUR_WIFI_PASSWORD'

led = Pin(0, Pin.OUT)

wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(SSID, PASSWORD)

print('Connecting to Wi-Fi...')

for _ in range(15):
    if wlan.isconnected():
        break

    time.sleep(1)

if not wlan.isconnected():
    raise RuntimeError('Wi-Fi connection failed')

ip_address = wlan.ifconfig()[0]

print('Connected. Open http://{} in your browser'.format(ip_address))


def web_page():
    state = 'ON' if led.value() else 'OFF'

    html = """<!DOCTYPE html>
<html>
<head>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<title>Wi-Fi LED Control</title>
<style>
body{font-family:Arial;text-align:center;padding:40px}
.btn{padding:16px 28px;font-size:20px;margin:10px}
</style>
</head>
<body>
<h1>Wi-Fi LED Control</h1>
<p>LED state: STATE_TEXT</p>
<a href='/on'><button class='btn'>ON</button></a>
<a href='/off'><button class='btn'>OFF</button></a>
</body>
</html>"""

    return html.replace('STATE_TEXT', state)


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

server = socket.socket()
server.bind(address)
server.listen(1)

while True:

    client, client_address = server.accept()

    print('Client connected from', client_address)

    request = client.recv(1024).decode()

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

    elif 'GET /off' in request:
        led.off()

    response = web_page()

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

    client.sendall(response)

    client.close()

How the Code Works

Code Section What It Does Why It Matters
Wi-Fi setup Connects the Pico 2 W to your local wireless network The web page cannot work until the board is online
IP address print Shows the address to open in your browser Students need this to reach the Pico web page
web_page() Builds the HTML page with the current LED state The browser needs a page to display buttons and status
Request check Looks for /on or /off in the browser request This is how the LED is controlled from the web page

Expected Result

After entering your Wi-Fi details and running the code, the Shell should print an IP address.

Opening that address in a browser should show ON and OFF buttons that control the LED.


Troubleshooting

Problem Possible Cause Solution
No Wi-Fi connection Wrong SSID or password Recheck the Wi-Fi name and password carefully
IP address does not appear Board is not connected to Wi-Fi Run the Wi-Fi connection test first
Web page loads but LED does not change Wrong GPIO pin or LED wiring Check that the LED is connected to GPIO 0 through the resistor
Browser cannot open the page Phone/computer is not on the same network Connect the browser device to the same Wi-Fi network as the Pico