Read from multiple switches?

Hi all,
Is there any way to use the Arduino to monitor input from multiple switches? Say 12 in total? My NG board only has 5 analog inputs.
What I'm looking for is keyboard-type behaviour from the switches. I want to be able to capture key down and key up events, and check for key combinations, i.e. different keys being pressed simultaneously.
Thanks for any suggestions!

Well, I just found the 4051 tutorial, I suppose that might meet my needs?

You can use the digital inputs to read the status of a switch.

The analog inputs are for reading a voltage in the range 0 - 5V and return it as a number to your program. Use them to connect things like potentiometers or sensors.

The digital inputs can read high / low (0V OR 5V) there are 13 digital pins on the Arduino board. Use them to connect switches.

The only problem is that you can't use pin 0 and 1 if you also use serial communication (talk to your computer over the "USB" connection).

You could use the 4051 to (de)multiplex your digital i/O pins.

Thanks, I'll do some experiments.

I'm trying to do something very similar: read the status of 20 buttons. Please post what you learn, and I'll do the same.

There are also some hacks for reading multiple switches with a single A-D input; basically you set up a resistor matrix so that each key ends up creating a different voltage on the input; it's pretty easy to do 16 or more switches with a single input (and about 16 resistors :slight_smile: The basic concepts are show in this PICmicro app note, but are applicable to any micro:
http://www.e-sonic.com/whatsnew/Microchip/input/40040b.pdf

This is a super interesting document. Contains lots of not so obvious solutions to I/O problems / tasks.

Thanks for posting that link.

The lesson learnt is that the different micro processors are not that different, analog I/O, digital I/O, PWM is the same for all. Maybe voltage levels and programming languages / environments are different but most principals are the same. So we can learn a lot from reading documents for other brands than Arduino / Atmega.

MikMo

You could use the 4051 to (de)multiplex your digital i/O pins.

Hey mikmo - did you really mean that the 4051 can also mux/demux digital inputs? are you sure?

Interesting info westfw. Any particular values for the resistors and capacitor?
I have a feeling that this approach won't work for checking for multiple simultaneous keypresses. (5 for example.) Do you think that's right?

Yes i really think it can.

Analaog is reading a value between 0 and 5 volts.

Digital is reading either 0 or 5 volts.

With the 4051 you can use 3 digital lines to control which of 8 lines on the 4051 you are reading if you just read 0 or 5 (connect a switch to the inputs of thr 4051 in stead of a pot) it should work, you are just limiting the 4051 to work with just two "analog" levels (0 or 5 volt).

see the drawings here Arduino Playground - 4051

Somebody kick me if i'm wrong.

Otherwise read this http://www.arduino.cc/en/Tutorial/ShiftIn using the 4021 shift register

MikMo