esp32 led blink example

ESP32 LED Blink Tutorial: Make Your LEDs Flash in No Time!

By Andrew Walker
AD

Embarking on your journey into the world of IoT with the ESP32, one of the first and arguably most fundamental concepts to grasp is lighting up an LED. This simple task serves as an excellent starting point, allowing you to get familiar with the ESP32's basic functionality and setup. In this guide, we'll walk you through an in-depth look at an ESP32 LED blink example, ensuring you understand the underlying concepts and can apply them to more complex projects.

The ESP32 is a low-cost, low-power System on a Chip microcontroller with integrated Wi-Fi and dual-mode Bluetooth. Its versatility and affordability make it an ideal choice for a wide range of Internet of Things (IoT) applications. By the end of this guide, you'll have a working knowledge of how to blink an LED using an ESP32, setting a strong foundation for your future IoT projects.

ESP32 Pinouts and LEDs

Before diving into the code, let's familiarize ourselves with the ESP32's pinouts and the LEDs we'll be working with.

AD
ESP32 GPIO with ESP-IDF with LED Blinking example
ESP32 GPIO with ESP-IDF with LED Blinking example
ESP32 and ESP8266 GPIO Programming with MicroPython - LED Blinking
ESP32 and ESP8266 GPIO Programming with MicroPython - LED Blinking
ESP32 - Blink multiple LED | ESP32 Tutorial
ESP32 - Blink multiple LED | ESP32 Tutorial

The ESP32 Development Board, for instance, usually features two built-in LEDs: one is reddish-orange (ADC_PIN to GND), and the other is green (IO25 to GND). These LEDs are Reading and Ready, respectively, and serve as indicators for the board's status. For this guide, we'll use the green LED, as it's active high, making it easier to work with.

Understanding ESP32 GPIO Pins

ESP32 GPIO (General-Purpose Input/Output) pins are where the action happens. They're bi-directional, meaning you can configure them to work as inputs or outputs. The GPIO pins allow the ESP32 to interact with the external world, connecting to sensors, displays, actuators, and, of course, LEDs.

How to Use ESP32 to Control LED With Blynk Via WiFi
How to Use ESP32 to Control LED With Blynk Via WiFi
How to Blink an Led Bulb Using ESP32 Board With Blynk App
How to Blink an Led Bulb Using ESP32 Board With Blynk App
ESP-IDF: ESP32 Blink LED Example (VS Code) | Random Nerd Tutorials
ESP-IDF: ESP32 Blink LED Example (VS Code) | Random Nerd Tutorials

There are 36 GPIO pins on the ESP32, and they're grouped into blocks. The pins we'll focus on in this guide are within the first block (GPIO0 to GPIO19), which is connected directly to the processor. These pins are useful for general-purpose tasks like driving LEDs, controlling relays, or interfacing with simple sensors.

Marking Up Our Project: The Circuit Diagram

For this LED blink example, no external components or breadboarding are necessary. We'll only use the ESP32's built-in green LED. However, if you'd like to connect an external LED, you can use any standard LED, preferably one with a forward voltage (Vf) between 2.0V and 3.4V to match the ESP32's GPIO pins' output voltage.

AD

To connect an external LED, follow these simple steps:

10 ESP32 Skills Every Maker Should Learn
10 ESP32 Skills Every Maker Should Learn
ESP32 Simulator - Arduino Core - LED Blink Example - How to use ESP32 Wokwi Simulator
ESP32 Simulator - Arduino Core - LED Blink Example - How to use ESP32 Wokwi Simulator
ESP32 LED Blink in Proteus8 Simulation - Complete Beginner Guide
ESP32 LED Blink in Proteus8 Simulation - Complete Beginner Guide
ESP32 RGB LED Controller Web Server - Remote Color Picker
ESP32 RGB LED Controller Web Server - Remote Color Picker
ESP32 LED Blink Simulation Project for Students
ESP32 LED Blink Simulation Project for Students
💡 ESP32 LED Control Web Server – Toggle LEDs from Your Browser!
💡 ESP32 LED Control Web Server – Toggle LEDs from Your Browser!
  • Connect the LED's positive leg (anode) to one of the ESP32's GPIO pins (e.g., IO23).
  • Connect the LED's negative leg (cathode) to a ground (GND) pin on the ESP32.
No need for resistors, as the ESP32's output can safely drive an LED without risking damage or excessive current draw.

ESP32 Firmware and Development Environment

Before coding, ensure you have the correct firmware on your ESP32 and set up your development environment to program it.

AD

The ESP32 comes with various firmware options like Espressif IDF (ESP-IDF) and Arduino. For this guide, we'll stick with Arduino, as it offers an intuitive, user-friendly environment and is well-documented. To set up the Arduino IDE for ESP32 programming, follow these steps:

WLED ON XIAO ESP32
WLED ON XIAO ESP32
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
DAY 5 — ESP32 Explained | Wi-Fi & Bluetooth Microcontroller Notes
ESP32 LED Blink Proteus Simulation - Free Files Included
ESP32 LED Blink Proteus Simulation - Free Files Included
Introduction to ESP32 Simulation: LED Blink in Proteus 8
Introduction to ESP32 Simulation: LED Blink in Proteus 8
Programming ESP8266 ESP-12E NodeMCU Using Arduino IDE - a Tutorial
Programming ESP8266 ESP-12E NodeMCU Using Arduino IDE - a Tutorial
ESP32 PWM with Arduino IDE (Analog Output) | Random Nerd Tutorials
ESP32 PWM with Arduino IDE (Analog Output) | Random Nerd Tutorials
Anatomy esp32
Anatomy esp32
ESP32 GPIO Pins Programming in Arduino IDE - LED Blinking Example
ESP32 GPIO Pins Programming in Arduino IDE - LED Blinking Example
5 Must-Try ESP32 Projects for Beginners (2025 Edition)
5 Must-Try ESP32 Projects for Beginners (2025 Edition)
ESP32 - LED Matrix | ESP32 Tutorial
ESP32 - LED Matrix | ESP32 Tutorial
How to Power the ESP32 Correctly âš¡
How to Power the ESP32 Correctly âš¡
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
LED Tutorial for Arduino, ESP8266 and ESP32
LED Tutorial for Arduino, ESP8266 and ESP32

1. Download and install the Arduino IDE from the official website: https://www.arduino.cc/en/main/software

2. Open the Arduino IDE and navigate to 'File' > 'Preferences'. A dialog box will appear. In the 'Additional Boards Manager URLs' field, paste the following URL and click 'OK':
http://arduino.esp8266.com/stable/package_esp8266com_index.json

AD

3. Go to 'Tools' > 'Board' > 'Boards Manager...'. Search for 'ESP8266' and install the ESP boards package by ESP8266 Community. You're now ready to program your ESP32.

Selecting the Correct Board and Port

To program your ESP32, first select the board type from 'Tools' > 'Board': ESP32 Dev Module (or whatever the name of your specific ESP32 board is).

Next, ensure you've correctly connected your ESP32 to your computer. When done correctly, a COM port should appear in the Arduino IDE in 'Tools' > 'Port'. Select the COM port that matches the one your ESP32 is connected to.

Writing the LED Blink Code for ESP32

Now that we have our hardware and development environment set up, let's write and upload the LED blink code to our ESP32.

The code is quite simple, as the core of it consists of a loop that digitally writes 'HIGH' or 'LOW' to the pin connected to our LED. These values correspond to logical true and false, respectively.

Arduino Sketch: Basic LED Blink

Open the Arduino IDE and create a new sketch. Replace any existing code in the sketch with the following:

```cpp const int LED_PIN = 2; // Connect the positive leg of your LED to this pin void setup() { pinMode(LED_PIN, OUTPUT); } void loop() { digitalWrite(LED_PIN, HIGH); // Turn on the LED delay(1000); // Wait for 1000 milliseconds (1 second) digitalWrite(LED_PIN, LOW); // Turn off the LED delay(1000); // Wait for 1000 milliseconds (1 second) } ```

In this sketch, we've defined a constant integer ('const int') called LED_PIN, which signifies the GPIO pin connected to our LED. In our setup function, we declare this pin as an OUTPUT using the pinMode() function.

In the loop function, we use the digitalWrite() function to turn the LED on (HIGH) and off (LOW). Between these commands, we insert a delay() function, which pauses the execution of the code for a specified amount of time in milliseconds, providing us with equal duration for each ON and OFF state.

Uploading and Testing Your Code

To upload your code to the ESP32, click the 'Upload' button (щиток with a right-facing arrow) in the Arduino IDE. The IDE will compile your code and upload it to your ESP32. Once complete, you should see the green LED (or your external LED if connected) blinking on and off every second.

Congratulations! You've successfully made your first ESP32 LED blink project. As a next step, you might consider adjusting the delay between blinks, adding more LEDs, or even connecting external components to explore the full potential of your ESP32 board.

Get creative, experiment, and have fun exploring the world of IoT with your ESP32. Happy blinking!

AD