#103Beginner

LED Blink with ESP32

Teaches basic GPIO output usage with ESP32. We achieve a blinking effect by turning an LED on and off from a digital pin. ESP32 version of the classic blink project on Arduino UNO; the basic blink commands in the Arduino environment are the same, but the ESP32's pin layout and 3.3 V GPIO levels differ from those of the Arduino UNO.

4.7(36)
96 completed
LED Blink with ESP32
Your first step with the ESP32: the LED connected to the board lights up for half a second, goes dark for half a second, and keeps going forever. All it takes is one LED, one resistor and a few wires. Once the LED starts blinking, you know the board works and your program is loaded.

Video

Circuit Diagram

LED Blink with ESP32 - Circuit

Source Code

Arduino (C++)
1// ─────────────────────────────────────────
2//   BlueGrays · bluegrays.com
3//   LED Blink with ESP32
4// ─────────────────────────────────────────
5// Blinks an LED on GPIO 5 of the ESP32 every 500 ms. It is the ESP32
6// version of the classic Arduino blink sketch; only the pin number
7// changes.
8//
9// Pins:
10//   GPIO 5 -> Red LED (with a 220 ohm resistor)
11//   GND    -> LED short leg (cathode)
12
13void setup() {
14  pinMode(5, OUTPUT);     // LED pin as output
15}
16
17void loop() {
18  digitalWrite(5, HIGH);  // Turn the LED on
19  delay(500);             // Wait 0.5 s
20  digitalWrite(5, LOW);   // Turn the LED off
21  delay(500);
22}
23

FAQ

Which pins on the ESP32 can I use for an LED?▾

GPIO 2, 4, 5, 12–19, 21–23, 25–27, 32, and 33 can be safely used as outputs. GPIO pins 34–39 are input-only and cannot drive LEDs. GPIO pins 6–11 are connected to internal flash memory and must not be used under any circumstances.

What is the appropriate resistor for an LED with an ESP32 at 3.3 V?▾

The ESP32 pins supply 3.3 V. A 100–220 Ω resistor is suitable for the red LED; a 220 Ω resistor provides approximately 6–7 mA of current, and the LED is clearly visible. Since blue and white LEDs have a high forward voltage, they may appear dimmer at 3.3 V.

When the code is loading, it says "Connecting..." and then freezes. What should I do?▾

On some ESP32 boards, you need to hold down the BOOT button to enter upload mode. When you see "Connecting...," hold down the BOOT button, and release it once the upload begins. Also, make sure the USB cable is a data cable.

Will the Arduino code run on the ESP32 without any changes?▾

The basic functions (pinMode, digitalWrite, delay) are the same; most of the time, you just need to change the pin numbers. Under the Tools menu, select "ESP32 Dev Module" as the board.