esp32 led example code

Esp32 LED Example Code: Light Up Your World with Simple Circuit Tutorial

By Emily Hernandez
AD

The ESP32, a popular microcontroller, is often used in projects that require wireless connectivity and high performance. LEDs are a common component in these projects, serving various purposes like status indicators or even as the primary visual output. This article will guide you through an example of controlling an LED using the ESP32.

Before we dive into the code, let's ensure you have the necessary tools and components. You'll need an ESP32 development board, an LED, and a resistor to prevent overloading the microcontroller. You'll also need the Arduino IDE or PlatformIO installed on your computer to upload the code to your ESP32.

Setting Up the Hardware

Connect your LED's positive leg (anode) to one of the ESP32's GPIO pins, and the negative leg (cathode) to a 220-330 ohm resistor. Then, connect the other end of the resistor to the ground (GND) pin on the ESP32.

AD
10 ESP32 Skills Every Maker Should Learn
10 ESP32 Skills Every Maker Should Learn
an image of a yellow and black cat on a piece of paper with the words epsp
an image of a yellow and black cat on a piece of paper with the words epsp
ESP32 - WS2812B LED Strip | ESP32 Tutorial
ESP32 - WS2812B LED Strip | ESP32 Tutorial

Ensure you've included the necessary libraries in your code. For ESP32 projects, you'll need to install the appropriate board and library settings in the Arduino IDE. You can do this by going to Tools > Board > ESP32 Dev Module, and then Tools > Flash Size > 4MB (320KB SPIFFS).

Connecting to Wi-Fi

Before we control the LED, let's make sure your ESP32 can connect to your Wi-Fi network. Include your network's SSID and password in the code as follows:

💡 ESP32 Web Server – Control an LED from Your Browser!
💡 ESP32 Web Server – Control an LED from Your Browser!
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
Anatomy esp32
Anatomy esp32
#include <WiFi.h>

const char* ssid = "your_network_SSID";
const char* password = "your_network_password";

Controlling the LED

Now, let's write a simple function to turn the LED on and off. We'll use one of the ESP32's GPIO pins to control the LED's brightness:

#define LED_PIN 2

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_PIN, HIGH);
  delay(1000);
  digitalWrite(LED_PIN, LOW);
  delay(1000);
}

In this code, the LED turns on for 1 second, then off for 1 second. You can adjust these delays to change the flashing rate.

AD

Making the LED Interactive

Let's make our LED responsive to external input. A common method is to use an HTTP request to trigger an action, which can be achieved with the ESP32's built-in web server.

ESP32 GPIO with ESP-IDF with LED Blinking example
ESP32 GPIO with ESP-IDF with LED Blinking example
WLED ON XIAO ESP32
WLED ON XIAO ESP32
Internet API for LED Pixel Strip based on ESP32
Internet API for LED Pixel Strip based on ESP32
ESP32 - LED Matrix | ESP32 Tutorial
ESP32 - LED Matrix | ESP32 Tutorial
the words esp now and encrypted are displayed
the words esp now and encrypted are displayed
ESP32 PWM with Arduino IDE (Analog Output) | Random Nerd Tutorials
ESP32 PWM with Arduino IDE (Analog Output) | Random Nerd Tutorials

Creating a Web Server

First, enable the web server and specify the URL that will trigger the LED:

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

WebServer server(80);

const char* ssid = "your_network_SSID";
const char* password = "your_network_password";

const IPAddress ip(192, 168, 1, 100);
const IPAddress gateway(192, 168, 1, 1);
const IPAddress subnet(255, 255, 255, 0);

void setup() {
  // ... Wi-Fi connection code ...

  // Set static IP
  WiFi.config(ip, gateway, subnet);

  // Route for root / web server home page
  server.on("/", HTTP_GET, []() {
    server.send(200, "text/plain", "Hi! The current LED state is: " + String(digitalRead(LED_PIN)));
  });

  // Route for LED control
  server.on("/led", HTTP_GET, []() {
    digitalWrite(LED_PIN, !digitalRead(LED_PIN));
    server.send(200, "text/plain", "LED state changed");
  });

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

Triggering the LED

Now, if you navigate to in your browser, you'll see the current LED state. Sending a GET request to will toggle the LED's state.

AD

In conclusion, controlling an LED with an ESP32 can be as simple or complex as you want it to be. From blinking an LED to creating interactive web servers, the possibilities are endless. So, start exploring, and happy hacking!

how to send sms from esp32 on the breadboard with an esp23
how to send sms from esp32 on the breadboard with an esp23
ESP32 Pinout Guide: GPIO, Buttons & Ports Explained
ESP32 Pinout Guide: GPIO, Buttons & Ports Explained
How to Easily Control Addressable LEDs with an ESP32 or ESP8266 | WLED Project
How to Easily Control Addressable LEDs with an ESP32 or ESP8266 | WLED Project
How to Power the ESP32 Correctly ⚡
How to Power the ESP32 Correctly ⚡
an esp web server is displayed on a smartphone next to a light and circuit
an esp web server is displayed on a smartphone next to a light and circuit
LVGL (Light Versatile Graphics Library) ESP32 Cheap Yellow Display (ESP32-2432S028R) | Random Nerd Tutorials
LVGL (Light Versatile Graphics Library) ESP32 Cheap Yellow Display (ESP32-2432S028R) | Random Nerd Tutorials
the temperature monitor is displayed in this screenshote image, and it shows different types of
the temperature monitor is displayed in this screenshote image, and it shows different types of
ESP32 Touch Controlled LED Using Arduino IDE
ESP32 Touch Controlled LED Using Arduino IDE
ESP-IDF: ESP32 Blink LED Example (VS Code) | Random Nerd Tutorials
ESP-IDF: ESP32 Blink LED Example (VS Code) | Random Nerd Tutorials
ESP32 Tutorials for Beginners
ESP32 Tutorials for Beginners
🌐 ESP32 Static IP Address – Assign a Fixed IP in Arduino IDE!
🌐 ESP32 Static IP Address – Assign a Fixed IP in Arduino IDE!
Beginner electronics mini projects for engineering – ESP32 in Proteus 8 Guide
Beginner electronics mini projects for engineering – ESP32 in Proteus 8 Guide
MicroPython PWM ESP32 & ESP8266 - LED fading & Brightness Control
MicroPython PWM ESP32 & ESP8266 - LED fading & Brightness Control
AD