IC for Input multiplexing?

Hi, I'm looking for an easy way to read the status of multiple switches and buttons.
Found this Arduino Playground - 4051 but it says it's unable to read more than 1 button at time which wouldn't work for me.
Can you guys please suggest something that even a newbie can understand and implement?
Thank in advance for any input.

Multiplexing means many in to one.

Mark

Thanks for answering Mark.

What you are saying is what I stated is not possible?

Then, how can I read tons of switches and buttons using the less possible arduino pins?

Would appreciate some guidance please.

Why do you think multiplexing won't work for you.

Mark

I don't know really, I'm not sure what you meant in your previous message.
I understood it that way.

Jay98:
Hi, I'm looking for an easy way to read the status of multiple switches and buttons.
Found this Arduino Playground - HomePage but it says it's unable to read more than 1 button at time which wouldn't work for me.
Can you guys please suggest something that even a newbie can understand and implement?
Thank in advance for any input.

Yes, in general you can only read the status of a single button with a single read. So what? Just loop to read all of the inputs. Set S0/S1/S2 to each of the values in turn to read all 8 values:

int values[8];

void readValues ()
{
  int i;

  for (i = 0; i < 8; i++) {
    digitalWrite (S0, i & 1);
    digitalWrite (S1, (i >> 1) & 1);
    digitalWrite (S2, (i >> 2) & 1);
    values[i] = analogRead (MULTIPLEX);
  }
}

it says it's unable to read more than 1 button at time which wouldn't work for me.

"at a time" means a very different thing to you than it does to a device running at 16MHz.

In general, a multiplexer is a digital IC that has many inputs, and only one output, as well as a binary based channel select that selects which input gets piped to the output. So a 3-8 multiplexer will have 8 different channels, selectable by 3 pins.
The idea for you is it will expand your inputs from 4 to 8 (3 select + 1 Input) or 5 to 16 (4 Select + 1 Input), and you cycle through each of channels on the Mux and read it then. It is slower than directly reading an input, since you can only read one at a time. Another option is this: SparkFun I2C Expander Breakout - PCF8575 - BOB-08130 - SparkFun Electronics, which is an I2C I/O expander. You will have to do some digging on the chip if you need more than the extra 16 to see if you can assign different I2C addresses to the chip.

Also note, the response time for a human versus the response time for a uC is much much different. From my experience, checking a button once every 100-200ms will respond the feel to you as if it was instantaneous. As long as you are not running anything that is 'mission critical' that it responds in microseconds (Not that any button should be that critical), you should be fine.

You can use a shift-in register or 2 or 3 - sample the state of 8 or 16 or 24 buttons/switches/levels whatever at one time, SPI them into 1-2-3 registers, and then do your logic from there, make a decision as to what to do on a bit by bit basis.

Jay98:
Hi, I'm looking for an easy way to read the status of multiple switches and buttons.

How many switches do you need to support?

Hi, and thank you all for the answers, I was thinking on about 90 switches/buttons

Jay98:
Hi, and thank you all for the answers, I was thinking on about 90 switches/buttons

Do they have to be in any particular orientation? 90 Buttons is 15 less than a full QWERTY keyboard.

Jay98:
Hi, and thank you all for the answers, I was thinking on about 90 switches/buttons

How about 6 * 74HC4067 and select which one you want to read from using a 3 to 8 multiplexer to their enable pins? It'll use 9 pins for 96 inputs.
Here is my code for reading two 74HC4067 using a transistor to invert the enable signal to one of them. It assumes that only one switch at a time will be on.

int readSwitches(){
  muxin = 0;
  for (y=0; y<2; y++){   //two multiplexers to be read in turn
        for (i=0; i<16; i++){  //check each input in turn
        digitalWrite(4, i&1);  //Output address, LSB first
        digitalWrite(5, (i/2)&1);
        digitalWrite(6, (i/4) &1);
        digitalWrite(7, (i/8) &1);  //MSB
         // now enable one of the Muxs
        digitalWrite(8, y); //send either a 0 or 1 to the enable pins
        delay(25);  //allow time for address to stabilise (probably could be reduced)
      //read whether switch is on
      if(digitalRead(9)==0){  //Switch is on when input grounded
        muxin = i +1 +(y*16);   //save address of the switch
      }
    }
  }
    if(muxin <33 && muxin>0){  //output number of switch that's on (if any)
      Serial.print("Selected switch = ");
      Serial.println(muxin);
     }
 return muxin;
}

Simpler approach:

#include <SPI.h>
byte buttonArray;
byte x;
byte ssPin=10;
void setup(){
pinMode(ssPin, OUTPUT);
SPI.begin();

void loop(){
digitalWrite(ssPin, LOW);
digitalWrite (ssPin, HIGH); // sample data on this edge
  for (x=0; x<12; x=x+1){
  buttonArray[x] = SPI.transfer[0];
  }
// do something with the data
}

CrossRoads:
Simpler approach:

That depends on how you define simpler :slight_smile:
12 ICs, to me, isn't simpler than 7, but I'll agree that your code is simpler.

Hello guys, I apologize for the late answer, was busy with school stuff the last few days.
Thanks for all who answered, sadly I'm unable to understand most suggested solutions specially the hooking part.

Let me ask you something...

Somewhere I read about a 8x8 matrix, being able to detect multiple buttons pressed at the same time, using diodes.
I think that would be a lot easier for me, may I ask you for a working example?

Thanks in advance.