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:

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:
Source: myavr.narod.ru
Blogsphere: TechnoratiFeedsterBloglines
Bookmark: Del.icio.usSpurlFurlSimpyBlinkDigg
RSS feed for comments on this post | TrackBack URI for this post
New on WinAVR Tutorial
Running TX433 and RX433 RF modules with AVR microcontrollers,Sometimes in embedded design you may want to go wireless. Might be you will want to log various readi …Programming AVR ADC module with WinAVR,Most of AVR microcontrollers have Analog to Digital Converter (ADC) integrated in to chip. Such solut … |
New on WinARM Tutorial
What are differences between WinARM and WinAVR,Everyone who is working with AVR microcontrollers knows this powerful tool – WinAVR (http://win …LPC2000 watchdog timer,As in all microcontrollers watchdog timers purpose isto reset microcontroller after reasonable amount … |
