Connect 6 LEDs using 3 microcontroller pins

Sometimes you need more than you have. I am talking about microcontroller pins. Lets say, you have to connect 6 LED diodes but you have only 3 microcontroller pins available. To use other microcontroller isn’t always a solution using decoder circuitry isn’t necessary as well.

There is a simple hint on how to do this:

Connect diodes to microcontroller as follows:

6_leds to AVR

Now look – if you set one pin to “1” and second to “0” (leave third pin high state – as input pin) then only one led lights on. You can light two LEDs at one time as well by setting third pin as output and state “1” or “0” depends on which LED you want additionally to light on.

If you need to light all LEDs at one time then you need to change states of microcontroller pins at some desired frequency to avoid visible blinking. Using this method you can connect twelve LEDs using only 4 pins. This is convenient method of expanding when using dual colour LEDs, when two LEDs are packed in one case but in different directions.

There is an example of AVR-GCC C code how to control LEDs. LEDs were connected to Atmega microcontroller PB.0 – PB.2 pins.

Control function fragment:

unsigned char leds;           // Bits are the leds states

void LEDs_refresh(void)
{
static unsigned char state;   // Current refresh state

OFF();                      // Turn all pins to High-Z

switch (state)
{
default:
CLR_0();                  // Turn pin 0 low
if (leds &  1) {SET_1();} // Turn pin 1 high for led 1
if (leds &  2) {SET_2();} // Turn pin 2 high for led 2
state=1;                  // Next state
break;
case 1:
CLR_1();                  // Turn pin 1 low
if (leds &  4) {SET_0();} // Turn pin 0 high for led 3
if (leds &  8 ) {SET_2();} // Turn pin 2 high for led 4
state=2;                  // Next state
break;
case 2:
CLR_2();                  // Turn pin 2 low
if (leds & 16) {SET_0();} // Turn pin 0 high for led 5
if (leds & 32) {SET_1();} // Turn pin 1 high for led 6
state=0;                  // Next state
break;
}
}

Call this function at frequency when blinking of LEDs is not visible. It is better to use timer interrupt. Complete project code is here:

6leds.zip

Source: myavr.narod.ru

One Response to Connect 6 LEDs using 3 microcontroller pins

  1. There could also be a further improvement of this scheme by using dual-color (red/green) LEDs instead of each of the reverse-direction pair of “normal” LEDs. In this case you could alternate pulses to the LEDs and, thanks to our persistence of vision, you can add a third color – yellow – as yet another indicator.

    So, in a sense, you would create 9 possible states of indication: 3 LEDs, each capable of showing 3 different colors.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

To submit your comment, click the image below where it asks you to...