# Problem with 4x3 button matrix

**URL:** <https://forum.arduino.cc/t/problem-with-4x3-button-matrix/1042320>\
**Category:** Programming\
**Created:** [October 15, 2022, 7:01pm UTC](https://forum.arduino.cc/t/problem-with-4x3-button-matrix/1042320 "2022-10-15T19:01:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![xsyrax123](https://avatars.discourse-cdn.com/v4/letter/x/ea5d25/32.png) [@xsyrax123](https://forum.arduino.cc/u/xsyrax123)\
**Post date:** [October 15, 2022, 7:01pm UTC](https://forum.arduino.cc/t/problem-with-4x3-button-matrix/1042320/1 "2022-10-15T19:01:03Z")

</div>

Good afternoon, I am making a 4x3 keypad for an access control system. The problem I have is that when I press a key for the first time on the keypad, the key pressed is displayed correctly in serial, but if I press another key it is not displayed. Am I missing something in the circuit? Is there something missing in my programming? I appreciate your help, thank you.

 ![queshittttt](https://europe1.discourse-cdn.com/arduino/original/4X/7/a/0/7a0c97c7aae0b809cb158f7aff153d56a120fcc6.png)

```arduino
const char keys[4][3] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
const byte rowPins[4] = {13, 12, 11, 10};
const byte colPins[3] = {9, 8, 7};
byte i, j;

void setup(){
    for (i=0; i<4; i++){
        pinMode(rowPins[i], OUTPUT);
    }

    for (i=0; i<3; i++){
        pinMode(colPins[i], INPUT_PULLUP);
    }
    Serial.begin(9600);
}
 
void loop(){
    for (i=0; i<4; i++){
        digitalWrite(rowPins[i], LOW);
        for (j=0; j<3; j++) {
            if (!digitalRead(colPins[j])){
                Serial.print("Key: ");
                Serial.println(keys[i][j]);
                while(digitalRead(!colPins[j]));
            }
        }
        digitalWrite(rowPins[i], HIGH);
    }
    delay(10);
}

```

---

<div class="post-metadata">

**Author:** ![LarryD](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/larryd/32/215550_2.png) [@LarryD](https://forum.arduino.cc/u/LarryD)\
**Post date:** [October 15, 2022, 7:04pm UTC](https://forum.arduino.cc/t/problem-with-4x3-button-matrix/1042320/2 "2022-10-15T19:04:45Z")

</div>

Read thru this discussion:

[https://www.gammon.com.au/forum/?id=14175](https://www.gammon.com.au/forum/?id=14175)

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [October 15, 2022, 7:07pm UTC](https://forum.arduino.cc/t/problem-with-4x3-button-matrix/1042320/3 "2022-10-15T19:07:08Z")

</div>

Are you releasing the first key before pressing the second one ?

---

<div class="post-metadata">

**Author:** ![xsyrax123](https://avatars.discourse-cdn.com/v4/letter/x/ea5d25/32.png) [@xsyrax123](https://forum.arduino.cc/u/xsyrax123)\
**Post date:** [October 15, 2022, 7:12pm UTC](https://forum.arduino.cc/t/problem-with-4x3-button-matrix/1042320/4 "2022-10-15T19:12:30Z")

</div>

I figured out the glitch, the problem was that I was negating the column and had to negate the read in:  
while(digitalRead(!colPins[j]));  
I changed it to: while(!digitalRead(colPins[j]))  
Now it works fine.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 13, 2023, 7:13pm UTC](https://forum.arduino.cc/t/problem-with-4x3-button-matrix/1042320/5 "2023-04-13T19:13:26Z")

</div>

Closed on April 13, 2023, 7:13pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
