Phone-controlled LED: ESP32 project with wiring and code
Connect the ESP32 to Wi-Fi and start a web server. The page at / must have links to /on and /off, and opening them must switch the LED on GPIO 23 on and off. Try it with the phone next to the board.
You will learn: WiFi.begin, the WebServer library, server.on() handlers, server.send() and server.handleClient(). This is how real smart-home switches work.
Project 60 of 62 in the project gallery.
Parts you need
| Part | How many | What it does in this project |
|---|---|---|
| ESP32 | 1 | Runs your sketch and powers the circuit. |
| LED (light-emitting diode) | 1 | The output. It switches on and off when the phone opens /on or /off. |
| 220 Ω resistor | 1 | Limits the current so the part it protects is not damaged. |
| Breadboard and jumper wires | 1 | Hold 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:
| Part | Connection |
|---|---|
| LED | GPIO 23 → 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 sketch and write the missing lines. The comments say what each part has to do.
#include <WiFi.h>
#include <WebServer.h>
const char* ssid = "SchoolWiFi";
const char* password = "12345678";
const int LED = 23;
WebServer server(80);
void handleRoot() {
String html = "<h1>My ESP32</h1>";
// add links to /on and /off here
server.send(200, "text/html", html);
}
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Open http://");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
// add handlers for /on and /off
server.begin();
}
void loop() {
server.handleClient();
}
Build it step by step
- Open the project. Press Build it in the Studio. The parts are already in the tray.
- Place and wire the parts. Follow the wiring table above, or switch on the guide rings.
- Write the code. Complete the starter sketch in the Code panel, in text or with blocks.
- Run it. Press ▶ Run and use the part's slider, button or switch to test it. The Serial Monitor shows what your sketch prints.
- 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 ESP32 to Wi-Fi and start a web server. The page at / must have links to /on and /off, and opening them must switch the LED on GPIO 23 on and off. Try it with the phone next to the board.
What parts do I need for the Phone-controlled LED project?
An ESP32, LED, 1 × 220 Ω resistor, a breadboard and jumper wires.
How do I wire the Phone-controlled LED project?
LED: GPIO 23 → 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?
WiFi.begin, the WebServer library, server.on() handlers, server.send() and server.handleClient(). This is how real smart-home switches work.
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.