Skip to content

How to Hook Up an LED with a Resistor to Control It Using an ESP8266

Overview

This guide explains how to connect a standard LED to an ESP8266 microcontroller with the proper current-limiting resistor so you can turn it on and off via GPIO pins. We’ll cover:

  • Parts required
  • Choosing the correct resistor value
  • Wiring diagram and instructions
  • Example code to control the LED

Parts You Will Need

  1. ESP8266 board (e.g., NodeMCU, Wemos D1 Mini, ESP-12E)
  2. LED – 5 mm or 3 mm standard red LED (forward voltage ~2 V, forward current ~20 mA)
  3. Resistor – 220 Ω (¼ W) or 330 Ω (¼ W)
  4. Breadboard
  5. Jumper wires (male-male)
  6. USB cable to program and power the ESP8266

Why You Need a Resistor

LEDs need limited current, or they will burn out. The ESP8266 GPIO pins provide 3.3 V at a maximum safe current of ~12 mA per pin. To calculate the resistor value:

\[ R = \frac{V_{supply} - V_{LED}}{I_{LED}} \]

Where:

  • \(V_{supply} = 3.3 V\)
  • \(V_{LED}\) (red LED) ≈ 2 V
  • \(I_{LED}\) ≈ 10 mA for safety
\[ R = \frac{3.3 - 2}{0.010} = 130 Ω \]

We choose the next higher standard value to limit current and protect both LED and ESP8266: 220 Ω or 330 Ω.


Wiring Instructions

We’ll use GPIO5 (D1) as the control pin in this example.

Step-by-step wiring:

  1. Insert LED into breadboard – The longer leg is the anode (+), shorter leg is cathode (–).
  2. Connect resistor to anode leg of the LED.
  3. Connect other side of the resistor to ESP8266 GPIO5 (D1).
  4. Connect cathode leg of LED to GND on ESP8266.
  5. Double-check polarity: Reversing LED legs will prevent it from lighting.

Wiring Diagram

ESP8266 (D1/GPIO5) ----[220Ω resistor]---->|---- GND
                                           LED
  • The arrow in LED symbol points from anode (+) to cathode (–).
  • The resistor is in series with the LED.

Example Arduino Code

#define LED_PIN D1  // GPIO5

void setup() {
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  digitalWrite(LED_PIN, HIGH); // LED ON
  delay(1000);                 // wait 1 second
  digitalWrite(LED_PIN, LOW);  // LED OFF
  delay(1000);                 // wait 1 second
}

Important Notes

  • Resistor specifics: 220 Ω (preferred for brightness) or 330 Ω (for longer LED life), ¼ W rating.
  • LED specifics: Standard red LED, forward voltage ~2 V, forward current ~20 mA.
  • ESP8266 pin voltage: 3.3 V logic, do not connect LED directly without resistor.
  • GND is shared: Make sure the LED cathode is connected to ESP8266 GND.