Wi-Fi Enabled LED Control with ESP32

This project is a simple Web-controlled LED using the ESP32. It leverages the ESP32’s built-in Wi-Fi capabilities and a web server to control an LED remotely through a web browser.

Components:

  1. ESP32 Board
  2. LED (with a suitable resistor, typically 220Ω or 330Ω)
  3. Breadboard (optional but helps with organization)
  4. Jumper wires

Wiring:

  1. ESP32 Pin 23 to LED Anode (Long leg)
    • Pin 23 on the ESP32 is defined as the ledPin in the code. Connect this pin to the longer leg (anode) of the LED.
  2. LED Cathode (Short leg) to Ground (GND)
    • Connect the shorter leg (cathode) of the LED to GND on the ESP32.
  3. Resistor (220Ω – 1kΩ) between LED Cathode and Ground
    • It is important to limit the current flowing through the LED to prevent damage. Place a resistor (220Ω to 1kΩ) in series between the LED’s cathode and the ground pin.

Power:

  • Connect the Vin or 5V pin of the ESP32 to a 5V power source (via USB or battery).
  • The GND pin of the ESP32 will go to the ground of the power source.

Summary of Connections:

  • GPIO 23 -> LED Anode (Long leg)
  • LED Cathode (Short leg) -> Resistor -> GND (ESP32)

Other Details:

  • The ESP32 will serve a web page allowing you to control the LED via the IP address that gets printed in the Serial Monitor after successful Wi-Fi connection.

Once you’ve wired everything, upload the code to your ESP32, open the Serial Monitor, and access the IP address printed in the console to control the LED.

#include <WiFi.h>
#include <WebServer.h>

// Define the GPIO pin connected to the LED
const int ledPin = 23;  // You can change the pin number as needed

// Replace with your network credentials
const char* ssid = "Wif Name";
const char* password = "Wifi Password";

// Create a WebServer object on port 80
WebServer server(80);

void setup() {
  // Start Serial Monitor for debugging
  Serial.begin(115200);

  // Initialize the LED pin as an output
  pinMode(ledPin, OUTPUT);

  // Connect to Wi-Fi
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print(".");
  }

  // Print the IP address when connected
  Serial.println();
  Serial.println("Connected to Wi-Fi");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());

  // Serve the web page to control the LED
  server.on("/", HTTP_GET, []() {
    String html = "<html><body>";
    html += "<h1>Control LED</h1>";
    html += "<a href=\"/led/on\"><button>Turn LED On</button></a><br><br>";
    html += "<a href=\"/led/off\"><button>Turn LED Off</button></a><br><br>";
    html += "</body></html>";
    server.send(200, "text/html", html);
  });

  // Turn LED on
  server.on("/led/on", HTTP_GET, []() {
    digitalWrite(ledPin, HIGH);  // Turn LED ON
    server.sendHeader("Location", "/");  // Redirect to home page
    server.send(303);  // HTTP status code for redirect
  });

  // Turn LED off
  server.on("/led/off", HTTP_GET, []() {
    digitalWrite(ledPin, LOW);  // Turn LED OFF
    server.sendHeader("Location", "/");  // Redirect to home page
    server.send(303);  // HTTP status code for redirect
  });

  // Start the server
  server.begin();
}

void loop() {
  // Handle client requests
  server.handleClient();
}

Workflow:

  1. The ESP32 connects to the Wi-Fi network.
  2. The IP address assigned to the ESP32 is displayed in the Serial Monitor.
  3. You open the IP address in a web browser, which displays the HTML page.
  4. You can click the “Turn LED On” or “Turn LED Off” buttons to control the LED on the ESP32.
  5. The server responds to the button clicks, turning the LED on or off based on the button pressed.

Applications and Benefits:

  • Remote Control: This project demonstrates how to control hardware (in this case, an LED) remotely using a web browser over a Wi-Fi network.
  • IoT Project: It’s a great beginner IoT project that uses the ESP32 to interact with the internet.
  • Learning Opportunity: Helps learn about web servers, HTTP requests, and using GPIO pins on the ESP32.