science tinker

Phone-controlled LED: Raspberry Pi Pico W project with wiring and code

Breadboard wiring for Phone-controlled LED, as drawn in the simulator.

Connect the Raspberry Pi Pico W to Wi-Fi and start a web server in MicroPython. The page at / must have links to /on and /off, and opening them must switch the green LED on GP15 on and off. Print the IP address when the board is connected. Try it with the phone next to the board.

  • Raspberry Pi Pico W
  • Advanced level
  • 3 parts
  • Free, in your browser

You will learn: network.WLAN to join Wi-Fi, a socket that listens on port 80, reading the request line, and sending the status line, headers and page back. This is how a web server works underneath.

Project 84 of 85 in the project gallery. Lesson 5 of 7 in the course Raspberry Pi Pico with MicroPython.

The idea behind this page: how a microcontroller project works

A microcontroller project has three sides that must all be right: the parts, the wiring and the program. A microcontroller is a small computer on one chip. Its pins can read inputs, such as a button or a sensor, and switch outputs, such as an LED, a buzzer or a motor. The program, called a sketch on an Arduino, is the list of instructions that says what the outputs should do when the inputs change. Almost every project follows the same pattern: read an input, decide, then set an output, and repeat this many times a second.

A guided project gives you a goal, the parts, the wiring and starter code with gaps for you to complete. The simulator then checks your work by running it, the way a teacher would test a real circuit. The solution is not shown on this page, because working it out is how the ideas are learned.

This page is about one guided project: Phone-controlled LED. Read it with these ideas in mind.

How to study this page

  1. Read the goal at the top, then the parts table to see what each part is for.
  2. Study the wiring table and the picture: which leg of each part goes to which pin.
  3. Read the starter code line by line before you change it. Find the place where your own lines must go.
  4. Open the project in the simulator, complete the code and press Run. Read any message the check gives you: it says what is wrong and what to write.
  5. When it passes, change one thing (a pin, a delay, a limit) and predict what will happen before you run it again.

Mistakes students make

  • Using a different pin in the code from the one in the wiring.The pin number in the sketch must match the pin the wire really goes to.
  • Leaving out pinMode() in setup().A pin must be set as an input or an output before it is used, or it will not behave as expected.
  • Changing many things at once.Change one thing, run, and see what happens. Then you know which change caused the result.
  • Ignoring the check message.The message names the fault. Read it fully before trying again.

Key words

Microcontroller
A small, complete computer on one chip, with a processor, memory and input and output pins.
Sketch
The Arduino name for a program.
Pin (of a microcontroller)
A metal contact on a chip or board through which it reads an input or drives an output.
Input
A signal going into the microcontroller, for example from a button or a sensor.
Output
A signal coming out of the microcontroller, for example to an LED or a motor.
Breadboard
A board with rows of joined holes for building a circuit without soldering.

See every key word in the glossary

What to learn first and next

Learn this first
Learn this next

Parts you need

PartHow manyWhat it does in this project
Raspberry Pi Pico W1Runs your Python program and powers the circuit.
LED (light-emitting diode)1Turns electric current into light, but only when the current flows the right way (long leg +).
220 Ω resistor1Limits the current so the part it protects is not damaged.
Breadboard and jumper wires1Hold the parts and join them without soldering.

How to wire it

Put the parts on the breadboard, join the board's 5V and GND pins to the breadboard's power rails, then make these connections:

PartConnection
LEDGP15 → LED → GND (220 Ω)

In the Studio, the 💡 guide rings show where each wire goes, and the ✔ Check button tells you which connection is missing.

Starter code

Start from this Python program and write the missing lines. The comments say what each part has to do.

import network
import socket
import time
from machine import Pin

led = Pin(15, Pin.OUT)

# 1. join Wi-Fi
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect("HomeWiFi", "password123")
while not wlan.isconnected():
    time.sleep(0.5)
print("IP:", wlan.ifconfig()[0])

html = '<h1>Pico W</h1><p><a href="/on">ON</a> <a href="/off">OFF</a></p>'

# 2. listen on port 80
addr = socket.getaddrinfo("0.0.0.0", 80)[0][-1]
s = socket.socket()
s.bind(addr)
s.listen(1)

# 3. answer each visitor
while True:
    cl, addr = s.accept()
    request = str(cl.recv(1024))
    # if "GET /on" is in the request, turn the LED on
    # if "GET /off" is in the request, turn the LED off
    # send the status line and headers, then the page, then close:
    # cl.send("HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n")
    cl.close()

Build it step by step

  1. Open the project. Press Build it in the Studio. The parts are already in the tray.
  2. Place and wire the parts. Follow the wiring table above, or switch on the guide rings.
  3. Write the code. Complete the starter program in the Code panel. It is Python, written as text.
  4. Run it. Press ▶ Run and use the part's slider, button or switch to test it. The terminal panel shows what your program prints, and Stop works like Ctrl+C.
  5. Check it. Press ✔ Check: the simulator tests the circuit and the code and tells you exactly what to fix.

Questions students ask

What does the Phone-controlled LED project do?

Connect the Raspberry Pi Pico W to Wi-Fi and start a web server in MicroPython. The page at / must have links to /on and /off, and opening them must switch the green LED on GP15 on and off. Print the IP address when the board is connected. Try it with the phone next to the board.

What parts do I need for the Phone-controlled LED project?

An Raspberry Pi Pico W, LED, 1 × 220 Ω resistor, a breadboard and jumper wires.

How do I wire the Phone-controlled LED project?

LED: GP15 → LED → GND (220 Ω). Power comes from the board's 5V (or 3.3V) and GND pins along the breadboard rails.

What will I learn from the Phone-controlled LED project?

network.WLAN to join Wi-Fi, a socket that listens on port 80, reading the request line, and sending the status line, headers and page back. This is how a web server works underneath.

Can I build this without real hardware?

Yes. The project runs in the free Science Tinker microcontroller simulator in your browser: place the parts, wire them, write the sketch and press Run. The ✔ Check button tests your circuit and code and tells you what to fix. Nothing to install and no account needed.

Where is the finished code?

The page gives the starter sketch so that you write the important lines yourself. If you are stuck, the 💡 Solution button in the Studio shows a complete, working sketch that you can run and study.

More advanced projects

Components in this project

Know someone who would use this?

It’s free for every learner. Send it to your class, team or club.

Updated · Free, no sign-up ·