LEDs and Microcontrollers

Another Fred Note – LEDs and Microcontrollers
(for you who need a refresher and you who are new)

Read: Forrest Mims III’s Getting Started in Electronics, pages 44-46, 66-69

Light Emitting diodes are very efficient electrical devices that turn small amounts of electrical current into light. Because they are so efficient, and because the light-generating material is so small, they do not tolerate large amounts of electrical current the way ordinary incandescent light bulbs, solenoids, and motors do. Light bulbs and motors are wasteful in that they produce a lot of heat as by-products of their normal operation (the light, after all, comes from a glowing piece of metal in an anaerobic (no oxygen) environment, so it doesn’t burn) as they dissipate excess current (up to a point).

We can take any lightbulb and drive it with its recommended voltage with no other devices in the circuit. We can usually go higher, making the bulb light much more brightly, though we risk blowing the bulb, or at least shortening its life. A normal Christmas tree bulb, for example, running at 3 volts DC draws .18 amps. If I bump that up to 6 volts, it draws .30 amps and is too bright to look at, but is otherwise doing fine. If I go much higher, I blow the bulb. Why? The filament eventually gets hot enough to melt.

A typical LED on the other hand is made to run on about 20mA; that’s .020 amps or 1/10 of the current used by the Christmas tree bulb. Also, an LED typically drops about 2 volts in its normal operation. So, if we power an LED with a 2 volt power supply, the device will have all the voltage it needs to light (to overcome the PN junction barrier) and will draw only as much current from the power supply as it needs. Most power supplies that we use however, are not 2 volt power supplies. So what do we do? The most common solution is to waste some current; add a series resistor to make sure that only 20mA runs through the circuit.

Our Atmega 168/328 chip’s output pins supply (source) 5 volts. So if we power an LED from a pin on our microcontroller, we need to deal with the 3 volts that remains after the LED drops its 2 volts. If we don’t do anything, that voltage will all turn to current and the LED will emit smoke and a horrible smell. Here’s how we calculate the value of resistor we need:

From Ohm’s law, we remember that V = I * R. In this case, our V is 5 volts. However, our LED will take care of 2 through the process of lighting, so what we have to deal with is really 3 volts.

3 volts = .02 amps * R ohms —-> 3 / .02 = R = 150 ohms

Resistors dissipate electrical current by creating heat, and are also rated in watts. Watts are an expression of how much work a device is doing. The small resistors we use in class are ¼ or ½ watt. The resistor that determines High or Low light level in a slide projector might be rated for 20 watts or even more, and is a big wire-wound ceramic monster.

Power (watts) = EMF (volts) * Current (amps) —> power = 3 * .02 = .06 watts.
So a ¼ watt resistor will be fine in this case.

The general formula for figuring out the value of a series resistor is:
Resistance = (Volts-in – Volts-led) / Current-Ied    (units are volts, amps, and ohms)

Very Important – Our microcontroller sources 5 volts, however, it also can only deliver these 5 volts at a certain amount of current. Microcontrollers are meant to be brains, not muscles. Typically we place devices between the microcontroller and the world to mediate the outputs. From the Atmega168 datasheet, each output pin can source 40mA of current. In addition, any combination of pins in the same register (A, B, C, D) can support a maximum of 100mA. So, if you use 5 of the D ports to drive LEDs, you reach this 100mA maximum when you drive each LED at 20mA. If you need to drive 8 LEDs, you could drive 4 on D ports and 4 on B ports. This becomes an issue when using lots of LEDs or ultrabright LEDs. Note: In the Arduino world, the registers are hidden. Refer to this document for a cross reference.

I will cover techniques for driving large current loads with the Atmega in another Fred Note. The short answer is: NPN transistors (2N2222a), MOSFETs, ULN2803 IC, SN754410 IC.

For those of you who want to speed ahead:
The folks at Machine Science have, for years, been developing curriculum targeted at high school students, written at a perfect technical level for most artists working in this area. Click the Tutorials tab to open the Machine Science Curriculum Guides. Scroll down to #2, Atmega Breadboard Projects. I suggest reading through it, although you will have to translate some of the code specifics from the Machine Science pure C and the modified C used in the Arduino IDE. [I will post a future note about this.] This materialis is really the core of what most artists working in microcontrollers need: simple lighting with LEDs, tone generation, reading analog light levels, and use of switches. The DC motors discussion, under robotics is a great discussion as well.

Be sure to visit the Atmel home page (www.atmel.com). This will give you a good idea of how these chips are used. For example, there are zillions that are strictly for automotive use. Toasters, space heaters, blenders, cardiac pacemakers, etc. all use them as well. Here’s a link to the Atmega168 chip we are using and one for the newer Atmega 328 chip. Under the Documents tab, download the chip summaries, and for late night reading, the full datasheets.