LED

A Light Emitting Diode (LED) is a semiconductor device that emits light when an electric current passes through it. Unlike traditional bulbs, LEDs are energy-efficient, long-lasting, and available in various colors. They operate with low voltage and current, typically in one direction, and produce minimal heat. LEDs are used in displays, indicators, lighting, and electronics due to their reliability and compact size.

Assortment of different colored through-hole LEDs (5mm)
Fig.: Assortment of different colored through-hole LEDs (5mm)
Electronic symbol of a LED
Fig.: Electronic symbol of a LED

Since there are many types of LEDs with different sizes, colors, power ratings, operating voltages, etc., we will focus in this article on the most commonly used LEDs in model building and hobby applications - specifically, the 3mm and 5mm wired LEDs (TH = through-hole).
These types of LEDs typically have a forward voltage of 2–3V (depending on the color or the material used) and a relatively low current consumption of about 1mA to 20mA. They are mostly used as indicator lights or for low-power lighting applications.

Applications

Features/Specifications

Typical attributes of LEDs

Semiconductor material Wavelength λ Color UD
GaAs >760 infrared 1.2V
AlGaAs, GaAsP, AlGaInP, GaP 610–760 nm red 1.6V – 2.1V
GaAsP, AlGaInP, GaP 590–610 nm amber/orange 1.8V – 2.0V
GaAsP, AlGaInP, GaP 570–590 nm yellow 2.2V
InGaN/GaN, GaP, AlGaInP, AlGaP 500–570 nm green 2.1V – 3.5V
ZnSe, InGaN, SiC 450–500 nm blue 2.2V – 3.6V
InGaN 450–450 nm violet 3.6V
AlN, AlGaN, AlGaInN, BN 450–450 nm ultraviolet 3.8V
InGaN 450 nm white 3.6V – 4.0V

📄 LED (5mm) datasheet (164 kB)

Never operate an LED without a proper current-limiting series resistor, as this will usually result in immediate destruction of the LED. (see: Calculate LED series resistor)

Connections

Terminal diagram of a through hole LED
Fig.: Terminal diagram of a through hole LED
Name Description Identification
Anode (+) Positive terminal — connect to the positive voltage Longer leg
Cathode (-) Negative terminal — connect to ground Shorter leg, flat edge on LED body

Used Components

Setup & Programming

Internal LED

Arduino Uno & ESP32

Many microcontroller development boards (e.g., Arduino UNO) come with a pre-installed LED, usually soldered as an SMD component on the board. This can easily be controlled as an indicator through a specific pin. The following code is working for both Arduino UNO and ESP32:

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

void loop() {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    digitalWrite(LED_BUILTIN, LOW);
    delay(500);
}

External LED

Arduino Uno

Circuit diagram for connecting an LED with the Arduino Uno
Fig.: Circuit diagram for connecting an LED with the Arduino Uno
#define PIN_LED 2

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

void loop() {
    digitalWrite(PIN_LED, HIGH);
    delay(500);
    digitalWrite(PIN_LED, LOW);
    delay(500);
}

ESP32

Circuit diagram for connecting an LED with the ESP32
Fig.: Circuit diagram for connecting an LED with the ESP32
#define PIN_LED 22

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

void loop() {
    digitalWrite(PIN_LED, HIGH);
    delay(500);
    digitalWrite(PIN_LED, LOW);
    delay(500);
}

LED dimming/PWM

Hardware PWM (Pulse Width Modulation) on Arduino and ESP32 is a method where a microcontroller's built-in timers generate precise, automatic high-speed on/off signals to simulate analog output on certain pre-defined pins.

MCU/Board PWM-capable pins
Arduino UNO 3, 5, 6, 9, 10, 11
ESP32 GPIO 0–19, 21–23, 25–27, 32–33 (most of them)

Arduino Uno & ESP32

The circuit diagram is the same for each microcontroller like for the external LED.

#define PIN_LED 9

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

void loop() {
    static int brightness = 0;
    static int offset = 5;

    analogWrite(PIN_LED, brightness);

    brightness = brightness + offset;
    if (brightness <= 0 || brightness >= 255) {
        offset = -offset;
    }

    delay(25);
}

Charlieplexing

Charlieplexing is a technique for controlling many LEDs with fewer microcontroller pins by exploiting tri-state logic. It allows LEDs to be wired in a matrix without traditional multiplexing, using the ability of each pin to act as INPUT (high-impedance), HIGH, or LOW. This enables control of n(n–1) LEDs with n pins, saving hardware and space. In this example we're using 3 pins to get the control of 6 LEDs.

Generic circuit diagram for connecting 6 LEDs to 3 digital pins of a microcontroller using Charlieplexing.
Fig.: Generic circuit diagram for connecting 6 LEDs to 3 digital pins of a microcontroller using Charlieplexing.

How does it work: An LED is connected between two pins – but only in one direction. Depending on the polarity, only the LED in that direction will light up. All other pins are set to a high-impedance state to prevent unwanted current flow.

Switching table for 6 LEDs

LED Pin X1 Pin X2 Pin X3
L1 HIGH LOW INPUT
L2 LOW HIGH INPUT
L3 INPUT HIGH LOW
L4 INPUT LOW HIGH
L5 HIGH INPUT LOW
L6 LOW INPUT HIGH
#define PIN_X1 2
#define PIN_X2 3
#define PIN_X3 4

void setup() {
    resetPins();
}

void loop() {
    // light one LED after each other
    for (byte i=1; i<=6; i++) {
        lightLed(i);
        delay(500);
    }
}

void lightLed(byte ledIndex) {
    switch(ledIndex) {
        case 1:
            setPins(PIN_X1, PIN_X2);
            break;
        case 2:
            setPins(PIN_X2, PIN_X1);
            break;
        case 3:
            setPins(PIN_X2, PIN_X3);
            break;
        case 4:
            setPins(PIN_X3, PIN_X2);
            break;
        case 5:
            setPins(PIN_X1, PIN_X3);
            break;
        case 6:
            setPins(PIN_X3, PIN_X1);
            break;
    }
}

void setPins(byte highPin, byte lowPin) {
    resetPins();
    pinMode(highPin, OUTPUT);
    pinMode(lowPin,  OUTPUT);
    digitalWrite(highPin, HIGH);
    digitalWrite(lowPin,  LOW);
}

void resetPins() {
    pinMode(PIN_X1, INPUT);
    pinMode(PIN_X2, INPUT);
    pinMode(PIN_X3, INPUT);
    digitalWrite(PIN_X1, LOW);
    digitalWrite(PIN_X2, LOW);
    digitalWrite(PIN_X3, LOW);
}

Disadvantages/Limitations of Charlieplexing

Further information

Last edited by Christian Grieger on 2025-05-12
X
  1. [top]
  2. Applications
  3. Features/Specifications
  4. Connections
  5. Used Components
  6. Setup & Programming
  7. Further information