Problem using 3x4 keypad

Hello all
I'm interfacing a 3x4 keypad with a NodeMCU with the following connections:

And here is my code:

#include "ESP8266HTTPClient.h"
#include "ESP8266WiFi.h"
#include "Keypad.h"
#include "Wire.h"
#include <LiquidCrystal_I2C.h>

const byte n_rows = 4;
const byte n_cols = 3;
 
char keys[n_rows][n_cols] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};

byte colPins[n_cols] = {D6, D8, D4};
byte rowPins[n_rows] = {D7, D3, D3, D5};
 
Keypad myKeypad = Keypad( makeKeymap(keys), rowPins, colPins, n_rows, n_cols);

// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup()
{
  lcd.begin();
}

void loop()
{
  char myKey = myKeypad.getKey();
 
  if (myKey != NULL){
      lcd.print(myKey);
      delay(500);
  }
  lcd.clear();
}

The strange problem is that when I press "2" key, the output is "1" and when I press "0" key I get "*"! I try disconnecting the keypad and short connect the corresponding pins the see if it is the keypad problem or what but I got the same output! Another tip is that when I press these two keys with right key of them ("1" with "3" and "0" with "#") I get the correct output!

Any ideas about this problem?

D7, 3, D3, D5 ?

(deleted)

byte colPins[n_cols] = {D6, D8, D4};

byte rowPins[n_rows] = {D7, 3, D3, D5};

Try the following declarations:

byte colPins[n_cols] = {D6, D8, D4}; //Col 0, Col 1, Col 2; from left-to-right
byte rowPins[n_rows] = {D7, D2, D3, D5};  //Row 0, Row 1, Row 2; Row 3 from left-to-right

Thank you guys. Change my code to following, worked:

char keys[n_rows][n_cols] = {
  {'2','1','3'},
  {'5','4','6'},
  {'8','7','9'},
  {'0','*','#'}
};

byte colPins[n_cols] = {D8, D6, D4};
byte rowPins[n_rows] = {D7, 3, D3, D5};

But I'm still wondering why this issue happens?!

(deleted)

xubin:
Thank you guys. Change my code to following, worked:

You should not change the layout of your Keypad to make it working. Follow logical reasoning and then try to fix the problem. What is 3? This is not available in your following diagram.
keyQ.jpg

keyQ.jpg

GolamMostafa:
You should not change the layout of your Keypad to make it working. Follow logical reasoning and then try to fix the problem. What is 3? This is not available in your following diagram.
keyQ.jpg

OOps! I updated the code, it is D3 in my code, but it was not working correct till I changed the layout!

xubin:
OOps! I updated the code, it is D3 in my code, but it was not working correct till I changed the layout!

You have changed the layout, and it works; that does not mean that the problem is solved. Keep the layout as it is and then try to make it works; only then your problem is solved.

Do you have the following keypad (Fig-1) which is fully compatible with the Keypad.h Library. If you have, use this Keypad and make the system working and then do experiment with your custom Keypad.
4x4KeypadX.jpg
Figure-1:

4x4KeypadX.jpg