Home / Learn / Electronics basics

How LEDs work, and how to wire them up

New to this? Read from the top. Already know the basics? Jump to Going deeper for intermediate and advanced detail.

A red LED glowing, with its longer anode leg and shorter cathode leg, wired through a resistor to a battery
Beginner

LEDs are usually the first thing anyone lights up in electronics, and the first thing many people accidentally burn out. This guide explains what's going on inside, and how to wire them so they last.

What an LED is

LED stands for light emitting diode. A diode is a part that lets current flow in one direction only. In an LED, as current flows, the energy released inside the semiconductor comes out as light. The material the LED is made from sets its colour.

Because it's a diode, an LED only works one way round. Connect it backwards and nothing happens. A small reverse voltage won't usually harm it, but LEDs can only withstand around 5 V in reverse, so don't rely on that.

Which leg is which

  • Anode (+): the longer leg. It connects towards the positive supply.
  • Cathode (−): the shorter leg. Most round LEDs also have a flat edge on the rim on this side.

If the legs have been trimmed, look inside a clear LED: the larger piece of metal is usually the cathode. A multimeter's diode test setting will also make an LED glow faintly when it's the right way round.

Forward voltage and current

An LED barely conducts until the voltage across it reaches its forward voltage, then it conducts very easily. That voltage depends on the colour:

ColourTypical forward voltage
Red1.8 to 2.2 V
Orange, yellow, yellow-green2.0 to 2.2 V
Bright green, blue, white3.0 to 3.4 V

Small indicator LEDs are usually rated for about 20 mA, and look plenty bright at 5 to 15 mA. Running them gently also makes them last longer.

Why an LED needs a resistor

Once an LED is past its forward voltage, a tiny increase in voltage causes a huge increase in current. Connect one straight to 5 V and the current shoots up until the LED overheats and dies, sometimes in a fraction of a second. A resistor in series limits the current to a safe value.

R = (supply voltage − LED forward voltage) ÷ current

For a red LED on 5 V at 15 mA: (5 − 2) ÷ 0.015 = 200 Ω, so use the next value up, 220 Ω. The LED resistor calculator does this for you, including the power rating.

The resistor can go on either side of the LED. What matters is that it's in series, in the same path as the LED.

Lighting an LED from a microcontroller

A microcontroller pin can drive a small LED directly, through a resistor, but pins can only supply a limited current. Check your board's limits: for example, the Arduino Uno R4 is rated at 8 mA per pin, so pick a resistor that keeps the LED under that. On a 3.3 V board such as the Raspberry Pi Pico, a red LED with a 330 Ω resistor draws about (3.3 − 2) ÷ 330 = 4 mA, which is safe and still clearly visible.

For anything brighter, such as a strip or a power LED, use the pin to switch a transistor instead. See using a transistor as a switch.

Several LEDs

  • In series: the forward voltages add up, and the same current flows through them all, so one resistor is enough. Three red LEDs need about 6 V, plus some headroom for the resistor.
  • In parallel: give each LED its own resistor. No two LEDs are identical, and if they share one resistor, the one with the lowest forward voltage takes most of the current.

Dimming an LED

Turning the voltage down doesn't dim an LED smoothly, because of how sharply its current changes. Instead, microcontrollers use PWM (pulse width modulation): they switch the LED on and off hundreds or thousands of times a second. Your eye averages the flicker, so on for 25% of the time looks dim, and on for 100% looks fully bright. There's a PWM example in Python for electronics beginners.

Intermediate and advanced

Going deeper

The LED's current curve

An LED's current rises roughly exponentially with voltage once it starts conducting, which is why a small voltage change makes a big current change. The forward voltage also falls as the LED warms up, by a few millivolts per °C. If an LED is driven from a fixed voltage, warming increases the current, which warms it further. A series resistor or a constant-current driver breaks this cycle.

Constant-current drivers

For power LEDs (hundreds of milliamps and up), a resistor wastes too much energy. A constant-current driver adjusts its output voltage to hold the current steady regardless of the forward voltage or temperature. Switching drivers do this efficiently, and many accept a PWM or analogue input for dimming.

Brightness isn't linear

Our eyes respond roughly logarithmically to light, so a PWM duty of 50% looks much brighter than "half". For smooth fades, apply a curve, often called gamma correction:

def perceived_to_duty(level, gamma=2.2, max_duty=65535):
    """level from 0.0 to 1.0, returns a PWM duty value"""
    return round((level ** gamma) * max_duty)

print([perceived_to_duty(x / 4) for x in range(5)])
[0, 3104, 14263, 34802, 65535]

A gamma of about 2.2 is a common starting point. Adjust it by eye.

PWM frequency

A few hundred hertz is enough to avoid visible flicker when you look straight at an LED. Higher frequencies, from a few kilohertz up, help avoid banding on phone cameras and flicker you notice when moving your eyes. Very high frequencies increase switching losses in drivers, so a few kilohertz is a common compromise.

Addressable LEDs

Strips like WS2812-type "NeoPixels" put a tiny controller in each LED, so one data wire sets every colour. Each pixel can draw up to about 60 mA at full white (about 20 mA per colour), so 60 pixels at full white can draw roughly 3.6 A. Power long strips from both ends or several points, add a large capacitor across the supply, and use a resistor of a few hundred ohms in the data line. Most of these strips want 5 V data, so a 3.3 V board may need a level shifter.

Sources

The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.