Tuesday, March 04, 2014

Theory

In this post I want to take some time to explain the schematics. I want this to be as accessible as possible, so we'll start at the very beginning.

Connecting an LED

LEDs arent' like ordinary light bulbs. You cannot (read: should not) connect an LED directly to a battery. The reason for this is that an LED has a very low internal resistance, so you'll effectively be shorting the battery. Sure, it will work if the voltage is right, but you risk damaging the LED. They're also picky about the voltage, so you'll want to provide an LED with just the right voltage.

To accomplish this we put a resistor in series with the LED. To get the right resistor value you need a couple of parameters: the forward voltage of the LED (VF), the input voltage (VI), and the forward current (IF). The forward voltage of an LED depends on its color. Red usually means 2.4V, Green and Blue 3.4V. The input voltage of my cube is 5V. The forward current of an LED is typically 20mA (0.02A). Check your datasheets for your specific LEDs to verify this.
Now we need our input voltage VI to drop to VF, so that's VI - VF Volts. Then we simply apply Ohm's law (I = U/R, or R = U/I) and we get R = (VI - VF) / IF. So for a red LED that's (5 - 2.4) / 0.02 = 130 Ohm. For green and blue it's (5 - 3.4) / 0.02 = 80 Ohm.

Let's wire up a red LED to 5V. 130 Ohm resistors aren't that common, so we'll use the next best thing: 150 Ohm. Remember, LEDs are Light Emitting Diodes, so they'll only let current flow from one lead to the other, specifically from the anode (long lead) to the cathode (short lead, and the side where there's a flat spot).

That was simple enough, now let's try something more interesting.

Connecting an RGB LED

Multicolor LEDs come in two configurations: common Anode and common Cathode. They are basically multiple LEDs in one package with either the anode or cathode lead shared between all LEDs.
Does it matter which one you use? Not really, as long as you know what type you're using/ordering. This also has consequences for where you place the series resistor. You shouldn't connect your resistor to the common lead of your LED, unless you have a bi-color LED where both LEDs have the same forward voltage, or you're using the highest resistor value.

For the rest of this post we'll be using common anode LEDs, since that's what I'm going to put in my cube.

Connecting it is easy, just hook 5V to the common anode, a few resistors to each cathode and the other end of the resistor to ground.

Controlling an RGB LED

So we've got our RGB LED burning, but now we want to control it. We want to be able to turn each color on and off. So we grab our Arduino (or other microcontroller) and hook our LED up. Instead of connecting the resistors to ground, we connect each resistor to a I/O pin of the microcontroller. With an AVR, like the Atmega328 that's on an Arduino Uno/Nano, we can set the pin to OUTPUT mode (low impedance), and by writing a digital LOW we connect the pin to ground (it sinks current), and the LED will turn on. By writing a digital HIGH we connect the pin to 5V (it sources current) and the LED will turn off (since both the anode and cathode are connected to 5V, no current will flow).

While this works fine for one RGB LED, and even for two or three, any more will start to become a problem. Remember how an LED has a forward current of 020mA? If you turn all three colors of an RGB LED on that becomes 60mA. If you connect 4 RGB LEDs and turn them all on you'll be putting 240mA through the microcontroller. Now google the datasheet for the Atmega328 and have a look at chapter 29. Electrical Characteristics, section 1 Absolute Maximum Ratings. It says: DC Current Vcc and GND pins: 200mA. Whoops, we possibly just killed our Arduino. Also note how the DC Current per I/O pin is just 40mA, so don't even think about connecting more than one LED to a pin.

So we'll need to come up with some way to reduce the amount of current going through our microcontroller. Fortunately, there's this thing called a transistor. With just a tiny amount of current you can control a much larger amount of current. A transistor has three leads: Base, Collector and Emitter. The base lead is used to control the amount of current flowing between the collector and emitter. There are two types of transistors: NPN and PNP. In an NPN transistor, a small current entering the base is amplified to produce a large collector and emitter current. (http://en.wikipedia.org/wiki/Bipolar_junction_transistor#NPN)

A PNP transistor works the other way around. When you pull the base low (connect to ground) a larger current will flow from the emitter to the collector (notice how the flow of current between the emitter and collector is reversed compared to a NPN transistor).

We'll be using a NPN transistor. We need a transistor that has a maximum collector current of at least 20mA. Then there's this thing called the gain. This figure indicates how much more current there is going to flow through the collector and emitter, compared to how much flows through the base and emitter. Basically it's your amplification. We need at least 20mA, but we don't want to use that much to control it. So let's use a gain of 10, this way we only need 2mA to control 20mA.

If you have a look at the datasheet of the 2N3904 you can find the following figures:
Collector Current : 200mA
DC current gain (at 1mA IC (input current)) : 70
That's well within specs.

The required base current will be 20mA / 70 = 0.3mA. Now we need to find a resistor to give us at least 0.3mA. First we have another look at the datasheet, we need to find the base-emitter saturation voltage, which happens to be between 0.65 and 0.85V, so 0.75 average. Enter Ohm's law again. (5V - 0.75V) / 0.0003A = 14166 Ohm. So a 10K resistor will do the trick, this will give us (5V - 0.75V) / 10000 = 0.4mA. A lower resistor like 1K will work as well, but it'll increase the power consumption, add extra heat, shorten the life of our components, etc. Don't even think about directly connecting your I/O pin to the transistor base lead since you'll effectively be shorting it to ground and kill your micro controller. If you want to read a bit more, click here.


That's it for now, next time I'll explain the anode driver and shift registers :)

No comments:

Post a Comment