Sub topic 6
Which way round an LED goes
This chapter assumes you know what voltage, current and resistance are at an introductory level, and that you have met pinMode() and digitalWrite() in earlier chapters. It covers one narrow topic: how to tell the two legs of an LED apart, why the orientation matters, and how to check a part when its markings are missing or misleading. Resistor sizing and breadboard wiring come in the next two chapters, so here we only note where the resistor fits.
An LED is a diode
LED stands for light-emitting diode. A diode is a component that lets current flow easily in one direction and blocks it in the other. An ordinary resistor works the same in both directions. A diode does not, so you cannot fit it either way round and expect the same result.
The two terminals have names:
- The anode is the positive terminal. Current enters the LED here.
- The cathode is the negative terminal. Current leaves the LED here.
Conventional current flows from anode to cathode. In a working circuit, the anode sits at the higher voltage and the cathode at the lower one.
Forward bias and reverse bias
Connecting the LED so current can flow is called forward bias. The anode is more positive than the cathode by at least the forward voltage, usually written Vf. Below Vf, almost no current flows and there is no light. Above it, current rises very steeply for small increases in voltage.
Connecting it the other way round is reverse bias. The LED blocks current and stays dark. A small leakage current exists, but it is far too small to see.
| Condition | Anode vs cathode | Current | Light |
|---|---|---|---|
| Forward bias, above Vf | Anode higher by at least Vf | Flows | On |
| Forward bias, below Vf | Anode higher, but by less than Vf | Almost none | Off or barely visible |
| Reverse bias | Cathode higher | Almost none (leakage) | Off |
Typical Vf values depend on the LED's colour, because colour is set by the semiconductor material. Red LEDs are usually around 1.8 to 2.2 V, green around 2.0 to 3.0 V, and blue and white around 3.0 to 3.4 V. Always prefer the figure in the datasheet for your part.
Why a wrong orientation usually does no harm, and when it can
For a standard 5 mm indicator LED at 5 V, plugging it in backwards generally leaves it dark and unharmed. LEDs do have a maximum reverse voltage rating, often only about 5 V for common types. A reverse voltage near or above that rating can damage the junction. An Arduino Uno's pins swing between 0 V and 5 V, so you are at the edge of that rating. Treat reverse connection as something to avoid, not something that is safe to test by trial and error.
Reading the physical markings
A new LED almost always gives you at least two clues. Use more than one before you trust any of them.
Leg length
On a new, uncut 5 mm LED, the longer leg is the anode and the shorter leg is the cathode. A useful memory aid: the anode has the "plus" and the longer lead, and the cathode is the "cut short" one.
This clue fails once the legs have been trimmed, or if the LED was recycled from other equipment. If you have clipped the legs to fit a breadboard, mark the anode with a note or a bend before you cut.
The flat edge on the body
Look at the rim of the plastic dome. Most 5 mm and 3 mm LEDs have a flat spot moulded into the base of the dome. The leg on the flat side is the cathode.
This clue is not perfectly reliable. Some manufacturers' flats are faint, and a few parts are moulded differently. When the flat and the leg length disagree, do not guess. Use the internal-structure check below or a meter.
Looking inside the dome
If the dome is clear, hold the LED up to the light and look at the two metal pieces inside.
- The larger piece, often shaped like a flag or an anvil, is the cathode.
- The smaller piece with a thin wire bonded to it is the anode.
This works on clear-lensed parts. It is harder on diffused (frosted) LEDs.
Surface-mount LEDs and onboard LEDs
Surface-mount LEDs have no legs, so manufacturers use other marks: a small dot, a green tint, a notch, a bevelled corner, or a T-shaped or arrow mark on the underside. The convention differs between makers, so check the datasheet for the exact part.
The LED on your Arduino board that LED_BUILTIN controls is already wired with its own resistor, so you never have to orient it. This is why the first blink sketch worked with no extra parts.
| Clue | Anode | Cathode | Reliability |
|---|---|---|---|
| Leg length (new part) | Longer leg | Shorter leg | Good, until legs are cut |
| Flat edge on the dome | Opposite side from the flat | Leg on the flat side | Fair, varies by maker |
| Inside the dome | Smaller electrode with wire | Larger electrode | Good on clear LEDs |
| Multimeter diode test | Red probe when it lights | Black probe when it lights | Best |
Testing an LED with a multimeter
A multimeter with a diode-test mode (the symbol looks like an arrow pointing at a bar) settles any doubt.
- Set the meter to diode test.
- Touch the red probe to one leg and the black probe to the other.
- If the LED glows faintly and the display shows a number, the red probe is on the anode and the black probe is on the cathode. The number is roughly the forward voltage in volts.
- If the display shows "OL" or a very large value and nothing glows, swap the probes and try again.
Some meters supply too little voltage to light a blue or white LED, whose Vf is higher than the test voltage. In that case the reading stays at "OL" in both directions even though the part is fine. Another way to test those parts is covered below.
Testing without a meter
You can use a 3 V coin cell. Touch the LED's legs to the battery's two faces. A forward-biased LED lights up. A backwards one stays dark. A coin cell has high internal resistance, which limits the current enough that most small LEDs survive the test. Do not do this with a larger battery or a power supply, because without a resistor the current is limited only by the source, and the LED can burn out in an instant.
Where orientation fits in the circuit
The Arduino drives the LED from a digital pin, with a resistor in series. Series means the current passes through each part in turn. There are two ways to arrange it.
Pin to anode (current sourcing)
The pin goes through the resistor to the anode, and the cathode goes to GND. When the pin is HIGH, it sits at about 5 V (or 3.3 V on 3.3 V boards), current flows from the pin through the LED to ground, and the LED lights. This is the arrangement the rest of this tutorial uses.
5 V to anode (current sinking)
The anode is connected through the resistor to the supply voltage, and the cathode goes to the pin. Now the LED lights when the pin is LOW, because that creates the voltage difference. HIGH turns it off. The logic is inverted compared with the sourcing wiring.
In both, the resistor can sit on either side of the LED. Current is the same everywhere in a series path, so the order of resistor and LED does not change the brightness. Conventionally the resistor sits next to the supply side, but placing it next to the ground side works equally well.
What the sketch sees
The code does not know which way the LED is installed. It only sets a pin level. Take the pin-8 sketch from the topic and keep it unchanged:
const uint8_t LED_PIN = 8;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(500);
digitalWrite(LED_PIN, LOW);
delay(500);
}With the sourcing wiring (pin, resistor, anode, cathode, GND), the LED is on for 500 ms and off for 500 ms. If you reverse the LED in that circuit, the sketch runs exactly as before, but the LED never lights. The Serial Monitor, the IDE and the upload all show success. Orientation is a hardware property, so nothing in the software reports it.
With the sinking wiring, the same sketch gives an LED that is off for 500 ms and on for 500 ms. The blink is the same visible pattern, but the phases are swapped. If you ever see a blink that seems to be inverted, for example the LED is on when you expected it off, check the wiring style before editing the code.
Common orientation mistakes
- Trusting leg length on a trimmed part. Both legs are now the same length, and you swap them. Check the flat edge or use a meter.
- Reading the flat edge from the wrong angle. Rotate the LED so you are viewing the base of the dome from the side. The flat sits on the rim, not on the top.
- Assuming the onboard LED rules apply to external parts. The onboard LED has a built-in resistor and fixed orientation. An external one needs both a resistor and the right polarity.
- Placing the LED across the breadboard's centre channel the wrong way. Each leg must be in a different, electrically separate row. Placing both legs in the same row of five connected holes shorts the LED out.
- Mixing up the wiring style and the logic. If the LED is on when the pin is
LOW, you have the sinking arrangement. That is not a software bug. - Using a mismatched Vf and supply. A blue LED with Vf near 3.2 V on a 3.3 V pin leaves almost no headroom for a resistor. It may light dimly or inconsistently. This is a voltage problem, not an orientation problem, but it looks similar from the outside.
Quick verification routine
Before you power a new LED circuit, run through this short sequence:
- Identify the anode using the longer leg and the flat edge. If they disagree, use the meter.
- Confirm the anode side of the LED faces the pin (sourcing) or the supply (sinking).
- Confirm the cathode side faces GND (sourcing) or the pin (sinking).
- Check that a resistor is somewhere in the series path.
- Upload the sketch and observe.
If the LED stays dark, swap the two legs once. Reverse-biasing a 5 V pin for a short time is generally survivable, so one swap is a reasonable last step. If it still does not light, the cause lies elsewhere, and the troubleshooting chapter has the full checklist.
Exercise
You wire an external LED so that pin 8 connects through a resistor to the LED's cathode, and the anode goes to GND. You upload the blink sketch with digitalWrite(LED_PIN, HIGH) and LOW alternating every 500 ms. The upload succeeds, but the LED never lights. What is wrong, and what are two ways to confirm it before changing anything?
Show answer
The LED is reverse biased: the cathode is on the higher-voltage side and the anode on the ground side, so it blocks current in both phases of the blink. The code is correct. To confirm, (1) check the legs: the longer leg, which is the anode, should lead toward the pin side, and the leg on the flat edge, the cathode, should lead toward GND, and here they are the other way round. (2) Use a multimeter in diode-test mode: the LED should glow, or give a reading near its forward voltage, only when the red probe is on the anode. The fix is to swap the LED's legs so the anode connects toward the resistor and pin 8 and the cathode connects to GND.
Exercise
A coworker's circuit connects 5 V through a resistor to an LED's anode, and the LED's cathode to pin 8. Their sketch sets pin 8 HIGH for one second, then LOW for one second. During the first second the LED is dark. Is the wiring wrong?
Show answer
No. This is the sinking arrangement, where the LED lights when the pin is LOW, because that is when a voltage difference exists across the LED and resistor. With HIGH, both sides of the series path are at about 5 V and no current flows. The LED is lit for the second half of each cycle. If they want the LED on during HIGH, either rewire to the sourcing arrangement (pin to resistor to anode, cathode to GND) or invert the logic in the sketch.