Keypad without keypad library

Hello,

I tried creating a code for a 4X4 keypad without the keypad library. Unfortunately it doesn't perfectly work. I'm sure the witring is correct and it is normal if my pins are not in INPUT_PULLUP because I wired real resistors in my wiring.
The buttons 4,5,6,B and *,#,D work perfectly. The buttons 7,8,9,C work weird beacause they always display respectively 1,2,3,A. And finally, the buttoons 1,2,3,A seems to work well but in fact, when Ipress them a short time, they display respectively 7,8,9,C.

I hope you cold help me,

Quintus

keypad_without_librairies.ino (1.62 KB)

Hello,

I tried creating a code for a 4X4 keypad without the keypad library. Unfortunately it doesn't perfectly work. I'm sure the witring is correct and it is normal if my pins are not in INPUT_PULLUP because I wired real resistors in my wiring.
The buttons 4,5,6,B and *,#,D work perfectly. The buttons 7,8,9,C work weird beacause they always display respectively 1,2,3,A. And finally, the buttoons 1,2,3,A seems to work well but in fact, when Ipress them a short time, they display respectively 7,8,9,C.

I hope you cold help me,

Quintus

const int pinCols[4]={30,32,34,36};     //pins of the differents columns
const int pinRows[4]={22,24,26,28};     //pins of the differents rows

int trueCols;
int trueRows;

char keys[4][4]={                  //correspondence with the keys of the keypad
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

void setup() {
  Serial.begin(9600);
  
  for(int i=0;i<4;i++){               //defining the output and input pins
    pinMode(pinCols[i], OUTPUT);
    pinMode(pinRows[i],  INPUT);
    digitalWrite(pinCols[i], HIGH);
    }
}

void loop() {
  readKey();
  delay(100);
}

void readKey(){
  for(int i=0; i<4;i++){
    if(digitalRead(pinRows[i])==HIGH){      //reading if a row is active
      trueRows=i;                           //so the right row is the one how is high
      for(int x=0; x<4; x++){
        digitalWrite(pinCols[0], LOW);      //putting all the cols low
        digitalWrite(pinCols[1], LOW);
        digitalWrite(pinCols[2], LOW);
        digitalWrite(pinCols[3], LOW);
        digitalWrite(pinCols[x], HIGH);     //except the one we want to test
        if(digitalRead(pinRows[trueRows])==HIGH){ //if now the row is high when a special column is high, we know the button how is pressed
          trueCols=x;
          Serial.println(keys[trueRows][trueCols]); //we dispalay the right symbol
          }
        }
      }
  }
  digitalWrite(pinCols[0], HIGH);           //re-putting the cols to high position to be ready for an upcoming reading 
  digitalWrite(pinCols[1], HIGH);
  digitalWrite(pinCols[2], HIGH);
  digitalWrite(pinCols[3], HIGH);
}

Duplicate topics merged

Why did you create a duplicate ?

first I posted a topic with the code in link and then I decided to remake a post with the code directly in the description. That is why there was 2 posts

Please do not do that again

You could have just added a post to the first one.

In a matrix keyboard (without diodes - I'm assuming you didn't use any) never write pins HIGH and LOW at the same time. That's a sure way to make a short.

If you are using pull-up resistors:
Write LOW only
For columns you are not scanning, switch them to inputs and let the pull-ups pull those pins HIGH

If you are using pull-down resistors:
Write HIGH only
For columns you are not scanning, switch them to inputs and let the pull-downs pull those pins LOW

This is why you should use the internal pull-ups. It saves a lot of effort and extra resistors...
On the down side you have to deal with the inverse logic (LOW = ON). Not really that onerous once you get your head round it.

I'm assuming you have pull-downs on rows AND columns...

const int pinCols[4]={30,32,34,36};     //pins of the differents columns
const int pinRows[4]={22,24,26,28};     //pins of the differents rows

int trueCols;
int trueRows;

char keys[4][4]={                  //correspondence with the keys of the keypad
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

void setup() {
  Serial.begin(9600);
  
  for(int i=0;i<4;i++){               //defining the output and input pins
    pinMode(pinCols[i], OUTPUT);
    pinMode(pinRows[i],  INPUT);
    digitalWrite(pinCols[i], HIGH);
    }
}

void loop() {
  readKey();
  delay(100);
}

void readKey(){
  for(int i=0; i<4;i++){
    if(digitalRead(pinRows[i])==HIGH){      //reading if a row is active
      trueRows=i;                           //so the right row is the one how is high
      for(int x=0; x<4; x++){
        pinMode(pinCols[0], INPUT);      //putting all the cols low
        pinMode(pinCols[1], INPUT);
        pinMode(pinCols[2], INPUT);
        pinMode(pinCols[3], INPUT);
        pinMode(pinCols[x], OUTPUT);     //except the one we want to test
        if(digitalRead(pinRows[trueRows])==HIGH){ //if now the row is high when a special column is high, we know the button how is pressed
          trueCols=x;
          Serial.println(keys[trueRows][trueCols]); //we dispalay the right symbol
          }
        }
      }
  }
  pinMode(pinCols[0], OUTPUT);           //re-putting the cols to high position to be ready for an upcoming reading 
  pinMode(pinCols[1], OUTPUT);
  pinMode(pinCols[2], OUTPUT);
  pinMode(pinCols[3], OUTPUT);
}

If that doesn't work then I expect you have a wiring problem.

Re-writing, I'd have perhaps gone with...

const int pinCols[4]={30,32,34,36};
const int pinRows[4]={22,24,26,28};

char keys[4][4]={
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};

void setup() {
  Serial.begin(9600);
  
  for(int i=0; i<4; i++) {
    pinMode(pinCols[i], INPUT);
    pinMode(pinRows[i], INPUT);
    digitalWrite(pinCols[i], HIGH);
  }
}

void loop() {
  readKey();
  delay(100);
}

void readKey(){
  for (int c=0; c<4; c++) {
    pinMode(pinCols[c], OUTPUT);
    for(int r=0; r<4; r++){
      if (digitalRead(pinRows[r]) == HIGH) {
        Serial.println(keys[r][c]); //we dispalay the right symbol
      }
    }
    pinMode(pinCols[c], INPUT);
  }
}

Thanks a lot pcbbc, you are my saver. My code was good even if he was long in comparison with yours :wink:
I only put pull down rsistors for the rows and not for the columns.
Thank you to have taked time to answer.

Quintus

quintus_magnus:
Thanks a lot pcbbc, you are my saver. My code was good even if he was long in comparison with yours :wink:

NP. You are welcome.
Probably too many years for me writing code.... :wink:

I only put pull down resistors for the rows and not for the columns.

Probably that will work as you are not reading from them, but it will leave the column pins that are switched to input floating, and that is never such a good idea.