How To Control Multiple Leds

Multiplexing the I/O ports, you can control many leds using a few ports. Improved! (5/21/2006)

Is possible to control 20 Leds using only 5 ports? Sure! Learn how to do that.

Updated: Now, use less resistors and the schematic is easier to understand. This page was originally devised on 2004.

Before we start, lets review some basic information about LEDS.

According to Princeton University in NJ, a LED is "light-emitting diode: diode such that light emitted at a p-n junction is proportional to the bias current; color depends on the material used".

Not each led uses the same voltaje. An infrared LED need at least 1.5 Volts. A Red LED needs 1.8 volts to produce light (That is the reason why a 1.5V battery can't make a LED work). Some green LEDs need 2.5 volts. A blue or white led can require up to 3.2 Volts.

A LED allows the voltaje to flow to one direction only. As you can read in the definition, it is a DIODE, not a resistor, so it need an additional resistor to create a circuit. You can calculate the value of the resistor using the OHMS LAW (V=I*R).

That means: RESISTOR = (SUPPLY VOLTAGE - LED VOLTAGE) / CURRENT

Supply voltage = The voltage in the circuit. LED Voltage = Voltage required by the LED. Current = Current required by the LED to produce light.

You can get these two last values in the datasheet of the LED.

Let's try the first circuit to control 2 LEDs using only one pin: Is something wrong with the circuit? You mean: the diodes are connected directly to the voltaje, the current will flow thru the leds and both leds will be on?

Not really. As you can see, the voltage is only 3 Volts, so each LED get 1.5 Volts. That is not enough voltage to turn on both LEDs. Only when voltage comes from the microcontroller port will turn on the LED. When you send a "1" (HIGH) thru the pin, the botton led will be on. With a "0" (LOW) the upper one will be on. How to turn off both LEDs? Just make the pin as INPUT.

If your circuit have a greater voltage, you can use the next circuit but you will not be able to turn off both LEDs, turn off one of them sending an "1" or "0" thru the port or turn on both LEDs making the port as input. The reason why I used a variable resistor, is because if you want to use a GREEN LED and a RED one, your have to adjust the resistance.) The following schematic shows multiple LED configuration using only a few ports. There is a formula to know how many LEDs can be used with certain IO ports. The formula is:

# LEDS = 'N' PORTS * ('N' PORTS - 1)

That means:

2 IO Ports allows 2 LEDs, LEDs = 2 * ( 2 - 1 )
3 IO Ports allows 6 LEDs, LEDs = 3 * ( 3 - 1 )
5 IO Ports allows 20 LEDs, LEDs = 5 * ( 5 - 1 )
6 IO Ports allows 30 LEDs, LEDs = 6 * ( 6 - 1 )
8 IO Ports allows 56 LEDs, LEDs = 8 * ( 8 - 1 )

The configuration to connect all these LEDs are: To be able to control all these LEDs, some IO ports need to be set as 'HIGH' (1), others as 'LOW' (0) and others as input (HI-Z).

Here is an example how can be controlled 6 LEDs: If you want to turn on many LEDs, you can try different configurations or send it as pulses. Using pulses to control leds, will allow to remove the resistor. Pulses limits, by time, the current in each LED.

An example of this information can be found in the " LED chaser " link.

 < Four Digit Counter Homepagemicrocontroller Index How to get 1 Second with Microcontrollers.>