ESP32 web server: control an LED from a web page (wiring, code and live simulator)
An ESP32 can serve a web page by itself. It joins your Wi-Fi, starts a web server on port 80 and answers every request: the page has a button, and pressing it calls an address on the board that switches an LED on GPIO4 on or off. You need an ESP32, one LED and one 220 ohm resistor. The circuit below runs in your browser so you can see it working first.
Run it in your browser (free simulator)
Press ▶ Simulate in the box above. Nothing to install and no account needed. Open it in the workbench to change it (free account).
Press Simulate
The board joins the virtual Wi-Fi network and starts its web server. The serial console prints its address.
Open the Browser tab
The Browser tab is a phone on the same Wi-Fi. Open the board's address (for example http://192.168.1.x/) and you see the page the ESP32 serves.
Press the button
The page calls /toggle on the board. The LED in the circuit lights, the page shows ON, and the console prints "LED ON". Press again to turn it off.
Change it
Open the circuit in the workbench and edit the code: add a second address such as /on and /off, or move the LED to another pin. The simulator runs your change at once.
What you need
- An ESP32 DevKit board and its USB cable
- One LED (any colour) and one 220 ohm resistor
- A breadboard and two jumper wires
- A 2.4 GHz Wi-Fi network, and a phone or computer on the same network
- Arduino IDE with the ESP32 board package installed (for the real board)
Careful: Always put the resistor in series with the LED. Without it the LED takes too much current from the GPIO pin and can burn out, or damage the pin.
Wiring: ESP32, resistor and LED
The LED is switched by GPIO4: current flows from the pin through the resistor and the LED to ground. The long leg of the LED (the anode) is on the resistor side, the short leg (the cathode) goes to GND.
| From | To | Why |
|---|---|---|
| ESP32 GPIO4 | Resistor 220 Ω, first leg | The pin that the code switches |
| Resistor 220 Ω, second leg | LED long leg (anode, +) | Limits the current to a safe value |
| LED short leg (cathode, −) | ESP32 GND | Returns the current |
Any other free GPIO works too if you change the LED line in the code. Avoid GPIO6 to 11 (used by the flash chip) and be careful with the boot pins GPIO0, 2 and 12 when something is connected to them at power-up.
How the code works
The sketch uses the WebServer library that comes with the ESP32 board package. setup() joins the Wi-Fi with WiFi.begin(), waits until the connection is up, registers two routes and starts the server. loop() calls server.handleClient() again and again, which is what makes the board answer requests.
The route "/" sends the page: plain HTML with a button and a few lines of JavaScript. The route "/toggle" flips a variable, writes it to the LED pin with digitalWrite() and answers with the text ON or OFF, which the page shows. Splitting the page from the action like this means the page does not reload when you press the button.
The page is stored in the program as one string (PROGMEM). For a bigger page keep it in a separate file in flash with LittleFS instead.
Put it on the real ESP32
Install the ESP32 board package
In Arduino IDE open Boards Manager, install "esp32 by Espressif Systems" and choose ESP32 Dev Module.
Wire the LED
GPIO4 to the resistor, the resistor to the LED anode, the LED cathode to GND, as in the table.
Set your Wi-Fi and upload
Paste the code below into a new sketch, change SSID and PASSWORD, choose the port and press Upload.
Open the address
Open the Serial Monitor at 115200 baud: the board prints its address once it has joined. Type it into a browser on the same network and press the button.
The code (Arduino C++)
This is the Arduino code for the real ESP32 (it needs the ESP32 board package). The simulator runs it too.
Show the code (72 lines)
// ESP32 web server (real board). Join the Wi-Fi, then open the address it prints in a browser.
#include <WiFi.h>
#include <WebServer.h>
const char* SSID = "HomeWiFi";
const char* PASSWORD = "12345678";
const int LED = 4;
bool ledOn = false;
WebServer server(80);
const char PAGE[] PROGMEM = R"rawliteral(<!doctype html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body { font-family: system-ui, sans-serif; text-align: center; margin-top: 40px; }
button { font-size: 22px; padding: 14px 28px; border: 0; border-radius: 10px; color: #fff; background: #6b7280; }
button.on { background: #16a34a; }
</style>
<h1>ESP32 LED</h1>
<p>The LED is <b id="state">OFF</b></p>
<button id="btn">Switch</button>
<script>
const btn = document.getElementById('btn');
const state = document.getElementById('state');
btn.onclick = async () => {
const r = await fetch('/toggle');
state.textContent = await r.text();
btn.className = state.textContent === 'ON' ? 'on' : '';
};
</script>)rawliteral";
void handleRoot() {
server.send(200, "text/html", PAGE);
}
void handleToggle() {
ledOn = !ledOn;
digitalWrite(LED, ledOn ? HIGH : LOW);
Serial.println(ledOn ? "LED ON" : "LED OFF");
server.send(200, "text/plain", ledOn ? "ON" : "OFF");
}
void handleNotFound() {
server.send(404, "text/plain", "Not found: " + server.uri());
}
void setup() {
Serial.begin(115200);
pinMode(LED, OUTPUT);
Serial.print("Connecting to ");
Serial.println(SSID);
WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
}
server.on("/", handleRoot);
server.on("/toggle", handleToggle);
server.onNotFound(handleNotFound);
server.begin();
Serial.print("Open http://");
Serial.print(WiFi.localIP());
Serial.println("/ in a browser");
}
void loop() {
server.handleClient();
delay(2);
}
Troubleshooting
- The Serial Monitor shows only dots, or nothing
- Set the baud rate to 115200. If dots keep coming, the Wi-Fi name or password is wrong or the network is 5 GHz only; the ESP32 needs 2.4 GHz.
- The page does not open
- The phone or computer must be on the same Wi-Fi as the board. Type http:// (not https://) and the exact address from the Serial Monitor. Some guest networks block devices from talking to each other.
- The button works but the LED stays off
- Check the LED direction (long leg to the resistor), the resistor, and that the code LED number is the GPIO you wired. The Serial Monitor prints "LED ON" every time the command arrives, which tells you the web part works.
- The address changes every time
- Your router hands out a new address on each connection. Reserve one for the board in the router (DHCP reservation) or use mDNS to open it by name.
Frequently asked questions
How do I control an LED from my phone with an ESP32?
Run a web server on the ESP32, as in this page. The phone opens the board's address in its browser, the page has a button, and the button calls an address that the board answers by switching the LED. No app and no internet are needed, only a shared Wi-Fi network.
Does the ESP32 web server work without internet?
Yes. The board and the phone only have to be on the same local Wi-Fi network. The router does not need an internet connection.
Which library do I use for the ESP32 web server?
WebServer, which comes with the ESP32 board package, as in this code. ESPAsyncWebServer is a popular alternative that handles many connections at once, at the cost of a separate library to install.
Which GPIO pin should I use for the LED?
GPIO4 here. Any free output pin works. Avoid GPIO6 to 11 (flash), and the input-only pins GPIO34 to 39, which cannot drive an LED.
Can I try this without an ESP32?
Yes. The simulator on this page runs the same code on a virtual ESP32 with a virtual Wi-Fi network and a phone browser, for free in your browser.
Embed this circuit on your own page
Copy this code into your blog, tutorial or course page. Visitors can run the circuit right there.
<iframe src="https://voltbench.click/embed/gallery/esp32-web-server-led" width="100%" height="560" style="border:1px solid #2a3140;border-radius:8px" loading="lazy" allowfullscreen title="ESP32 web server: control an LED from a web page"></iframe>
Build your own version
Open this circuit in the VoltBench workbench, change the wiring or the code, and run it. When it works, flash a real board. Free to use.