esp32 led example

Esp32 Led Project: Complete Example

By Richard Young
AD

The ESP32, a low-cost, low-power system on a chip microcontroller with integrated Wi-Fi and dual-mode Bluetooth, has become a favorite among developers for variousInternet of Things (IoT) projects. In this guide, we'll explore a simple yetCalendar affordable ESP32 LED example, helping you understand the basics of controlling LEDs with this versatile chip.

Before we dive into the LED example, ensure you have the following: an ESP32 development board, a breadboard, some jumper wires, and, of course, an LED (any color of your choice). For this tutorial, we'll use an ESP32 DevKitC V4 and an LED connected to GPIO23.

Setting Up Your ESP32 Development Environment

First, let's set up the Arduino IDE to program the ESP32. Install the Arduino IDE if you haven't already. Then, follow these steps to add ESP support:

AD
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
10 ESP32 Skills Every Maker Should Learn
10 ESP32 Skills Every Maker Should Learn
ESP32 GPIO with ESP-IDF with LED Blinking example
ESP32 GPIO with ESP-IDF with LED Blinking example
  • Open the Arduino IDE.
  • Go to File > Preferences.
  • Under 'Additional Boards Manager URLs,' add: https://dl.espressif.com/dl/package_esp32_index.json
  • Go to Tools > Board > Boards Manager.
  • Search for 'esp32' and install 'ESP32' by Espressif Systems.

Now that you've set up your environment, let's proceed with the LED example.

ESP32 LED Pinout and Wiring

For this example, connect your LED's long leg (positive, or anode) to GPIO23 on your ESP32 and the short leg (negative, or cathode) to a GND pin. Ensure the LED is facing the right way; if it's backwards, it won't light up.

ESP32 - WS2812B LED Strip | ESP32 Tutorial
ESP32 - WS2812B LED Strip | ESP32 Tutorial
ESP32 RGB LED Controller Web Server - Remote Color Picker
ESP32 RGB LED Controller Web Server - Remote Color Picker
WLED ON XIAO ESP32
WLED ON XIAO ESP32

ESP32 LED Example Code

Now, let's write the code to control our LED. Open the Arduino IDE and paste the following example:

```cpp #define LED_PIN 23 void setup() { pinMode(LED_PIN, OUTPUT); } void loop() { digitalWrite(LED_PIN, HIGH); // Turn the LED on (HIGH is the voltage level) delay(1000); // Wait for 1000 millisecond(s) digitalWrite(LED_PIN, LOW); // Turn the LED off by making the voltage LOW delay(1000); // Wait for 1000 millisecond(s) } ```

This simple sketch turns the LED on for 1 second, then off for 1 second, creating a 1-second interval blink. Now, upload this code to your ESP32 using the Arduino IDE.

AD

Exploring LED Modes and Patterns

Once you've understood the basics, you can explore different LED modes and patterns. Below are some examples:

💡 ESP32 Web Server – Control an LED from Your Browser!
💡 ESP32 Web Server – Control an LED from Your Browser!
ESP32 Touch Controlled LED Using Arduino IDE
ESP32 Touch Controlled LED Using Arduino IDE
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
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
Anatomy esp32
Anatomy esp32

Blinking with Different Delays

Change the delay times in the loop function to create different blink intervals. For example, try changing the delays to 500 millisecond(s) each to create a faster blink (every 0.5 seconds).

LED Fading

For a more advanced example, let's use PWM (Pulse Width Modulation) to create a fading effect. Here's the modified code:

AD
```cpp int fadeValue = 0; int direction = 1; void setup() { pinMode(LED_PIN, OUTPUT); } void loop() { analogWrite(LED_PIN, fadeValue); fadeValue = fadeValue + direction; if (fadeValue == 255 || fadeValue == 0) { direction = -direction; } delay(10); } ```

Upload this code to your ESP32, and you'll see your LED gradually brighten and dim in an infinite loop.

ESP32 PWM with Arduino IDE (Analog Output) | Random Nerd Tutorials
ESP32 PWM with Arduino IDE (Analog Output) | Random Nerd Tutorials
ESP8266 0.96 inch OLED Display with Arduino IDE | Random Nerd Tutorials
ESP8266 0.96 inch OLED Display with Arduino IDE | Random Nerd Tutorials
ESP32 Pinout Guide: GPIO, Buttons & Ports Explained
ESP32 Pinout Guide: GPIO, Buttons & Ports Explained
How to Power the ESP32 Correctly âš¡
How to Power the ESP32 Correctly âš¡
ESP32 Multiple Sliders WebSocket Web Server - PWM Control LED
ESP32 Multiple Sliders WebSocket Web Server - PWM Control LED
How to Use ESP32 to Control LED With Blynk Via WiFi
How to Use ESP32 to Control LED With Blynk Via WiFi
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
Modulo ESP32 Devkit V1 - Led RGB WS2812B - www.adrirobot.it
Modulo ESP32 Devkit V1 - Led RGB WS2812B - www.adrirobot.it
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
how to send sms from esp32 on the breadboard with an esp23
how to send sms from esp32 on the breadboard with an esp23

Now that you've mastered controlling an LED with the ESP32, explore more advanced projects, such as creating a sunset lamp or integrating LED controls into your IoT devices. Happy tinkering!

AD