The ubiquitous LED is one of the more useful components in your collection. As well as being an integral part of your project (for output indication or purely cosmetic purposes) they also serve well as a method of fault finding (or debugging) your circuit. If you've had any experience with C programming the simple LED is the hardware version of the 'printf()' method of debugging.
The next article in the '' uses a lot of LEDs (as it concentrates on digital output) so I though I'd take the opportunity to cover how to use them in a more general sense.
What is a LED?
An LED (or Light Emitting Diode) is, as the name suggests, a diode that has the side effect of generating a light source. It has all the properties of a diode (polarity, forward voltage drop and a maximum current) and you need to take these into account when designing your circuit.
There are a range of different colors for LEDs, for this article I'm going to concentrate on the two most common - Red and Green. For other variations you will need to refer to the datasheet for the particular LED you are using to get the required parameters. Please note that in this article I'm only using standard LEDs, if you are using a super bright or other variation of LED you will once again need to refer to the datasheet to get the appropriate parameters.
LEDs are a type of p-n diode and have the electrical characteristics of a non-ideal rectifier as shown in the image to the right. The remainder of this section looks at what those characteristics mean in terms of circuit design.
As with a normal diode a LED will only conduct in one direction. The cathode (negative pin) of the LED can be identified by a flat surface on the circular packaging or by the shorter length of the pin (if you have not yet trimmed the connectors).
Unlike a normal diode you should not use LEDs for circuit protection, that is not what they are designed for. If the reverse voltage exceeds the breakdown voltage for the LED it will conduct high currents in the reverse direction which will destroy the LED (and most likely the rest of your circuit).
The forward voltage is essentially the turn on voltage for the LED (show as the 'knee' on the electrical characteristics graph above). Voltages below this level will not activate LED and no current will flow. Once the forward voltage is met or exceeded the LED will conduct current (and therefore lights up).
Once the activation voltage is met the LED offers very little resistance and as the voltage increases the current passing through the LED increases dramatically and can easily destroy the diode when it exceeds the maximum current for the device.
An LED has a maximum operating current (usually between 20mA and 30mA - refer to the datasheet for your LED for details). If this current is exceeded the LED can be destroyed. Because the LED has such low resistance small voltage increases above the forward voltage can easily increase the current beyond the capacity of the device and therefore destroy it.
Directly Driving LEDs
To directly drive LEDs you will need a current limiting resistor in series with the LED. This resistor performs two purposes:
- It ensures the forward voltage is met and the LED will conduct (and light up).
It ensures the current flowing across the LED does not exceed the maximum rating and therefore protects the LED from destruction.
Although actual forward voltage and current limitations differ from device to device a safe rule of thumb is to assume a 2V forward voltage and a 20mA drive current. These values will work for Red and Green LEDs (both 3mm and 5mm versions).
The general circuit to drive an LED with a current limiting resistor is shown to the left. The value of the resistor can be calculated with the following formula:
R = (Vin - Vf) / I
Where R is the value of the current limiting resistor in Ohms, Vin is the driving voltage, Vf is the forward voltage for the LED and I is the operating current of the LED in Amps. For the generic LED values at a 5V driving voltage this gives:
R = (5 - 2) / 0.02, R = 150 Ohms
When the result does not match a standard resistor value you should choose the next highest available value.
For other LEDs you might find this online tool very useful, it allows you to enter the parameters for the LED and power source you are using and will calculate the value of the appropriate driving resistor for you.
Indirectly Driving LEDs
Using the direct drive circuit will still draw current from the driving voltage source. There are many situations where this current is limited (most PIC controllers for example have a limit of 25mA per output pin and will certainly fail if all outputs are providing this current simultaneously).
One way around this is to drive the LED indirectly using an NPN BJT Transistor as shown in the circuit to the left. This pulls a minimal amount of current through the signal (in the order of microamps) while allowing the full LED driving current to be pulled through the power connection. A device such as the BC547 can be used for this purpose.
There is a small voltage drop across the transistor (about 0.7V for a BC547 at 5V) so you need to take this into account when calculating the value of the current limiting resistor. The calculation becomes as follows:
R = (5 - 2 - 0.7) / 0.02, R = 115 Ohms
Once again you should use the next highest available resistor value. You can always use the original calculated value but your LED may appear dim.
Another advantage of this circuit is that the signal input has minimal impact on the rest of the circuit so it can be used as a non-obtrusive way of determining the digital output of any line without changing the behaviour of the circuit.
The takeaway points from all of this are:
Do not drive an LED directly, use a current limiting resistor to ensure the LED is protected. When using a 5V circuit a 150 Ohm resistor will drive most LEDs (2V forward voltage, 20mA operating current). This will still draw 20mA from the signal pin. * You can drive an LED indirectly through a BC547 transistor with a 120 Ohm current limiting resistor. This will draw minimal current from the signal pin and allows low current devices to drive a number of LEDs simultaneously.
Using LEDs is very simple but not as simple as it first appears. For general usage the simple scenarios outlined will be sufficient, when using non-standard LED types or using LEDs in a non-standard way you will need to take their behaviour and operation into account.
The next part of the PIC tutorial uses a number of LEDs for output, the techniques described here will be put into practice in that project.