Esp32 LED Example Code: Light Up Your World with Simple Circuit Tutorial
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.
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:
#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.
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.
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 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!