Microcontroller Interfacing –  Part 4

Driving LED and Other Simple Loads

  

Goals

This section describes how to drive an LED and other simple, low current loads with a microcontroller output pin. 

LEDs are very commonly used as indicators on embedded systems. When bringing up a new design, getting the LED to flash is usually the first step in checking out the hardware.  When a new programmer writes his first program on a PC, it is usually the famous “Hello, world!” program.  The embedded system equivalent is a flashing LED.

LED characteristics

LEDs are diodes.  Diodes conduct current when they are forward biased. LEDs give off light when a current flows through them.  The amount of light given off, or its brightness, will be proportional to the current. The more current that is allowed to flow, the brighter it will get. This will continue until you get one bright flash, after which it no longer emits light, or acts as a diode for that matter.

The data sheet for the LED will generally give the light output at some typical operating current.  You can operate the LED at a lower current and get less light or at a higher current and get more light as long as you don’t exceed the maximum ratings of the LED.

Different LEDs will produce different amounts of lights for the same current.  High efficiency LEDs will be brighter, but will be more expensive than garden variety LEDs.  Small LEDs typically run at 15 to 25 ma, while larger, high output types may run 100ma or more.

Another important characteristic of an LED is the Vfwd, or forward drop voltage.  A diode has an exponential V-I curve.  As the current through the LED increases, the voltage drop across it will increase only slightly. Vfwd will be listed at a specific current, usually the same one used for rating the light output.

Vfwd will usually be around 2 volts for red LEDs, and increase with different colors. Blue or white LEDs are often a bit over 3 volts.  Vfwd will be important for setting the proper current level for the LED.

LED design example

Your project will control a motor. It will use an LED to indicate an over temperature condition.   A second identical LED will indicate an over speed condition. You select a red LED and look up the specs in the data sheet.  The luminosity (light output) is what you want at the recommended current of 15 ma. The voltage drop at that current is 1.9 volts. 

Figure 4-1 shows the two LEDs being driven in different ways. LED1 is uses the microcontroller output pin P0 as a sink, and LED2 uses the pin P1 as a source. You will note a resistor in series with the LEDs. If you were to omit the resistor and drove the LED directly with the microcontroller pin, excessive current would flow when you turned on the LED, probably burning out that pin on the microcontroller or maybe the LED. The question is what value should we use for the resistor?   

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.  

Figure 4-1

Arduino  Tips

The I/O pins of the ATMega328 used on the Arduino Duemilanove can sink and source 40 ma per pin.  There is a maximum of 200ma total for the package.  About 25 ma should be reserved for the internal operation of the ATMega328 leaving about 175ma maximum current for the rest of the I/O pins.

Gotcha Checklist

1. Ensure the operating current does not exceed the LED maximum operating current.

2. Ensure the operating current does not exceed the microcontroller sink or source maximums.

3. If using the current sinking configuration, use the same voltage to drive the LED that supplies the microcontroller. 

Unified Microsystems  for electronic equipment, modules and kits for engineers, electronic hobbyists and Amateur Radio operators.

© 2009 - 2016 Gary C. Sutcliffe

  

  

Created with the QTH.com SiteBuilder.