LED guides · On this page

Blink an LED with an ESP32

Blink an LED with an ESP32: on for a second, off for a second, as Arduino's Blink example does. It's wired as on an Arduino, with a resistor from a pin to the LED and the LED to GND. The ESP32's pins give 3.3 V, not 5 V, so the same 220 Ω resistor lights a red LED a little less brightly.

This guide uses an ESP32 DevKitC, the common ESP32-WROOM-32 board, with a red LED on GPIO 19. For which leg is which and how the resistor is worked out, see the LED guide.

Try it first, no parts needed

This is the whole project, wired and running, with an LED on GPIO 19 through a 220 Ω resistor. Press Run simulation. The LED turns on for a second and off for a second, and the Serial Monitor prints LED on and LED off.

Ready
Open in Cirkit Designer

The LED, blinking

Its glow, enlarged.

Lights up when the simulation runs.

ESP32 serial monitor115200 baud

Output appears here when the simulation runs.

What the code does

The sketch switches GPIO 19 on, waits a second, switches it off, waits a second, and repeats.

esp32_blink.ino
  1. const int LED_PIN = 19;

    The LED is on GPIO 19. Most ESP32 pins can blink an LED; see which pins work.

  2. Serial.begin(115200);

    Opens the serial port at 115200 baud, the speed of the ESP32's own start-up lines too.

  3. pinMode(LED_PIN, OUTPUT);

    Makes GPIO 19 an output, so the sketch can switch it between 0 V and 3.3 V.

  4. digitalWrite(LED_PIN, HIGH);  // on

    GPIO 19 to 3.3 V: current flows through the resistor and the LED, and it lights. The sketch prints LED on, and delay(1000) waits a second.

  5. digitalWrite(LED_PIN, LOW);   // off

    GPIO 19 to 0 V: no current, so the LED goes dark for another second, and loop() starts again.

Build it on an ESP32

With real parts, wire it, get the code, then upload it and watch it blink. Not sure which leg is which? See the LED pinout.

You'll need

  • An ESP32 DevKitC (ESP32-WROOM-32) and a USB cable that carries data
  • A red LED, 5 mm or 3 mm
  • A 220 Ω resistor (red, red, brown bands)
  • A breadboard and two jumper wires
  • Software to upload the sketch: either Cirkit Designer, free in your browser, or the Arduino IDE with Espressif's ESP32 boards installed
  1. Wire it

    Two wires put the LED and its resistor in a row from GPIO 19 to GND.

    Tap a connection to see it on the circuit.

    PinConnect to the ESP32Show
    LED anode (long leg)220 Ω resistor, then GPIO 19Show
    LED cathode (short leg)GNDShow

    220 Ω works at 3.3 V. It gives a red LED about 6 mA, (3.3 − 1.9 V) ÷ 220 Ω, against about 14 mA on an Uno.

  2. Get the code

    I'm using:

    Steps 2 and 3 follow the tool you pick.

    Open in Cirkit Designer

    The project opens with the sketch already in it, so there's nothing to install.

    Install the ESP32 boards first: open Tools → Board → Boards Manager, search for esp32 and install esp32 by Espressif. The sketch needs no library. Copy it into a new sketch:

    How it works line by line is in what the code does, above.

  3. Upload and check it

    Plug in your ESP32. In Cirkit, open the Build tab, then Upload firmware: click Select Port and pick your board, then Upload to Device. The first time, Cirkit asks you to install Cirkit Create Agent, a small desktop helper that lets it reach the board over USB. Then open Upload output and serial monitor and click Start Serial Monitor.

    Show me where to click in Cirkit
    Blink an LED with an ESP32FileEditExportHelp
    DesignCodeSimulateBuild
    1Parts & purchasing2Assemble3Upload firmware
    ESP32Edit Code

    Board

    ESP32 Change

    2

    Port

    Select PortCOM4 Connected

    3

    Upload

    Upload to DeviceUploading...Upload successful

    4

    Check that it works

    Your project should now run as designed.

    Upload output and serial monitor
    Compile / Upload Output
    Sketch uses 282203 bytes (21%) of program storage space. Maximum is 1310720 bytes.
    Global variables use 20800 bytes (6%) of dynamic memory, leaving 306880 bytes for local variables. Maximum is 327680 bytes.
    Device SerialStop

    Read serial data from your device.

    Start Serial Monitor
    Listening on COM4 at 115200 baud
    LED on
    LED off
    LED on
    LED off
    Select port

    Select a device port

    Select the device port for uploading code and serial communications

    COM4 (ESP32)
    1. Open Build
    2. Choose Upload firmware
    3. Click Select Port
    4. Pick COM4 (ESP32)
    5. Click Upload to Device
    6. Open Upload output and serial monitor
    7. Click Start Serial Monitor

    In the Arduino IDE, choose ESP32 Dev Module and its port from the board menu, press Upload, then open the Serial Monitor and set it to 115200 baud.

    Show me where to click in the Arduino IDE
    Select BoardESP32 Dev Module

    Select other board and port

    ESP32 Dev ModuleCOM4
    esp32_blink.ino
    // Blink: the LED on GPIO 19 on for a second, off for a
    // second, and the Serial Monitor says which.
    const int LED_PIN = 19;
    
    void setup() {
      Serial.begin(115200);
    OutputSerial Monitor115200 baud

    Compiling sketch...

    Sketch uses 282203 bytes (21%) of program storage space. Maximum is 1310720 bytes.

    Global variables use 20800 bytes (6%) of dynamic memory, leaving 306880 bytes for local variables. Maximum is 327680 bytes.

    Uploading...

    LED on

    LED off

    LED on

    LED off

    Done uploading.

    1. Click the board menu
    2. Pick ESP32 Dev Module on COM4
    3. Click Upload
    4. Wait for Done uploading
    5. Click Serial Monitor
    6. Check it is set to 115200 baud

    It works when:

    • The LED blinks, on for a second and off for a second.
    • After a few start-up lines from the ESP32's boot ROM, the Serial Monitor prints LED on and LED off in step with the LED.
    The LED on the ESP32 twice: lit, with GPIO 19 HIGH; then dark, with GPIO 19 LOW. Both times GPIO 19 runs through the 220 ohm resistor to the LED's long leg, and its short leg goes to GND.
    Left: GPIO 19 HIGH and the LED lit. Right: a second later, GPIO 19 LOW and the LED dark.

    Not working? See troubleshooting.

    Next guideNext: dim it with a knobThe same LED plus a knob: on an Uno with analogWrite(), then on an ESP32 with its LED controller, LEDC.

Which ESP32 pins can drive the LED

Most ESP32 pins can be an output and blink an LED; GPIO 19 is one. Avoid the pins in this table: some can't be outputs, and the others are used for flash memory, for choosing how the chip boots, or for USB.

AvoidClassic ESP32ESP32-S3
Input onlyGPIO 34 to 39None
FlashGPIO 6 to 11GPIO 26 to 32, and 33 to 37 on modules with octal flash or octal PSRAM
Boot (strapping)GPIO 0, 2, 5, 12 and 15GPIO 0, 3, 45 and 46
Native USBNoneGPIO 19 and 20, the USB port's D− and D+

Troubleshooting

Find your symptom; the checks under it run from the most common cause down.

The LED doesn't light, but the Serial Monitor prints LED on and LED off

The sketch runs, but no current flows through the LED. Check, in order:

  • The LED is the right way round. The long leg goes toward GPIO 19, the short leg to GND.
  • The wire is on GPIO 19. It must be the pin the sketch names, and not an input-only pin, 34 to 39; see which pins work.
  • The circuit is complete. GPIO 19, the resistor, the LED and GND form one loop.

Nothing in the Serial Monitor, or garbled characters

  • The Serial Monitor is set to 115200 baud. It must match Serial.begin(115200) in the sketch. The ESP32's own start-up lines, from its boot ROM, come first at the same speed, so they should be readable too.

No port appears for the ESP32

  • The cable carries data. The board's USB port carries its serial link as well as power, so a cable that only charges powers the board but shows no port.
  • The board's USB-to-serial chip has its driver. Its name, such as CP210x, is printed on the chip near the USB socket. The driver usually installs itself; if no port appears, install it from the chip's maker (Silicon Labs for a CP210x).

The upload stops at Connecting..., then says Failed to connect

  • The board is in download mode. Hold BOOT, press and release EN (RST on some boards), then release BOOT, and upload again.

More causes are in the LED guide's troubleshooting.

FAQ

How do I dim the LED on an ESP32?

With PWM, from the ESP32's LED controller, LEDC: analogWrite() works from 0 to 255, or ledcAttach() and ledcWrite() let you choose the speed and resolution. See dim it on an ESP32.

Do I need a different resistor at 3.3 V?

No, 220 Ω works; see wire it. For another current, divide (3.3 V − the LED's forward voltage) by the current, as the LED guide shows.

Does the same code work on an ESP32-S3?

Yes, with the LED on another pin. GPIO 19 is the S3's USB D− pin, so put the LED on another, such as GPIO 4, and set LED_PIN to 4. In the Arduino IDE, choose ESP32S3 Dev Module.

Can I blink the DevKitC's own LED instead?

No. The ESP32 DevKitC's only LED shows that the board has power, and no pin controls it, so wire an LED to a pin as above.

Can I use an RGB LED on an ESP32?

It depends on the LED. Driven straight from the pins through resistors, a 5 mm RGB LED such as the Fedy FD-5WSRGB-A, whose green and blue drop about 3.2 V typically, has only 0.1 V to spare at 3.3 V, so those colours may be dim. The RGB guide runs on a 5 V Uno.

Simulating the LED on an ESP32 in Cirkit Designer

Cirkit runs your compiled sketch on an emulated ESP32, and its LED lights while GPIO 19 is high, as in the LED guide; the LED is tested on the classic ESP32, the ESP32-S3 and the Arduino Uno. Cirkit works with voltages, not current, so the LED is as bright at 3.3 V as at 5 V; a real one is dimmer at 3.3 V.

References