Introduction
The Celtic Engineer is a weekly newsletter produced by Celtic Engineering Solutions.  We hope you enjoy it.  If you have any suggestions for topics, would like to give feedback or want your email added to the distribution list please send an email to [email protected].
Hello world?
Blinking an LED with a microcontroller for the first time (maybe the first time on your current project), is the hardware equivalent of “Hello World” for a programmer.  It means you have configured the microcontroller well enough to change the state of a port pin.  It is a very satisfying feeling.
Hardware and firmware are intimately connected and the development of firmware is an incremental task.  As you develop the firmware you are also testing out hour hardware.  This is even more exciting if you are both the hardware and firmware dude or dudette.  You might say, “Psh, you are just turning on an LED. No big deal.” Well yes, but if you remember how you felt the first time you accomplished this task, it was a big deal.  Subsequent successes may be less exciting, but you still get that good feeling.
Just plug it in and turn it on, right?
If we were to just plug an LED into a port pin and power it, that would not be a good thing.  Whether this is the first time you are attempting to power an LED from and MCU or if it has just been a long time, we should review the proper way to set the current. An LED (or light emitting diode) has a voltage drop associated with it.  When you run the specified current, or desired current which might be a lot lower, you develop a voltage across the diode.  This voltage is specified in the data sheet and it is not the same for all diodes.
The parameter we are looking for is Vf, or the forward voltage. A quick look atDigikey, will show a range of voltages from 1.6 to 14 volts.  How is that possible? It is just a PN junction.  The answer is there is a range of expected voltages for a single PN junction that vary depending on the material and the design of the junction.  There are also instances where several junctions are wired in series in a single LED.
To set the current to the desired level you must look at the drive voltage of the port pin.  Let’s assume it is 3.3 volts.  And I will also assume the forward voltage of the LED is 1.9V.  For the particular LED I want to use, it says the max current is 20mA.  This is a chip LED and I am going to use it as an indicator on my PCB.  It will not be seen by the end user.  It will not be used in bright light.  It will stare at me on my desk.  So if I choose to run it at 20mA, it will be very bright and use a lot of power.  This is not necessary.  For indicator LED’s I usually run just a few milliamps. To calculate the resistance needed to set the current, R = (3.3 – 1.9) / 0.003 = 467 ohms.  I will choose a standard 1% resistor and use 475 ohms, which will give me just under 3mA. I have found this is a comfortable light level to work with but you will have to decide what is best for your application.
Umm, LED voltage is above my pin voltage
I said above that the forward voltage can be as high as 14 volts (higher, but that’s is what was shown on the DigiKey page).  How do we deal with that being above the voltage of my MCU pin?  One way is to use a transistor and a secondary voltage source.  You could also use some type of relay.
I will use the example of the green LED made by SunLED that has a Vf = 14V.  We will assume we have a 24V supply.  The data sheet says it has a typical forward current of 10.5mA at 14V.  R = (24 – 14) / 0.0105 = 952 ohms. A standard 1% resistor of 953 ohms is chosen.  But we have one more thing to check.  How much power is dissipated in the resistor? P = V x I = 10 * 0.0105 = 105 mW.  Most 0603 resistors are 60mW, so I will probably choose an 0805 125mW or a 1206 250mW depending on the application (for more on this see Issue #8 on resistor power dissipation).
While we are talking about power dissipation, it is important to note that the LED will be dissipating almost 150mW of power when it is on.  This cannot be ignored.  If the PN junction gets too hot it will be damaged and the LED will fail.  Proper heatsinking to dissipate all that heat is essential.
Transistor!
I think we should devote a whole newsletter to talking about transistors, maybe a few newsletters.  However, I will make a few suggestions here.  The application we are discussing is a switching application.  The transistor is either on or off.  In that case, little power will be dissipated in the transistor.  What we have to watch for it’s max voltage and current. One of my favorite transistors is the FDN337N made by Fairchild Semiconductor.  It has a Drain to Source voltage of 30V and a continuous Drain current of 2.2A.  The max power dissipation is 460mW, but since the on resistance is 65 mohms (0.065 ohms), that is not a concern for this application and is a good choice.
Another reason to use a transitor that I have not yet mentioned is that MCU pins have limited drive capability. You need to check the MCU datasheet to ensure you are not trying to overdrive the pin.  If you need more current that the pin is capable of, try using the FDN337.  If you do, drop me a note and let me know what you think.
Final thoughts
This newsletter is sponsored by Celtic Engineering Solutions LLC, a design engineering firm based out of West Jordan, Utah, which can be found on the web at www.celticengineeringsolutions.com.  You can find the newsletter on the company blog, LinkedIn or by subscribing.  Send your emails to The Celtic Engineer at [email protected].