Saturday, April 28, 2012

Multiplexing LEDs

If you want to do anything really over the top using an Arduino than the limited number of pins you have will become a problem. For little projects there are more than enough pins, but if you want to make for example a table with a ton of LEDs or a large cube of LEDs you won't have enough.





One way to get around this problem is the use of a multiplexer. With the NTE4097B multiplexer I bought three output pins on the Arduino can control a total of 8 LEDs.

To get it to work I connected the pins as follows:
1: Vin on the Arduino, through a resistor (connected to 9V power supply )
2: The + side of an LED
3: Another LED
4: Another LED
5: Another LED
6: Another LED
7: Another LED
8: Another LED
9: Another LED
10: Digital Pin #4 on the Arduino
11: Digital Pin #5 on the Arduino
12: Ground
13: Ground
14: Pin #6 on the Arduino
15: Unused
16: Unused
17:Vin on the Arduino, through a resistor
18: Unused
19: Unused
20: Unused
21: Unused
22: Unused
23: Unused
24:  Vin on the Arduino

The program cycles through the 8 LEDs with 1 on at a time:



I found this to be a poor way to handle things. For one, this chip was a bit touchy. I accidentally shorted out something momentarily a while after making this video and the chip seems to have stopped working. Perhaps it will recover, but more importantly, there is no way to turn on more than 1 LED at a time. Next time I will have to try Shift Registers.


Here is the code, mostly stolen from here:

// Define binary control output pins
#define S0 4
#define S1 5
#define S2 6

void setup()
{
  // Set multiplexer controller pins to output
  pinMode(S0, OUTPUT);
  pinMode(S1, OUTPUT);
  pinMode(S2, OUTPUT);
}

void loop()
{
  // Loop through the 8 possible channels in the multiplexer
  for( int i = 0; i < 8; i++ ){
    setMultiplexer(i);
    delay(1000);
  }
 
}

void setMultiplexer( int i ){
 
  int s0Value = i & 0x01;      // get value of first bit
  int s1Value = (i>>1) & 0x01; // get value of second bit
  int s2Value = (i>>2) & 0x01; // get value of third bit
 
  digitalWrite( S0, s0Value ); // turn first pin on or off
  digitalWrite( S1, s1Value ); // turn second pin on or off
  digitalWrite( S2, s2Value ); // turn third pin on or off

No comments: