We will use the LED1-R1 combination to do the calculation. The source voltage is 5V from our supply. The voltage dropped across the LED is 1.9, which we found in the data sheet. Since the voltage around the circuit loop must be zero volts,
Vdd = Vr + Vfwd
Rearranging the terms gives us
Vr = Vdd – Vfwd = 5 – 1.9 = 3.1 V
We now know the voltage across the resistor is 3.1V, and we decided earlier that we wanted to drive the LED at 15ma, so a simple application of Ohm’s Law will tell us what value resistor is needed.
R = V/I = 3.1/.015 = 207 ohms.
Resistors come in standard values. The closest 5% resistor values are 200 ohm and 220 ohm. Either would be OK. The 200 ohm would result in a little more than 15ma of current, and the 220 ohm resistor would be less than 15ma.
The value for R2 is the same as R1. The only real difference is the microcontroller is supplying 5 volts instead of directly getting it from the power supply.
Sink or Source?
Which one should you use? In most cases it is a matter of personal preference. To turn on LED 1 you need to set the PO register bit to a zero. To turn on LED2, you set P1 to a logical 1. To many developers, setting a pin to a 1 or high seems more natural for turning something on, so you might want to use the microcontroller pin as a source.
One case you might need to use one driving method over another is when the maximum source and sink currents are not the same, and only one method will be able to handle the current needed.
If the pin you selected is an open drain type, you will need to sink the current. Open drain outputs do not have an internal transistor to Vdd, so the pin can’t supply current. Some microcontrollers have a few I/O pins that are open drain.
Summary
Controlling an LED with a microcontroller is a common application and is easy to implement. Make sure that current limits of the microcontroller and LED are not exceeded. |