driving 12 transistors with few I/O's - is it possible?

Hi,

I've build a waveshield (Audio Shield for Arduino)
And now I have 12 push buttons (with 12v lamps inside) split out on A0, A1 and A2, each with their own resistor ladder.

In addition to this, I would like to be able to switch the specific lamp on when that button is pushed. So my initial thought was to use 12 transistors and just turn them on/off with digitalWrite.

Apparently that wasn't going to be so straightforward as I thought, because I only have 10 available pins (A3, A4, A5, 0, 1, 6, 7, 8, 9, 12).

So either I can try making a longer resistor ladder and see if I can have all my buttons on one analogin-pin, or else I would really like to know if there is some kind of smart way to drive 12 transistors with, say, 2 or 4 I/O's? Some kind of matrix or.. I don't know.

Well if you have at least 4 pins available, you can use a 4 - 16 deMux here.

Ah! Thanks - I'll have to try that out

Hi a usual solution is to use a 15 cent shift register like these: http://yourduino.com/sunshop2/index.php?l=product_detail&p=319

See: http://arduino.cc/en/Tutorial/ShiftOut for How-To.

DISCLAIMER: Mentioned stuff from my own shop...

OK, So I've tried for quite a long time now doing this with a 74HC595 - without luck.

I used this tutorial and the example code 1.2 (one by one): http://arduino.cc/en/Tutorial/ShiftOut

So I hooked 3 transistors (IRF520N) up to pin 1, 2 and 3 of the 595 and I hooked everything else up as explained in the above-mentioned tutorial (with a 0.1f cap) I also tried it without the 0.1f cap.

I also tried using the LED setup (as in the tutorial), and the LED's light up when I hook up the arduino, but as soon as I send anything to the serial, all the LEDs go off and I can't put any of them on. Something is wrong, and I'm really on bare bottom here.
Any suggestions what I might be doing wrong?

And by the way, does anyone know if the 0.1"f is a 0.1 uf cap ? this really confuses me

Søren

soeren:
as soon as I send anything to the serial, all the LEDs go off and I can't put any of them on. Something is wrong, and I'm really on bare bottom here.
Any suggestions what I might be doing wrong?

Suggest you create a minimal test sketch that demonstrates your problem, and post the entire sketch plus a circuit diagram showing what hardware you have connected.

This is my code:

/*
  Shift Register Example
 for 74HC595 shift register

 This sketch turns reads serial input and uses it to set the pins
 of a 74HC595 shift register.

 Hardware:
 * 74HC595 shift register attached to pins 2, 3, and 4 of the Arduino,
 as detailed below.
 * LEDs attached to each of the outputs of the shift register

 Created 22 May 2009
 Created 23 Mar 2010
 by Tom Igoe

 */

//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 8;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 11;

void setup() {
  //set pins to output because they are addressed in the main loop
  pinMode(latchPin, OUTPUT);
  pinMode(dataPin, OUTPUT);  
  pinMode(clockPin, OUTPUT);
  Serial.begin(9600);
  Serial.println("reset");
}

void loop() {
  if (Serial.available() > 0) {
    // ASCII '0' through '9' characters are
    // represented by the values 48 through 57.
    // so if the user types a number from 0 through 9 in ASCII, 
    // you can subtract 48 to get the actual value:
    int bitToSet = Serial.read() - 48;

  // write to the shift register with the correct bit set high:
    registerWrite(bitToSet, HIGH);
  }
}

// This method sends bits to the shift register:

void registerWrite(int whichPin, int whichState) {
// the bits you want to send
  byte bitsToSend = 0;

  // turn off the output so the pins don't light up
  // while you're shifting bits:
  digitalWrite(latchPin, LOW);

  // turn on the next highest bit in bitsToSend:
  bitWrite(bitsToSend, whichPin, whichState);

  // shift the bits out:
  shiftOut(dataPin, clockPin, MSBFIRST, bitsToSend);

    // turn on the output so the LEDs can light up:
  digitalWrite(latchPin, HIGH);

}

this is my setup:

I'm sending int numbers from Max to the arduino (0-9) and I see that the RX led is lighting up on the board - so the serial connection is working.

What happens is when I connect the arduino, some of the LEDs light up and some don't. When I then send the first number through serial to the arduino, all the LEDs go off and they stay off no matter what number I send.

The IC is a 74HC595 and the cap is a 100nf ceramic cap

Is the 'Max' you refer to a Max232 adapter? If not, what is it?

I recommend you modify your sketch to print out the bit numbers you're receiving so you can be sure it's doing what you intended.

I see the pins you're using do not exactly match the comments in the sample sketch, and I assume you've chosen to use different pins. I don't see anything wrong with the ones you're using, but I suggest you double and triple check that the pins used in your code have been wired to the correct inputs on the shift register. If you have accidentally transposed any of them either in the code or in the hardware it would not be obvious and could produce some very strange behaviour.

PeterH:
Is the 'Max' you refer to a Max232 adapter? If not, what is it?

I recommend you modify your sketch to print out the bit numbers you're receiving so you can be sure it's doing what you intended.

I see the pins you're using do not exactly match the comments in the sample sketch, and I assume you've chosen to use different pins. I don't see anything wrong with the ones you're using, but I suggest you double and triple check that the pins used in your code have been wired to the correct inputs on the shift register. If you have accidentally transposed any of them either in the code or in the hardware it would not be obvious and could produce some very strange behaviour.

it's Max/Msp (a visually oriented programming interface). A bit like pure data.

I'm absolutely sure that the Arduino is receiving the correct data over serial, and the pin wiring is exactly the same as in the tutorial. They've made a mistake in the first comment of the sketch where they write pin 2, 3, and 4. That's got to be a mistake, because they've obviously hooked it up to 8, 11 and 12 - both on the physical side and in the code (the part that's not commented out).

In the datasheet of the MCP23016, it says to use a cap value of 33pf from the clock pin to GND.
I don't have a 33pf exactly, but I have some 10pf's lying around.

Could I just hook 3 of them up in parallel and get 30pf? Would that difference matter at all?