Sub topic 4
Wiring an LED with a Current-Limiting Resistor
Assume you know the basic Arduino sketch structure, pinMode(), digitalWrite(), breadboard wiring, and introductory Ohm’s law. This chapter focuses on building a safe LED circuit before using PWM to control its brightness.
Why the resistor is required
An LED is a light-emitting diode. Like other diodes, it allows current to flow mainly in one direction. Once its forward voltage is reached, a small voltage increase can cause a large current increase.
An Arduino Uno output pin provides about 5 V when set HIGH. If you connect an LED directly between the pin and ground, the LED and the pin may draw excessive current. The LED can fail, and the Arduino pin can be damaged.
A current-limiting resistor is placed in series with the LED. The resistor absorbs the remaining voltage and sets the current:
R = (Vpin - Vf) / I
Here:
Ris the resistor value in ohms.Vpinis the Arduino output voltage.Vfis the LED’s forward voltage.Iis the desired current in amperes.
For a red LED with a forward voltage of about 2 V and a target current of 10 mA:
R = (5 V - 2 V) / 0.010 A = 300 Ω
The next common value is 330 Ω. A 330 Ω resistor is a good default for an indicator LED. It produces useful brightness while keeping the current modest.
Identify the LED terminals
Most through-hole LEDs have two leads:
- The longer lead is usually the anode, or positive terminal.
- The shorter lead is usually the cathode, or negative terminal.
- The flat edge on the LED body usually marks the cathode.
- The larger internal metal shape is usually connected to the cathode.
The anode connects toward the Arduino output through the resistor. The cathode connects to GND. The resistor can be placed on either side of the LED, because both components are still in series.
| Component | Connection |
|---|---|
| Arduino pin 9 | One end of the resistor |
| Resistor, 220–1,000 Ω | In series with the LED |
| LED anode | Resistor side |
| LED cathode | Arduino GND |
Breadboard circuit
Use pin 9 as the output in this example. Pin 9 supports PWM on an Uno, so the same circuit can later be used with analogWrite().
Insert the LED so that its two leads are in different breadboard rows. Connect a jumper from pin 9 to one end of the resistor. Connect the other end of the resistor to the LED anode. Connect the LED cathode to a GND pin on the Arduino.
The resistor must not be bypassed by placing both of its leads in the same breadboard row. On a typical breadboard, holes in one connected row are electrically common. Check the breadboard’s connection layout before wiring.
Test the wiring
Upload this complete sketch. It turns the LED on for one second and off for one second.
const uint8_t LED_PIN = 9;
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
digitalWrite(LED_PIN, HIGH);
delay(1000);
digitalWrite(LED_PIN, LOW);
delay(1000);
}If the LED stays off, disconnect power before changing the wiring. First reverse the LED if its polarity is wrong. Then check that the ground jumper reaches an Arduino GND pin and that the resistor is actually in series with the LED.
If the LED is unusually bright, hot, or the board resets, disconnect it immediately. Recheck for a missing resistor or a short circuit. A resistor between 220 Ω and 1 kΩ is suitable for most small indicator LEDs. Larger resistors reduce brightness but also reduce current.
The LED now has a safe circuit. In the next step, a PWM-capable pin can switch this circuit rapidly to control its average power and apparent brightness.
Exercise
You have a red LED with Vf = 2.0 V and want approximately 10 mA from a 5 V Arduino output. What resistor value should you calculate, and which common resistor value would you choose?
Show answer
Calculate R = (5 - 2.0) / 0.010 = 300 ohms. Choose the next common value, such as 330 ohms, to keep the current at or below the target.