Noise in my switch matrix?

Hi,

I am currently trying to use my Diecimila to read a 16-key matrix keypad. The keypad is a DIY piece, right now built on perfboard with 6mm Omron tactile switches. I threw together a simple sketch just to see if I had the basic wiring right. The idea is to output to the serial interface a row and column number corresponding to the switch currently pressed.

What is actually happening is that when I press a switch, I will get one or more results, many of which are not the switch I pressed. I can also get results by holding the board, touching traces, etc., which leads me to think I am getting noise. Right now I am feeding the outputs of pins 4-7 into the columns of the matrix, and then looping through the inputs on pins 8-11 to read each row to see if any switch is pressed.

Assuming I am not missing a really silly mistake in my sketch, do I need to add some caps or pull-up(down) resistors to my switch matrix to smooth things out? Or maybe I am missing a fundamental design thing here? I know my way around code and can follow a schematic OK but design from scratch I am a relative newbie... any help appreciated!

Oh, and yes, I did beep the board to make sure the wiring is basically sound... ::slight_smile:

void setup() {                
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, INPUT);
  pinMode(9, INPUT);
  pinMode(10, INPUT);
  pinMode(11, INPUT);
  Serial.begin(9600);
  Serial.println("READY.");
}

void loop() {
  for(int i = 4; i <= 7; i++) {
    digitalWrite(i, HIGH);
    for(int j = 8; j <= 11; j++) {
      if(digitalRead(j) == HIGH) {
        Serial.print(i-3);
        Serial.print("-");
        Serial.print(j-7);
        Serial.print("\n");
        delay(1000);
      }
      delay(10);
    }
    digitalWrite(i, LOW);
  }
}

Yes, sounds like you might be fighting 'floating inputs' due to lack of proper pull-down resistors. You could change your logic around and use active low outputs to scan the keypad and then just enable the internal pull-up resistors for the digital input pins.

Lefty

Other than missing pull-downs, you are in danger of frying your output pins when two keys in the same row are pressed at the same time .

You generally work with such kind of keypad as follows:

  • activate internal pull-ups
  • use a LOW strobe for the columns
  • set the other column lines to tri-state, not HIGH (i.e. INPUT)

And of course you will also get "ghosting" when two keys are pressed at the same time in different rows :slight_smile:

Have you got diodes in line with each switch?

As have been pointed out before we need to see a schematic of what you have.

No, no diodes. I don't have an easy way to attach an image of my schematic right now, but it's simply an LED matrix with switches instead of LEDs. Instead of driving a row high and a column low, I'm driving a row high and then looping through the columns to find if I see a high on any of them. Too naive, perhaps.

In reading the replies, I am seeing that in addition to noise, I am probably suffering from a fundamental design flaw. Does anyone have a link to a good example schematic I could borrow from? Everything I found was dealing with how to read a matrix keypad that was already built. I need a custom keypad configuration so I can't use an off-the-shelf unit.

Too naive, perhaps.

yes you will never cope with more than one switch being pressed at a time. If you have two pressed you will see four being pressed, this is known as ghosting.

For and example of a switch matrix see this project:-
http://www.thebox.myzen.co.uk/Hardware/Econo_Monome.html

Grumpy_Mike,

Very neat build! I think I can figure out what I need to know from that.