ESP32 Web Server: Control an LED from Your Phone over Wi-Fi
Turn an LED on and off from your phone's browser with an ESP32 web server. Wiring, 3.3V notes, commented Arduino IDE code and fixes.
On this page
The ESP32 is a small, cheap board with built-in Wi-Fi and Bluetooth, and it is the natural next step after Arduino Uno for IoT projects. In this tutorial the ESP32 hosts a tiny web page. Open it on your phone, tap a button, and an LED turns on or off. There is no cloud account and no app to install, only your home Wi-Fi router.
Safety note
This project uses 3.3 V logic and a USB cable only. Keep it on your home network and do not expose it to the internet, because this simple page has no password.
What you need
Scroll sideways to see all columns.
| Item | Qty | Notes |
|---|---|---|
| ESP32 development board (30 or 38 pin "DevKit") + micro-USB or USB-C data cable | 1 | The cable must carry data, not only power |
| LED 5 mm | 1 | Long leg = anode (+) |
| Resistor 220 Ω | 1 | |
| Breadboard and jumper wires | 1 set | |
| Wi-Fi router (2.4 GHz) | 1 | The ESP32 does not connect to 5 GHz-only networks |
| Phone or laptop on the same Wi-Fi | 1 | |
| Digital multimeter | 1 |
How it works
The ESP32 joins your Wi-Fi router as a client, and the router gives it a local IP address (for example 192.168.1.50). The ESP32 then runs a small web server on port 80. When your phone opens that address in a browser, the ESP32 replies with an HTML page that contains two buttons. Each button is a link: /on and /off. When you tap one, the ESP32 receives that request, switches the LED pin, and sends the page back.
Two ESP32 facts matter for beginners:
- 3.3 V logic. GPIO pins output 3.3 V (not 5 V) and are not 5 V tolerant. Never feed 5 V signals into them. With a 220 Ω resistor and a red LED you get about (3.3 − 2.0) / 220 ≈ 6 mA, which is safe and visible.
- Pins to avoid. GPIO 6 to 11 connect to the board's flash and must not be used. GPIO 34 to 39 are input only. GPIO 0, 2, 5, 12 and 15 are strapping pins on the classic ESP32. We use GPIO 23 for the LED.
Connection table
Scroll sideways to see all columns.
| ESP32 pin | Connects to |
|---|---|
| GPIO 23 | 220 Ω resistor → LED anode (long leg) |
| GND | LED cathode (short leg) |
| USB | Computer (power and programming) |
Signal path in one line:
Phone browser → Wi-Fi router → ESP32 web server → GPIO 23 → 220 Ω → LED → GND
Step-by-step build
- Install Arduino IDE 2. Open File > Preferences and add Espressif's board manager URL (find the current one in the Espressif Arduino-ESP32 documentation). Then in Boards Manager, install esp32 by Espressif Systems.
- Select the board ESP32 Dev Module and the right serial port.
- Wire the LED on the breadboard as in the table.
- In the code below, change
YOUR_WIFI_NAMEandYOUR_WIFI_PASSWORDto your own. - Upload. If the IDE shows "Connecting....", hold the BOOT button on the board until upload starts (some boards need this).
- Open Serial Monitor at 115200 baud. Press the EN (reset) button. After a few seconds you will see the IP address.
- On a phone connected to the same Wi-Fi, open that address in the browser.
The code
// ElectroDai - ESP32 web server to control an LED
// LED (with 220 ohm resistor) on GPIO 23
#include <WiFi.h>
#include <WebServer.h>
const char* WIFI_SSID = "YOUR_WIFI_NAME";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";
const int LED_PIN = 23;
bool ledOn = false;
WebServer server(80); // HTTP port
// Build the web page. The text changes with the LED state.
String page() {
String html = "<!DOCTYPE html><html><head>";
html += "<meta name='viewport' content='width=device-width, initial-scale=1'>";
html += "<title>ESP32 LED</title></head><body style='font-family:sans-serif;text-align:center'>";
html += "<h1>ElectroDai ESP32 LED</h1>";
html += ledOn ? "<p>LED is <b>ON</b></p>" : "<p>LED is <b>OFF</b></p>";
html += "<p><a href='/on'><button style='font-size:2em;padding:20px'>ON</button></a> ";
html += "<a href='/off'><button style='font-size:2em;padding:20px'>OFF</button></a></p>";
html += "</body></html>";
return html;
}
void handleRoot() { server.send(200, "text/html", page()); }
void handleOn() {
ledOn = true;
digitalWrite(LED_PIN, HIGH);
server.sendHeader("Location", "/"); // go back to the main page
server.send(303, "text/plain", "");
}
void handleOff() {
ledOn = false;
digitalWrite(LED_PIN, LOW);
server.sendHeader("Location", "/");
server.send(303, "text/plain", "");
}
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println();
Serial.print("Open this address on your phone: http://");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/on", handleOn);
server.on("/off", handleOff);
server.begin();
}
void loop() {
server.handleClient(); // answer browser requests
}
Testing with a multimeter
- 3.3 V rail: measure the 3V3 pin to GND. It should read about 3.3 V.
- GPIO 23 when ON: measure GPIO 23 to GND after tapping ON. You should read about 3.3 V, and about 0 V after tapping OFF.
- LED branch: with the LED lit, the voltage across the 220 Ω resistor should be around 1.2 V to 1.5 V for a red LED, which is about 5 to 7 mA.
- USB supply: the 5V (VIN) pin should be close to 5 V when powered by USB.
Troubleshooting
Scroll sideways to see all columns.
| Symptom | Likely cause | Fix |
|---|---|---|
| Stuck on "Connecting...." during upload | Board not in download mode | Hold BOOT during upload; use a data USB cable |
| Dots forever in Serial Monitor | Wrong Wi-Fi name or password, or a 5 GHz-only network | Check spelling (case-sensitive); use a 2.4 GHz network |
| Page does not open | Phone on mobile data or a different network | Join the same Wi-Fi as the ESP32 |
| Garbled text in Serial Monitor | Wrong baud rate | Set it to 115200 |
| LED never lights | LED reversed or wrong pin | Flip the LED; check GPIO 23 |
| Board resets randomly | Weak USB cable or port | Try another cable or port |
| IP address changes next day | Router DHCP | Reserve the address in the router settings |
FAQ
Can I use the built-in LED instead?
Yes. Check whether your board has a controllable LED, its pin and whether it lights on HIGH or LOW. Update LED_PIN and the output levels to match.
Can I control it from outside my home?
Not with this sketch. Opening your router to the internet is a security risk. For remote control, use a cloud service such as Blynk, as in our next tutorial.
Why does the ESP32 need a 2.4 GHz network?
Its Wi-Fi radio supports 2.4 GHz only.
Can I add more LEDs?
Yes. Add more pins and more /on and /off handlers, or use one handler that reads a parameter.
Next step: control and monitor from anywhere in ESP32 IoT Dashboard with Blynk: Send Sensor Data to Your Phone.