Sub topic 4
Wiring an LED with a current-limiting resistor
This chapter builds the circuit that every later sketch in this tutorial drives: one LED, one resistor, and a PWM-capable pin. It assumes you can write a basic sketch with setup() and loop(), use pinMode() and digitalWrite(), place parts on a breadboard, and apply Ohm's law (V = I × R). The chapter covers why the resistor is required, how to choose its value, how to wire it, and how to check the result.
Why an LED needs a resistor
An LED is a diode. Below its forward voltage (Vf) it conducts almost no current. Just above Vf, a small rise in voltage produces a large rise in current. A bulb or heater behaves differently: its current grows roughly in proportion to voltage. An LED does not.
Because of this, an LED has no useful fixed resistance. If you connect it straight to a 5 V pin, the LED only drops about 2 V, and nothing else in the loop limits the current. The current rises until something fails. On an Uno (ATmega328P), the datasheet gives an absolute maximum of 40 mA per I/O pin. A safe continuous figure is about 20 mA, and the whole chip should stay under 200 mA total. An unprotected LED can exceed these limits. It can burn out, and it can also damage the pin driver.
A series resistor fixes this. The resistor takes up the voltage the LED does not drop, and Ohm's law then sets the current:
I = (Vsupply - Vf) / RHere Vsupply is the pin's HIGH voltage: 5 V on an Uno and 3.3 V on boards such as the Due or Nano 33 IoT. The LED holds its own voltage near Vf, so the resistor sets the current almost on its own.
Choosing the resistor value
Rearrange the formula to get the resistance for a target current:
R = (Vsupply - Vf) / ITypical forward voltages depend on LED color. Datasheet values vary by part, so treat these as starting points.
| LED color | Typical Vf | R for 10 mA at 5 V | Nearest common value |
|---|---|---|---|
| Red | 1.8 to 2.0 V | 300 to 320 ohm | 330 ohm |
| Yellow | 2.0 to 2.1 V | 290 to 300 ohm | 330 ohm |
| Green (standard) | 2.0 to 2.2 V | 280 to 300 ohm | 330 ohm |
| Blue | 3.0 to 3.2 V | 180 to 200 ohm | 180 or 220 ohm |
| White | 3.0 to 3.3 V | 170 to 200 ohm | 180 or 220 ohm |
Worked example for a red LED (Vf about 2.0 V) at 10 mA on a 5 V board:
- Voltage across the resistor: 5 V − 2.0 V = 3.0 V.
- Resistance: 3.0 V / 0.010 A = 300 ohm.
- Pick the next standard value up, 330 ohm. The actual current is 3.0 V / 330 ohm ≈ 9.1 mA.
Round up, not down. A higher resistance gives a slightly dimmer LED. A lower one gives a slightly higher current than you planned. Many indicator LEDs reach good visible brightness at 5 to 10 mA, so a 220 ohm or 330 ohm resistor suits most 5 V projects.
On a 3.3 V board, the same red LED needs (3.3 − 2.0) / 0.010 = 130 ohm, so use 150 ohm. A blue or white LED with Vf near 3.2 V leaves almost no headroom at 3.3 V. Its brightness then depends heavily on its exact Vf and on temperature. For those combinations, drive the LED from a higher supply through a transistor, or choose a lower-Vf color.
Power in the resistor is not a concern here. At 10 mA through 330 ohm, P = I² × R = 0.01² × 330 ≈ 33 mW. A standard 1/4 W resistor is rated for 250 mW.
Identifying LED polarity
An LED passes current in one direction only. The positive terminal is the anode and the negative terminal is the cathode. You can tell them apart in three ways:
- The anode has the longer leg on a new, untrimmed LED.
- The cathode side of the plastic body has a flat edge.
- Inside a clear LED, the cathode connects to the larger metal piece (the anvil), and the anode connects to the smaller one (the post).
If you insert the LED backwards, it stays dark. A reverse-biased LED blocks current, so a backwards 5 V LED is usually harmless. The reverse breakdown rating of a standard LED is often only about 5 V, so do not leave it reversed for long on a higher supply.
The circuit
Current flows from the pin, through the resistor, through the LED from anode to cathode, and back to the ground (GND) pin. This is the high-side (sourcing) arrangement, where the pin supplies the current when it is HIGH.
On a breadboard, build it like this:
- Plug a jumper wire from Arduino pin 9 to a free row, for example row 10.
- Place the resistor with one leg in row 10 and the other leg in row 14.
- Place the LED's anode (long leg) in row 14 and its cathode (short leg) in row 15.
- Run a jumper from row 15 to a GND pin on the Arduino.
Each numbered row on a standard breadboard connects the five holes on one side of the center channel (a to e, or f to j). Two parts that share a row are connected. If the resistor leg and the LED anode are in different rows, or in holes on opposite sides of the channel, the circuit is open and the LED stays dark.
Pin 9 is one of the Uno's PWM-capable pins, marked with a tilde (~). Choosing it now means the same circuit works unchanged with analogWrite() in later chapters. The next chapter covers which pins support PWM.
Test the circuit with digitalWrite()
Before adding PWM, confirm the wiring with plain on/off control. This sketch blinks the LED once per second:
const uint8_t LED_PIN = 9;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH); // LED on
delay(500);
digitalWrite(LED_PIN, LOW); // LED off
delay(500);
}If the LED blinks, the wiring is correct and you can move on. If not, go through the troubleshooting section below. Debugging a fade is harder than debugging a blink, so the plain blink test is worth running first.
How the resistor behaves under PWM
PWM switches the pin between HIGH and LOW many times per second. While the pin is HIGH, the LED sees the full loop voltage, and the resistor sets the current just as it does for a steady signal. While the pin is LOW, the current is zero.
The resistor therefore sets the peak current. It does not change with duty cycle. A 330 ohm resistor gives about 9 mA at the peak whether the duty cycle is 10% or 90%. What changes is how much of each period the LED spends at that peak. The average current is roughly the duty cycle times the peak current:
I_average ≈ duty × I_peakAt 50% duty with a 9.1 mA peak, the average is about 4.5 mA. Size the resistor for the peak current you can accept at 100% duty. The PWM signal then can't exceed that value. The practical consequence is that you never size the resistor for a "dim" setting. Pick the value for full brightness and let the duty cycle reduce it.
Active-high and active-low wiring
You can also connect the LED between the supply and the pin, so the pin sinks current. In that arrangement, the LED turns on when the pin is LOW. Some boards and shields wire their LEDs this way, and so do many external LED modules.
With this wiring, the PWM value is inverted: analogWrite(pin, 255) turns the LED off, and analogWrite(pin, 0) turns it fully on. A constant lets you write the sketch once and switch wiring styles without hunting through the code:
const uint8_t LED_PIN = 9;
const bool ACTIVE_HIGH = true; // set to false for the sink wiring
void setLed(uint8_t brightness) {
analogWrite(LED_PIN, ACTIVE_HIGH ? brightness : 255 - brightness);
}
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
setLed(128); // about half brightness in either wiring
}The sketch uses analogWrite() ahead of its detailed chapter. For now, all you need to know is that the value 128 means "about half on", and that 0 and 255 are the two extremes.
Checking the circuit with a multimeter
A multimeter can confirm your calculation. With the LED lit steadily, for example during the blink test's ON half or with digitalWrite(LED_PIN, HIGH) held:
- Set the meter to DC volts and measure across the resistor, with the probes on its two legs.
- Divide the reading by the resistor value. For 3.0 V across 330 ohm, the current is about 9.1 mA.
- Measure across the LED. The reading is its actual Vf. If it differs from the table, recalculate with the measured value.
This method avoids breaking the circuit to insert the meter in series. It also works because the resistor's value is known, and resistors usually have a 5% tolerance (1% for metal-film parts).
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| LED never lights | LED reversed | Swap the LED's legs |
| LED never lights | Resistor and LED in different breadboard rows | Move parts so they share a row |
| LED never lights | Wrong pin number in the sketch | Match `LED_PIN` to the wire's pin |
| LED very dim | Resistor value too high (for example 10 kohm) | Check the color bands and replace with 220 or 330 ohm |
| LED lights at full brightness, no matter what | Resistor missing or wired in parallel with the LED | Put the resistor in series |
| LED works backwards (on when it should be off) | Sink wiring with source-style code | Invert the value, or set `ACTIVE_HIGH` to `false` |
A 330 ohm resistor has orange, orange, brown color bands (3, 3, then ×10). A 220 ohm resistor has red, red, brown. A 10 kohm resistor is brown, black, orange. Misreading a 10 kohm resistor as 330 ohm is a frequent cause of a nearly dark LED.
Where this leads
You now have a circuit that is safe to drive from any digital pin. The next chapters look at which pins on the Uno support PWM, then use analogWrite() to vary the brightness of this same LED. The resistor you chose here keeps the peak current fixed throughout.
Exercise
You are using a blue LED with a forward voltage of 3.0 V on a 5 V Uno, and you want about 15 mA when the pin is HIGH. What resistor do you calculate, which standard value do you pick, and what is the actual current with that value?
Show answer
R = (5 V − 3.0 V) / 0.015 A ≈ 133 ohm. The next standard value up is 150 ohm. The actual current is 2.0 V / 150 ohm ≈ 13.3 mA, which is under the 15 mA target and well below the 20 mA continuous limit.
Exercise
With a 330 ohm resistor and a red LED (Vf about 2.0 V) on a 5 V pin, the peak current while the pin is HIGH is about 9.1 mA. If the PWM duty cycle is 25%, what is the approximate average current, and does the resistor value need to change to make the LED dimmer?
Show answer
The average current is about 0.25 × 9.1 mA ≈ 2.3 mA. The resistor does not need to change. It sets the peak current at full on, and the duty cycle controls how much of each period the LED spends at that peak.
Exercise
You rewire the LED so its anode goes to 5V and its cathode goes through the resistor to pin 9. A sketch calls analogWrite(9, 255). Is the LED on or off, and what value should you pass for near-full brightness?
Show answer
The LED is off, because a constant HIGH leaves no voltage difference across the LED loop. For near-full brightness in this active-low wiring, pass a value near 0, such as analogWrite(9, 0) for fully on or 255 - brightness for any brightness you compute.