Mastering ESP32 Part 3: Your First ESP32 Project: Blinking an LED
Take a flashlight with a push switch. Closing the switch completes a loop that runs from the battery, through the bulb, and back to the battery, and the bulb lights. Opening the switch breaks the loop, and the bulb goes dark. A blinking LED on an ESP32 is the same loop. A pin on the chip plays the part of the switch, and the program opens and closes it on a schedule.
That pin is a GPIO, short for general-purpose input/output pin. When a sketch sets a GPIO as an output, the chip drives that pin to either 0V (written LOW) or 3.3V (written HIGH), and an LED wired from the pin to ground follows it. Writing the sketch takes a few lines. Most of this lesson goes to three questions around it: why the LED needs a resistor, which resistor value to use, and which pin to wire it to. The ESP32's 3.3V pins and the varied LEDs on its boards change the usual Arduino answers to all three.
Part 2, Installing and Configuring ESP32 Support in Arduino IDE, proved that the toolchain, the upload path, and the serial link work, and it deliberately avoided wiring anything. This part adds the first physical output. Later lessons that light a status LED, switch a relay, or flag a sensor reading build on the same two steps shown here: configure a pin as an output, then write a level to it.
This lesson covers the two functions every sketch needs, setup() and loop(), then wires one red LED to GPIO 27 through a 220Ω resistor, works out that resistor value from a datasheet, uploads the sketch, and runs the same circuit in the Electronics Lab. It also explains why the board's own LED is not used, and which pins to leave alone for now. Everything runs at 3.3V and needs only USB power. Reading buttons and the boot behavior of certain pins belongs to Part 4, dimming an LED belongs to Part 12, and timing without delay() belongs to Part 13.
Bill of Materials
| Component | Description | Buy on Amazon | Buy on Temu | Buy on SparkFun | Buy on Seeed Studio | Datasheet |
|---|---|---|---|---|---|---|
| ESP32 development board | Original ESP32 (ESP32-WROOM-32) on a 30-pin DevKit-style board. Not an S2, S3, C3 or C6 board. | Amazon Link | Not yet available | Not yet available | Not yet available | ESP32 datasheet |
| 5mm red LED | One red LED from a 5mm pack. The numbers in this lesson use a red LED, so pick a red one from a mixed pack. | Amazon Link | Temu Link | SparkFun Link | Not yet available | Vishay TLHR5400 datasheet |
| 220Ω resistor | One resistor. A resistor kit that includes 220Ω covers it. | Amazon Link | Temu Link | SparkFun Link | Seeed Link | N/A |
| Breadboard and jumper wires | Two jumper wires are enough for this circuit. | Amazon Link | Temu Link | SparkFun Link | Seeed Link | N/A |
| Micro-USB data cable | A micro-USB cable for the board's USB port. It must carry data, not only power. | Amazon Link | Temu Link | SparkFun Link | N/A | N/A |
The products linked above may contain affiliate links. The Makers Workbench earns from qualifying purchases when these links are used.
A mixed 5mm LED pack contains several colors, and each color has its own forward voltage, which the resistor calculation in this lesson depends on. The figures here describe a red LED, so use a red one. Before using another color on a 3.3V pin, find its forward voltage in a datasheet and repeat the calculation.
Series Baseline
This series uses a classic ESP32-WROOM-32 module on a 30-pin DevKit-style board, programmed with Arduino IDE 2. The latest stable Arduino-ESP32 core on November 23, 2026 is 4.0.0, based on ESP-IDF 6.1.1 and published on November 19, 2026.[1] Part 2 was written when 3.3.12 was current. For this lesson the pin definitions were read from the 4.0.0 source, and the sketches were compiled with core 3.3.9 using arduino-cli. They call only pinMode, digitalWrite, delay, and Serial, with no board-specific code. If a newer stable core exists when you read this, use the stable one that Boards Manager offers.
All logic on this board runs at 3.3V. The pins are not 5V tolerant: the datasheet allows an input to reach 0.3V above the pin's supply, which is 3.6V on a 3.3V supply.[2] The header pin marked 5V belongs to the board's power supply, and nothing in this lesson connects to it.
What setup() and loop() Do
An Arduino sketch is built from two functions that you write and the core calls for you. A function is a named block of instructions. The core calls setup() once, right after power-up or reset, and it calls loop() over and over for as long as the board has power. When loop() reaches its last line, the core calls it again immediately.
That arrangement gives each kind of instruction a natural home. Anything that has to happen once, such as choosing what a pin does, goes in setup(). Anything that has to keep happening, such as turning an LED on and off, goes in loop().

Three functions from the Arduino-ESP32 core do the work in this lesson.[3]
pinMode(pin, mode)chooses what a pin does. WithOUTPUTas the mode, the chip can drive the pin to 0V or 3.3V.digitalWrite(pin, value)sets the pin toHIGH(3.3V) orLOW(0V). It only has an effect on a pin thatpinModehas already set toOUTPUT.delay(milliseconds)pauses the program. A millisecond is one thousandth of a second, sodelay(1000)waits one second.
The delay() call stops the whole program while it waits. Nothing else can run during that time, which is acceptable for a lone blinking LED and becomes a limit in a bigger project. Part 13 shows how to wait without stopping, using millis().
How an LED Behaves
An LED, short for light-emitting diode, is a part that lets current through in one direction only and gives off light while it does. The two legs have names. Current enters at the anode and leaves at the cathode. On a typical 5mm LED the anode is the longer leg, and the cathode sits on the side of the plastic rim that has a flat edge. A new LED has legs of different lengths unless they have been trimmed, so check before trimming them.
A bulb has enough resistance of its own that the current through it settles at whatever the supply voltage allows. An LED behaves differently once the voltage across it reaches its forward voltage, the voltage at which it starts to conduct and glow. Past that point, a small further rise in voltage produces a large rise in current. If a 3.3V pin were connected straight across an LED, nothing would limit the current except the pin and the LED themselves, and either the LED would draw more than it is rated for or the pin would be asked for more than it can supply.
A resistor in series fixes that. It takes up the difference between the supply voltage and the LED's forward voltage, and in doing so it sets the current. Resistance is measured in ohms (Ω), and one ohm is one volt per amp.[4] The voltage across a resistor equals the current through it times its resistance, which means the current is the voltage divided by the resistance. The next section applies that to the loop, using numbers from a datasheet.
Choosing the Resistor
Two numbers come from the LED's datasheet, and they are the only ones needed. For the Vishay TLHR5400, a common red 5mm LED, the datasheet lists a forward voltage of 2V typical and 3V maximum when the current is 20mA (milliamps, or thousandths of an amp). It also lists 30mA as the highest continuous forward current at 65°C or below.[5] The forward voltage is stated at that 20mA test current, so for the smaller current used here, treat 2V as a working estimate and not an exact value.
The loop has three parts in series: the pin at 3.3V, the resistor, and the LED. The voltage across the resistor is what remains after the LED takes its share:
I = (Vpin − VF) / R
where I is the current in amps, Vpin is the voltage the pin delivers, VF is the LED's forward voltage, and R is the resistance in ohms. With a 220Ω resistor, a 3.3V pin, and a 2V forward voltage:
I = (3.3V − 2V) / 220Ω = 1.3V / 220Ω = 0.0059A
That is 5.9mA, about 6 thousandths of an amp. I chose 220Ω because it is in the usual resistor kit and keeps the arithmetic short. The current is well below the 20mA test current and the 30mA maximum, and the LED is clearly visible at that level.
Two checks show how much room the choice has. If the LED's forward voltage turned out to be the 3V maximum, the current would be (3.3V − 3V) / 220Ω, about 1.4mA, and the LED would look dim. If a pin delivered only 2.64V, which is the guaranteed minimum for a high output (0.8 times 3.3V, in the datasheet's DC characteristics table),[2] the current would be (2.64V − 2V) / 220Ω, about 2.9mA. The datasheet gives that minimum for a pin supplying up to 40mA, so a pin supplying 6mA should do better. Those cases bracket the calculated current between about 1.4mA and 5.9mA, so brightness will differ between LEDs and boards, and the resistor stays safe in all of them.
The check at the other end matters more, because a resistor that is too small can damage parts. Running the LED at its 20mA test current would need (3.3V − 2V) / 0.020A, which is 65Ω. A 220Ω resistor is more than three times that, so it keeps a wide margin below the limit. Do not skip the resistor or substitute a much smaller one.
The LED in your pack may not be a Vishay part. Mixed packs rarely name the maker, so the figures above are a worked example for a typical red LED and not a measurement of your LED. If you want an exact number for a particular LED, find its datasheet and repeat the calculation with its forward voltage.
How Much a Pin Can Supply
Three separate current numbers appear in the datasheet for output pins, and none of them is the LED current. They answer different questions, so keep them apart.

- Default drive strength. Each output has a configurable drive strength, and the default setting is about 20mA.[2]
- Typical source current. For pins in the CPU and RTC power domains, which include GPIO 27, the datasheet lists a typical high-level source current of 40mA with the drive strength set to maximum, as long as the output stays at or above 2.64V. The minimum and maximum columns are blank, and the figure falls toward about 29mA per pin as more pins source current at once.[2]
- Cumulative limit. The absolute maximum ratings give 1200mA as the total output current for all IO pins together. There is no per-pin absolute maximum in that table.[2]
The LED circuit here draws about 6mA, which is far below the first two numbers. Do not treat the 40mA figure as a safe operating current for one pin. It is a typical value measured under stated conditions.
Why Not the Built-In LED
Most blink tutorials skip the wiring and use LED_BUILTIN, a name that the Arduino core replaces with the pin number of the board's own LED. On the ESP32 that approach fails for three reasons.
First, the LED you see lit on the board may not be controllable at all. Espressif's own ESP32-DevKitC V4 documents a single LED, the "5V Power On LED," which lights whenever USB or an external 5V supply is connected.[6] The guide lists no other LED, so on that board no sketch controls a lamp. A reader who watches that LED stay on while the sketch runs may conclude that the upload failed.
Second, the board entry chosen in Part 2 has no LED_BUILTIN. In core 4.0.0 the ESP32 Dev Module entry uses a pin definitions file that defines the SPI pins and nothing named LED_BUILTIN.[7] The Blink example in File > Examples > 01.Basics uses that name, so with ESP32 Dev Module selected the compiler reports that LED_BUILTIN was not declared.
Third, the entries that do define it describe particular boards. The DOIT ESP32 DEVKIT V1 and NodeMCU-32S entries both set LED_BUILTIN to 2,[7] which points to an LED on GPIO 2 for boards of those designs. A board from another maker may wire its lamp somewhere else or not have one. GPIO 2 is also one of the pins the chip reads at reset to decide how to boot.[2] Part 4 explains that behavior. For a first project it is better to use a pin without that side effect, and to build the circuit yourself so you know exactly where the LED is connected.
An LED you wire yourself avoids all three problems. This series uses GPIO 27 for it. The datasheet lists GPIO 27 as an ordinary input/output pin, it is not one of the boot-configuration pins, and it is not one of the input-only pins 34 to 39.[2]
Wire the Circuit
The circuit has three connections. The resistor and the LED form a single chain from GPIO 27 to ground.

| From | To | Why |
|---|---|---|
| GPIO 27 | One leg of the 220Ω resistor | The pin supplies the voltage, and the resistor limits the current |
| Other leg of the 220Ω resistor | LED anode (longer leg) | Current enters the LED here |
| LED cathode (shorter leg) | Any GND pin on the ESP32 board | Completes the loop back to ground |
Disconnect the USB cable before wiring, and reconnect it when the circuit is finished.
- Find GPIO 27 on your board. Silkscreen text varies between makers, so look for the number 27 next to a header pin and confirm it against the pinout diagram for your exact board. The code refers to the GPIO number. The position of the pin on the header does not matter to the sketch.
- Insert the resistor into the breadboard so its legs sit in two different rows. In a breadboard, the five holes of one short row are connected to each other, and neighboring rows are not. A resistor across two rows gives each leg its own row.
- Insert the LED so the anode (longer leg) shares a row with the second resistor leg and the cathode has a row of its own.
- Run a jumper wire from GPIO 27 to the row holding the first resistor leg.
- Run a second jumper wire from the cathode row to a GND pin.
If the board sits in the breadboard, build the circuit in free columns beside it. If it does not fit, place the board beside the breadboard and use jumper wires for both connections.
Test the LED Before Using the Sketch
An LED that is backwards, a resistor in the wrong row, and a bad jumper wire all produce the same symptom: nothing lights. Separate them before you upload anything. Move the jumper wire from GPIO 27 to the 3V3 pin, with USB connected. The pin marked 3V3 is the board's 3.3V supply, and the LED should light and stay lit. Move the wire back to GPIO 27 when it does.
If the LED does not light on 3V3, the fault is in the circuit and not in the sketch. Turn the LED around and try again. Reversing an LED at 3.3V does not harm it, because the datasheet's maximum reverse voltage is 6V.[5]
The Sketch
The sketch below does what the flashlight did: it closes the loop for one second, then opens it for one second, and repeats.
// Part 3: blink an external LED wired to GPIO 27 through a 220 ohm resistor.
const int kLedPin = 27; // The GPIO number the resistor and LED are wired to. const means the value never changes.
void setup() {
pinMode(kLedPin, OUTPUT); // Make GPIO 27 an output so the sketch can drive it to 0V or 3.3V.
}
void loop() {
digitalWrite(kLedPin, HIGH); // Drive the pin to 3.3V. Current flows through the resistor and LED, and the LED lights.
delay(1000); // Wait 1000 milliseconds (one second) with the LED on.
digitalWrite(kLedPin, LOW); // Drive the pin to 0V. No current flows, and the LED goes dark.
delay(1000); // Wait one second with the LED off, then loop() starts again.
}
The pin number sits in a named constant at the top instead of being typed into every call. Say you wire the LED to a different pin later. If the number is named once, you change one line. If you typed 27 into all three calls, you would have to find and change every one of them. The k prefix is a common habit for constants and has no special meaning to the compiler.
Select ESP32 Dev Module and the port that follows the board, as in Part 2, and choose Upload. The first compile can take longer than later ones. When the Output panel reports that the upload is done, the board restarts and the LED begins to blink, on for one second and off for one second. If the LED does not blink after the restart, press the EN button once, which resets the chip and starts the sketch from the beginning.
A lamp on the board itself will have been lit all along, before the upload, during it, and after it. On many boards that is the power LED described earlier, and it stays on because it is connected to the supply. The LED you wired is the one that blinks.

Try the Blink in the Lab
The Lab runs the sketch above on a simulated ESP32 with one LED and one 220Ω resistor wired to GPIO 27, so you can watch the blink without building the circuit. It uses Espressif's DevKitC V4 board model because the simulator has no 30-pin board. The sketch and the GPIO number are the same on both boards, and only the position of the pins on the board differs.
The Lab circuit is wired like this:
| From | To | Why |
|---|---|---|
| GPIO 27 | 220Ω resistor, first leg | The pin supplies the voltage, and the resistor limits the current |
| 220Ω resistor, second leg | LED anode (A) | Current enters the LED here |
| LED cathode (C) | GND | Completes the loop back to ground |
To use the Lab:
- Select Run. The first run compiles the sketch, so it takes a few seconds before anything moves.
- Watch the LED switch on and off once per second.
After the sketch starts, the LED should light for one second and go dark for one second, repeating until you stop the simulation. The Lab simulates the circuit and the sketch. It does not show the lamps that are printed on a real board, and the brightness of the simulated LED is not a measurement of a physical one.
What Changes When You Edit the Sketch
Small edits show what each line controls, and each one has a result you can predict before you upload.
Change the timing. Replace delay(1000) after HIGH with delay(200) and leave the other delay at 1000. The LED is now on for a fifth of a second and off for a full second.
Print the state. The second sketch repeats the blink with named timings and adds two lines that report each change to the Serial Monitor at the 115200 baud rate from Part 2.
// Part 3, variation: blink with named timings and report each state over serial.
const int kLedPin = 27; // GPIO 27, wired as before.
const unsigned long kOnMs = 200; // How long the LED stays on, in milliseconds. unsigned long is a whole number that cannot be negative.
const unsigned long kOffMs = 800; // How long the LED stays off, in milliseconds.
void setup() {
Serial.begin(115200); // Start the serial link at 115200 baud, matching the Serial Monitor setting.
pinMode(kLedPin, OUTPUT); // Make GPIO 27 an output.
}
void loop() {
digitalWrite(kLedPin, HIGH); // LED on.
Serial.println("LED on"); // Send the text LED on, followed by a new line, to the Serial Monitor.
delay(kOnMs); // Hold the LED on for kOnMs milliseconds.
digitalWrite(kLedPin, LOW); // LED off.
Serial.println("LED off"); // Report the new state.
delay(kOffMs); // Hold the LED off for kOffMs milliseconds.
}
With these numbers, the LED is on for 200 milliseconds and off for 800 milliseconds, so one full cycle takes one second. The Serial Monitor shows the two lines alternating. Part 8 covers variable types such as unsigned long in full. For now, treat each named value as a label for a number.
Move the LED to another pin. Choose another ordinary output pin, move the jumper wire, and change kLedPin. Part 4 lists which pins are safe. Until then, GPIO 27 is the one this series uses.
Where the Pins Get Complicated
So far the circuit is wired, the value of the resistor comes from a datasheet, and the sketch drives one pin. The pin number was chosen with care, and the reasons form the last piece of this lesson, because the next several parts depend on it.
Not every GPIO on the chip is an ordinary output. The datasheet sets some apart.
- Strapping pins. At reset, the chip reads the voltage on GPIO 0, GPIO 2, GPIO 12 (also named MTDI), GPIO 15 (MTDO), and GPIO 5, and uses it to choose boot settings.[2] A wire on one of those pins can change how the board starts. Part 4 shows what each one does.
- Input-only pins. GPIO 34 to 39 have no output driver, so
digitalWritecannot drive them.[2] - The official example. The Arduino-ESP32 GPIO page shows an example that sets the LED on GPIO 12 and a button on GPIO 2.[3] Both are strapping pins.[2] Do not copy that example into a first project.
GPIO 27 passes all three tests. It is also free of the default roles that later parts use: the Arduino-ESP32 pin definitions for ESP32 Dev Module give it only an analog-input name (A17) and a touch-sensor name (T7), and they assign the default SPI pins (SCK 18, MOSI 23, MISO 19, SS 5) to other pins.[7] The LED can stay on GPIO 27 when the series reaches SPI.
Exercises
- Current with a different resistor. Use the same 3.3V pin and the same 2V estimate for the LED. What current flows with a 330Ω resistor? Answer: (3.3V − 2V) / 330Ω = 3.9mA, so the LED is dimmer than with 220Ω.
- The smallest sensible resistor. What resistor gives 10mA, which is half the datasheet's 20mA test current? Answer: 1.3V / 0.010A = 130Ω. Choose the next larger value in your kit.
- A double blink. Change
loop()so the LED flashes twice quickly (200 milliseconds on, 200 milliseconds off, twice) and then stays off for one second before repeating. Hint: the loop body needs fourdigitalWritecalls (on, off, on, off), each followed by adelay, and the last delay is the long one. Upload it and watch the pattern.
Troubleshooting
| Symptom | Most likely area | First action |
|---|---|---|
| The LED does not light at all | LED backwards, resistor in the wrong row, or a loose wire | Move the jumper to 3V3. If the LED lights there, check the GPIO 27 wire and the pin number |
| The LED lights but never turns off | The jumper is still on 3V3, or the board runs an older sketch | Move the jumper back to GPIO 27, then upload again and watch the Output panel for the completed message |
| A lamp on the board is on but the LED you wired never changes | Board lamp confused with the circuit LED | A power LED stays on. Watch the LED you wired |
| LED_BUILTIN was not declared | The Blink example was opened with ESP32 Dev Module selected | Use the sketch in this lesson, which names GPIO 27 directly |
| The LED is very dim | An LED of another color, or a resistor larger than 220Ω | Use a red LED and confirm the resistor value |
| Upload fails before writing | Port, cable, or bootloader entry | Follow the checks in Part 2 |
The LED never lights, even on 3V3
Check the polarity first by turning the LED around. Confirm that the resistor and the LED share a row, and that the cathode leg has its own row leading to GND. Look for a jumper wire pushed only partway into a hole. If the circuit looks right and the LED still stays dark on 3V3, try a second LED from the pack, since a pack can contain a dead one.
The LED lights on 3V3 but not on GPIO 27
The wire is on the wrong pin, the sketch is not the one running, or the pin number in the code does not match the wire. Count the header position against a pinout diagram for your exact board. If the board has more than one marking for the same pin, the one labeled with a GPIO number is the one that matches the code.
The sketch will not compile
Read the first error, not the last line. An error about LED_BUILTIN means the Blink example is open. An error about a missing semicolon or an unknown name means a typo in the pasted sketch. Compare it with the code above character by character.
The LED blinks, but not at the expected rate
The numbers inside delay() are in milliseconds. A value of 1000 is one second, and a value of 100 is a tenth of a second. If the LED flickers too fast to see, the delay is shorter than intended.
Where This Fits in the Series
Part 1, Getting Started with the ESP32: A Complete Beginner's Guide, identifies the board and its electrical limits. Part 2 installs the programming support and proves the upload path. This part adds the first output. Part 4 adds the first input, a button, and explains the pins that are not ordinary GPIOs. Part 12 dims the same LED with PWM, and Part 13 replaces delay() with millis() so the board can do more than one thing at a time.
References
- Espressif Systems, “Arduino release 4.0.0 based on ESP-IDF v6.1.1,” arduino-esp32 releases, GitHub, published November 19, 2026. https://github.com/espressif/arduino-esp32/releases/tag/4.0.0 Accessed November 23, 2026.
- Espressif Systems, “ESP32 Series Datasheet,” version 5.3, July 2026, Table 2-1, Table 3-1, Table 5-1, Table 5-3, and the IO_MUX appendix, espressif.com. https://documentation.espressif.com/esp32_datasheet_en.pdf Accessed November 23, 2026.
- Espressif Systems, “GPIO,” Arduino ESP32 documentation, docs.espressif.com. https://docs.espressif.com/projects/arduino-esp32/en/latest/api/gpio.html Accessed November 23, 2026.
- OpenStax, “9.3 Resistivity and Resistance,” University Physics Volume 2, openstax.org. https://openstax.org/books/university-physics-volume-2/pages/9-3-resistivity-and-resistance Accessed November 23, 2026.
- Vishay Semiconductors, “TLHR540., TLHY540. High Efficiency LED, 5mm Tinted Diffused Package,” document 83012, Rev. 2.7, January 25, 2024, vishay.com. https://www.vishay.com/docs/83012/tlhr540x_tlhy540x.pdf Accessed November 23, 2026.
- Espressif Systems, “ESP32-DevKitC V4,” esp-dev-kits documentation (user guide, Functional Description), docs.espressif.com. https://docs.espressif.com/projects/esp-dev-kits/en/latest/esp32/esp32-devkitc/user_guide.html Accessed November 23, 2026.
- Espressif Systems, “arduino-esp32 source at tag 4.0.0,” boards.txt and variants esp32, doitESP32devkitV1 and nodemcu-32s (pins_arduino.h), GitHub. https://github.com/espressif/arduino-esp32/tree/4.0.0 Accessed November 23, 2026.
Wrap-Up and What's Next
A GPIO set as an output switches between 0V and 3.3V, and an LED in series with a resistor turns that into light. The resistor sets the current: with a red LED at about 2V, a 3.3V pin, and 220Ω, the current is about 5.9mA, well under what the LED and the pin can handle. The sketch has one pin number, one pinMode call in setup(), and a digitalWrite and delay pair for each state in loop(). The board's own lamp is a power indicator on many boards and is not used here, and GPIO 27 was chosen because it has no boot-time role.
In Part 4, we'll read a button on a second pin and learn which pins have restrictions.
Hack The World and Make Awesome.
