Skip to main manual content
Circuit Manual
BeginnerESP323.3V Logic / 5V Load

ESP32 Local WiFi Desk Lamp Controller With Web Switch and PWM Dimmer

Build a local WiFi web server on an ESP32 to dim a 5V LED strip or desk lamp using hardware PWM, a logic level MOSFET, and physical button sync.

By Chathura PrabashanaUpdated Nov 18, 2026
Difficulty
Beginner
Build / Read Time
60 mins
Operating Voltage
3.3V Logic / 5V Load
Hardware Platform
ESP32 DevKit V1
Est. Parts Cost
$14 USD
Direct Summary / Key Takeaway

Run a lightweight local HTTP server directly on the ESP32 using the built in WebServer library, drive a 5V LED load safely through an IRLZ44N logic level N channel MOSFET on GPIO25 using 5 kHz hardware PWM, and add a 10 kOhm gate pull down resistor so the lamp never flickers during boot.

Once you can blink a tiny indicator LED, the next milestone in hardware engineering is controlling a real higher power load over WiFi. In this build, your ESP32 hosts a clean local web dashboard that smoothly dims a 5V LED desk light bar using hardware while staying synced with a physical desk button.

01. Circuit Theory: Why We Use a Logic Level MOSFET

Microcontroller pins are brains, not muscle. An ESP32 GPIO pin outputs 3.3V and should stay under 12 mA to 20 mA continuous current. A bright 5V LED light bar easily pulls 250 mA to 400 mA at 5V.

An N channel bridges those two worlds cleanly:

  1. GPIO25 drives the Gate pin through a 220 Ohm resistor. Because a MOSFET gate draws almost zero steady current, GPIO25 barely works at all.
  2. A 10 kOhm pull down resistor connects the Gate to GND. Whenever the ESP32 restarts, GPIO25 floats briefly as an input; without this pull down resistor, static charge on the gate could turn the lamp on unpredictably.
  3. Low side switching: The 5V LED positive wire stays tied to +5V, while the MOSFET switches the negative wire to GND at 5,000 cycles per second (5 kHz), fast enough to look completely flicker free to human eyes and phone cameras.

02. Bill of Materials

WiFi PWM Lamp Controller BOM

6 Line Items / 6 Parts Total
Part NameSpecificationQtyBeginner Notes
ESP32 DevKit V1 (30 Pin)ESP32-WROOM-32, 2.4 GHz WiFi1Connects to your home 2.4 GHz WiFi router; 5 GHz only networks are unsupported.
Logic Level N Channel MOSFETIRLZ44N or IRL540N (TO-220 package)1Must be a logic level part (IRL prefix) so 3.3V at the gate turns it on cleanly.
Gate Current Resistor220 Ohm, 1/4W (Red, Red, Brown, Gold)1Limits brief inrush current from GPIO25 into the MOSFET gate capacitance.
Gate Pull Down Resistor10 kOhm, 1/4W (Brown, Black, Orange, Gold)1Keeps the MOSFET firmly OFF while the ESP32 boots or resets.
5V USB LED Light Bar or 5V LED Strip Segment5V DC, <= 400 mA draw (low voltage only)1Stay strictly on 5V DC low voltage LED loads; never wire mains AC to a breadboard.
Momentary Tactile Pushbutton6mm x 6mm, through hole1Provides instant physical toggle override on GPIO26.

03. Wiring Schedule and Schematic

MOSFET Low Side Dimmer Wiring Schedule

6 Jumpers
Source PinDestination PinWire ColorSignal / Purpose
ESP32 GPIO25 (D25)220 Ohm Resistor -> MOSFET Gate (Pin 1)OrangeDelivers 5 kHz 3.3V PWM dimming signal to the MOSFET gate
MOSFET Gate (Pin 1)10 kOhm Resistor -> Common GNDBrownPull down resistor holding the lamp off during microcontroller reset
MOSFET Source (Pin 3)ESP32 GND / Common GND RailBlackLow side current return path to ground
MOSFET Drain (Pin 2)5V LED Lamp Negative (-) WireBlueSwitches the negative side of the 5V LED load rapidly for smooth dimming
5V Power Rail (VIN or External 5V Supply)5V LED Lamp Positive (+) WireRedProvides steady 5V DC supply to the LED bar anode
ESP32 GPIO26 (D26)Tactile Button Pin 1 (Other side to GND)YellowManual physical toggle using internal INPUT_PULLUP resistor

SCHEMATIC FIG 02 / LOW SIDE IRLZ44N MOSFET PWM STAGE

ESP32 DEVKITGPIO25 (PWM)GPIO26 (BTN)GND220 OHM10kIRLZ44NN-CH MOSFETG: PIN1D: PIN2S: PIN35V LED BAR+5V IN- LOAD DRAIN+5V DC SUPPLYSW

04. Commented ESP32 Firmware

Notice how this sketch uses ESP32 Arduino Core v3.x ledcAttach() for flicker free 5 kHz PWM and serves a crisp, zero dependency HTML control panel right from the chip.

wifi_pwm_lamp.inocpp
Library Note:Built in ESP32 Arduino Core v3.0+ libraries only (WiFi.h and WebServer.h); zero external downloads needed
#include <WiFi.h>
#include <WebServer.h>

const char* WIFI_SSID = "YOUR_24GHZ_WIFI_NAME";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";

const int PIN_PWM_GATE = 25;
const int PIN_BUTTON   = 26;
const int PWM_FREQ_HZ  = 5000;
const int PWM_RES_BITS = 8; // 0 to 255 duty range

WebServer server(80);

bool lampOn = false;
int brightness = 180; // Default 70% brightness when toggled on
unsigned long lastButtonMs = 0;

void applyLampOutput() {
int duty = lampOn ? brightness : 0;
ledcWrite(PIN_PWM_GATE, duty);
}

String renderDashboardHtml() {
int pct = map(brightness, 0, 255, 0, 100);
String html = "<!doctype html><html><head><meta name='viewport' content='width=device-width,initial-scale=1'>";
html += "<title>Circuit Manual Lamp</title><style>";
html += "body{font-family:monospace;background:#F6F4EF;color:#161615;max-width:420px;margin:2rem auto;padding:1rem;}";
html += ".box{border:1px solid #161615;background:#FDFCFA;padding:1.25rem;}";
html += "a.btn{display:inline-block;padding:0.65rem 1.1rem;background:#B84A14;color:#fff;text-decoration:none;font-weight:bold;}";
html += "input[type=range]{width:100%;margin:1rem 0;}";
html += "</style></head><body><div class='box'>";
html += "<h2>BENCH LAMP / GPIO25</h2>";
html += "<p>STATE: <strong>" + String(lampOn ? "ON" : "OFF") + "</strong> | DUTY: <strong>" + String(pct) + "%</strong></p>";
html += "<p><a class='btn' href='/toggle'>TOGGLE LAMP</a></p>";
html += "<form action='/dim' method='GET'><label>BRIGHTNESS (0-255)</label>";
html += "<input type='range' name='val' min='15' max='255' value='" + String(brightness) + "' onchange='this.form.submit()'>";
html += "</form></div></body></html>";
return html;
}

void setup() {
Serial.begin(115200);
pinMode(PIN_BUTTON, INPUT_PULLUP);

// Attach hardware PWM channel directly to GPIO25 (ESP32 Core v3 API)
ledcAttach(PIN_PWM_GATE, PWM_FREQ_HZ, PWM_RES_BITS);
applyLampOutput();

WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.print("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
  delay(400);
  Serial.print(".");
}
Serial.println();
Serial.print("Dashboard ready at http://");
Serial.println(WiFi.localIP());

server.on("/", []() {
  server.send(200, "text/html", renderDashboardHtml());
});

server.on("/toggle", []() {
  lampOn = !lampOn;
  applyLampOutput();
  server.sendHeader("Location", "/");
  server.send(303);
});

server.on("/dim", []() {
  if (server.hasArg("val")) {
    brightness = constrain(server.arg("val").toInt(), 0, 255);
    lampOn = brightness > 0;
    applyLampOutput();
  }
  server.sendHeader("Location", "/");
  server.send(303);
});

server.begin();
}

void loop() {
server.handleClient();

// Hardware pushbutton check with 250 ms debounce window
if (digitalRead(PIN_BUTTON) == LOW && (millis() - lastButtonMs > 250)) {
  lastButtonMs = millis();
  lampOn = !lampOn;
  applyLampOutput();
  Serial.printf("Button toggled lamp: %s\n", lampOn ? "ON" : "OFF");
}
}

05. Bench Verification Steps

  1. Before connecting the 5V LED strip, test with a simple red LED and 220 Ohm resistor tied from +3.3V to the MOSFET Drain so you can verify switching safely.
  2. Open the Serial Monitor at 115200 baud and note the IP address printed (for example http://192.168.1.48).
  3. Visit that URL from any phone or computer on the same WiFi network, drag the dimmer slider, and tap the tactile button on GPIO26 to confirm both hardware and browser stay cleanly in sync.
Diagnostics & Common Questions

Testing and Troubleshooting FAQ

Why cannot I plug a 300 mA 5V LED light strip directly into an ESP32 GPIO pin?+

Each ESP32 GPIO pin can safely supply at most 20 mA to 40 mA at 3.3V. Drawing 300 mA directly from a GPIO pin will permanently burn out the output driver inside the chip. The MOSFET acts as an electronic valve controlled by a tiny 3.3V gate voltage while carrying the heavy 5V load current directly to ground.

Can I substitute an IRF540 or IRF520 MOSFET instead of an IRLZ44N?+

Not recommended for 3.3V boards. Standard IRF series MOSFETs expect 10V on the gate to fully saturate. At 3.3V from an ESP32, an IRF520 stays half closed, dimming poorly and getting hot. Choose a logic level MOSFET such as IRLZ44N, IRL540N, or AO3400.

Does the ESP32 web dashboard require internet access or a cloud account?+

No. The HTML control page is hosted directly inside the ESP32 flash memory over your local WiFi network. It responds in milliseconds with zero subscriptions and continues working even if your external internet connection goes offline.

Chathura Prabashana
Verified Author / Bench Tester

Chathura Prabashana

Embedded Systems Builder and IT Undergraduate

Chathura Prabashana builds, tests, and documents beginner friendly ESP32 circuits, microcontroller firmware, and practical electronics prototypes from scratch.

About the Author & Verification Standards →