John L Errington MSc

John Errington's Experiments with an Arduino

Driving LEDs from an Arduino microcontroller

How to drive an LED indicator (basic)

An LED is connected between the arduino output pin and EITHER the +Vcc supply, or the arduino GROUND, as shown in this diagram. driving ledIn either case you need a series resistor to limit the LED current.

The box in the diagram is just depicting a single arduino digital output pin.

 

1: Here the LED and resistor are connected between the output pin and GROUND.
The LED is lit when the output is HIGH.

 

2: Here the LED and resistor are connected between the output pin and Vcc.
The LED is lit when the output is LOW.

 

Each has its own advantages and disadvantages.

Note that if you are using style 2 the LED current also flows in the arduino GROUND line (see Grounds)

 

As a starting point a resistor of anywhere between 470 ohm - 1k will be suitable.

However if you are driving a lot of LEDs you may wish to use more appropriate values as explained below.

 

Choose the right resistor value for your application

I'm often asked "what is the right value resistor to drive an LED from a digital output on the arduino". Well "it depends". It depends on (1) the supply voltage, (2) the LED forward voltage, Vf and (3)the current you choose to run through the LED.

Vf in turn depends on the colour of your LED, and the materials used to make it.

 

Table 1 - the "right" resistor value for your project - values for use in daylight:

For use as indicators indoors you can even use resistors up to 10k to minimise power consumption. Try it.

Supply voltage Vcc
LED colour
Current mA
Resistor
3.3
red-green
2.5
470
3.3
red-green
10
120
3.3
blue, white
2.5
180
3.3
blue, white
10
39
5.0
red-green
2.5
1200
5.0
red-green
10
270
5.0
blue, white
2.5
820
5.0
blue, white
10
220

 

The table above gives some "rule of thumb" figures. If you want to know how these were calculated , read on.

 

LED forward voltage

Here is a table (Table 2) showing the forward voltage for different LEDs in the common 3mm or 5mm package. Its compiled from various manufacturer's data sheets. If you need precise values consult the data sheet for the device you are using.

LED characteristicsYou can see that LEDs in the "warm" end of the spectrum only need low forward voltages, while some types in the "cool" end - green onwards - may need over 3.3V so you could not drive these directly from a 3.3V model arduino.

 

We also need to bear in mind the electrical characteristics of the arduino chip; each output is capable of sourcing or sinking 40mA; however "the maximum current for each I/O port should not exceed 100mA".

(32U4 data sheet page 379)

 

If we connected 8 LEDs drawing 20mA each to a single port we could be drawing 8*20mA = 160mA.

I measured the forward voltage of some cheap LED's at a current of 10mA; 3mm and 5mm sizes showed the same values. Here are the results:

Colour Vf at 10mA Comment
infra-red 1.2 IR 850nm
red 2.0 bright
amber 2.0 bright
green 1.9 very dim
blue 2.85 dim
white 2.80 bright

 

NOTE: for use as indicators indoors, red, orange and yellow LEDs AND also blue and white ones are typically acceptable at currents of about 1mA. Inexpensive green LEDs are MUCH less efficient than red - yellow ones, and need a higher current if their intensities need to match.

While common 3mm and 5mm leds CAN be run at 20mA and more, a maximum of 10mA is generally sufficient, and limits your consumption, especially if you have several inicator lights. (See below for limit of consumption on output ports.)

What current do you need?

Assuming you are using the LEDs as indicators, rather then e.g. for a light show, modern LEDs work very well at currents of 2-10mA, and a maximum of 20mA. Higher power LEDs will take more current, but then its better to use a ready-made LED driver chip.

What is the supply voltage?

The voltage available to drive your LED from a digital output is the difference between the voltage on the digital output pin and the forward voltage of the LED. The data sheet for the ATMega 32U4 gives the following MAXIMUM values (29.0 -29.2) for Vcc = 3.0 or 5.0V. The values I measured for Vol were typically much lower - around 0.1V

Vcc
Iol or Ioh
Vol
Voh
5.0
10mA
0.7
4.2
3.0
5mA
0.5
2.3
3.3 *
5mA
0.55
2.6

* not on data sheet - extrapolated.

And finally - the calculation

Suppose we wish to drive a red LED with a series resistor, connected between a digital output pin and ground on a 5V Arduino. We choose a current of 10mA.

The voltage available to drive current through the LED is Vcc -Voh = 4.2V (- 0V) and the LED forward voltage (from the data sheet) is 2.1V at 10mA.

The voltage across the resistor Vr will be 4.2 - 2.1 = 2.1V

By Ohms law (V / I = R where V=volts, I=Amps, R=Ohms) we get

R = 2.1 / 10 * 10^-3 = 210 ohms.

The nearest preferred value is 220 ohms.

Reference: Table 2

Easy way to measure Vf

Measure LED Vf

If you dont have a data sheet all you need to do to get a useful (not accurate) idea of the Vf is a 9v battery and a 1k resistor. Except for Infra-red, most LEDs have a Vf of about 2-3V so a 9V battery will push 6-7mA through the 1k resistor. Then just use a multimeter (or an arduino) to measure the voltage across the LED.