arduino keypad not working

I'm trying to use an Arduino UNO to read which button I push on a 4x4 matrix keypad, then output it on an LCD screen. However, I can't get the Arduino to read which button is pressed. Can someone please help me? I've started over with the code from the keypad tutorial with no luck and haven't found a similar example.

When I upload the code to the arduino controller, there are 5 volts on the row pins of the keypad and 0-0.5 volts on the column pins. Pushing the buttons will cause 5 volts to show up on the individual pins of the corresponding buttons, but the arduino still doesn't recognize it to output it to the LCD screen. Do I need to hold the button? or push it repeatedly for the getkey() function to work? do I need to put a delay in somewhere?

Please help...

Here is my code: (I've swapped keypads and reduce the matrix size in an attempt to do some troubleshooting)

#include <Key.h>
#include <Keypad.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const byte ROWS = 2; //four rows
const byte COLS = 2; //three columns
char keys[ROWS][COLS] = {
{'1','2'},
{'7',''},
// {'7','8','9'},
//{'#','0','
'}
};

byte rowPins[ROWS] = {6, 7}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 10}; //connect to the column pinouts of the keypad

//Create the Keypad
Keypad slcKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){

Serial.begin(9600);

// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("buttons");

}

void loop(){

char key2 = slcKeypad.getKey();

// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
lcd.print(key2);

if (key2 == NO_KEY){
lcd.print("agitated");
}

}

A 4 by 4 keypad has 4 rows and 4 columns. Your rowPins and colPins arrays are too small.

How did you determine which pins to connect the keypad to?

Also:
Please use code tags. Use the </> icon in the posting menu. [code] Paste sketch here. [/code]

Thank you for responding so quickly! The size of the matrix should not be the issue. As I mentioned above, I reduced the size of the matrix so I could troubleshoot easier. I've bumped my matrix size back up to a 3x3 (only connected the first 3 row pins and first 3 column pins on the 4x4 keypad). I'm not sure I can use a 4x4 with the LCD plugged in. I only have 16 pins and I read somewhere that I can't use pins 1 and 2 because of the TX/RX capability (but I'm not sure if this applies to me. I wanted to get part of the keypad working first, then I'd go back and see if I can use pins 1 & 2).

LarryD, is this what you mean by using the code tags?

#include <Key.h>
#include <Keypad.h>
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const byte ROWS = 3; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
   {'1','2','3'},
   {'4','5','6'},
   {'7','8','9'}
};

 byte rowPins[ROWS] = {6, 7, 8}; //connect to the row pinouts of the keypad
 byte colPins[COLS] = {9, 10, 13}; //connect to the column pinouts of the keypad

//Create the Keypad
 Keypad slcKeypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );



void setup(){

   Serial.begin(9600);

   // set up the LCD's number of columns and rows:
   lcd.begin(16, 2);
   // Print a message to the LCD.
   lcd.print("buttons");
   
}



void loop(){
  
   char key2 = slcKeypad.getKey();

   // set the cursor to column 0, line 1
   // (note: line 1 is the second row, since counting begins with 0):
   lcd.setCursor(0, 1);
   lcd.print(key2);

   if (key2 == NO_KEY){
   lcd.print("agitated");
}
     
   
}

PaulS,

The pins I used for the keypad are just what was left over after I had the LCD display working.

The question was how did you determine which wires coming from the keypad to connect to which row pins and which column pins.

Put the LCD in a box, in a drawer, in another room, in another house, in another city, in another country until you KNOW that the keypad is wired and working correctly.

Please confirm with one of the example library keypad sketches that your keypad is wired properly.
Please confirm with one of the example library LCD sketches that your LCD is wired properly.

Then:
Since loop() runs very fast, you are not going to see what you think you will, in the sketch you attached.
You are updating the LCD too often.
Remember, you first have to erase old text on a LCD line if new text is being written.

Edit:
you don't need this #include <Key.h>

After you have confirmed wiring.
Maybe some like this:

  char key2 = slcKeypad.getKey();

  if (key2 != NO_KEY)
  {
    lcd.setCursor(0, 1);
    lcd.print(key2);
    //         123456789111111
    //                  012345
    lcd.print("               ");
    delay(1000);
  }
  else 
  {
    lcd.setCursor(0, 1);
    lcd.print("agitated");
  }

Just a quick note to let you know that the keypad and LiquidCrystal libraries were written so they can share pins.

You can connect the keypad row pins and the LCD data pins together on the same Arduino pins.

I'm not sure about the LiquidCrystal library but the Keypad library will NOT work with the two serial pins.

PaulS,

I determined the pinout of the keypad by holding a button and then using a multimeter to check connectivity between the pins. (i.e. I held button #1, and then moved by multimeter leads from pins 1 & 2, to 1 & 3, to 1 & 4... to 1 & 8. If there was not connectivity I moved to pins 2&3, 2&4... 2&8; 3&4, 3&5... up to 7&8 or until I found the pins that the button connected. In the end I had the following:

column pins -> 1 2 3 4
row pins V
5 [1] [2] [3] [A]
6 [4] [5] [6]
** 7 [7] [8] [9] **
```c
**                8    [*]  [0]  [#]  [D]

I have the LCD board pulled off, but how do I determine if the keypad is working if I don't have the LCD display to print to?**
```

I have the LCD board pulled off, but how do I determine if the keypad is working if I don't have the LCD display to print to?

Serial.print() comes to mind, if you are not using the hardware serial pins for the keypad. If you are, the author of the library, in another post this morning, said that the keypad library will not work with the hardware serial pins.

Did you try the examples that came with both libraries to prove your wiring?

.

I didn't know where the serial.print function was printing to (text file, pop-up window, etc, so I couldn't use it... but I figured it out...'Tools>>Serial Monitor' :slight_smile:

I pulled off the LCD screen and plugged the 4x4 keypad into the pins listed in the tutorial. Then half of the board started working. A couple keypad pins needed to be re-soldered as they were being flaky(worked then didn't work, then worked again, etc). Now the whole keypad appears to be working .

However, before I try re-connecting the LCD display and possibly doubling up on pins for the LCD and keypad, I'd like to make sure I can enter a floating decimal point number using the keypad that I can do math with. for example I'd like to enter the number "10.25" or "22.5" and be able to multiply/add that number.

I found an example that's really close to what I'd like to do, but it doesn't seem to work for float numbers?

https://forum.arduino.cc/index.php?topic=57627.0

But my original issue I started this post for appears to have been resolved for now... until I start working with the LCD display again.

mstanley:
Just a quick note to let you know that the keypad and LiquidCrystal libraries were written so they can share pins.

You can connect the keypad row pins and the LCD data pins together on the same Arduino pins.

I'm not sure about the LiquidCrystal library but the Keypad library will NOT work with the two serial pins.

mstanley,

I've gotten the keypad and LCD displays to work, and even share the row and data pins as you mentioned above. However, my LCD prints fine up until the "char key = Keypad.getKey();" command. After it's gone through that code all I can get the LCD to print out is blocks instead of letters/numbers. It's printing out the right number of characters, but they all show up as blocks. Would you have any ideas as to what might cause this?

I found an example that's really close to what I'd like to do, but it doesn't seem to work for float numbers?

Is that a question or a statement?

You could store each character entered in the next position in an array, followed by a NULL. Then, when you know that the end of the number has been entered, use atof() to convert the array to a float.

byte index = 0;
char numStg[20];



   if(key != NO_KEY)
   {
      if(key >= '0' && key <= '9' || key == '.' || (key == '-' && index == 0))
      {
         numStg[index++] = key;
         numStg[index] = '\0';
      }
      else if(key == /* whatever the "I'm done" value is */)
      {
         numVal = atof(numStg);
      }
   }

PaulS:
Is that a question or a statement?

It was supposed to be a statement but since I'm a bit of a newb at this I guess I was also asking to make sure my statement wasn't incorrect.

PaulS:
You could store each character entered in the next position in an array, followed by a NULL. Then, when you know that the end of the number has been entered, use atof() to convert the array to a float.

byte index = 0;

char numStg[20];

if(key != NO_KEY)
  {
      if(key >= '0' && key <= '9' || key == '.' || (key == '-' && index == 0))
      {
        numStg[index++] = key;
        numStg[index] = '\0';
      }
      else if(key == /* whatever the "I'm done" value is */)
      {
        numVal = atof(numStg);
      }
  }

Thank you for this! I'll have to try it out in a bit. First I'd like to figure out why my LCD only works part of the time per my statement just before your last. Should I start a new thread for that since its a different issue?

If, without the keypad sharing pins with the LCD, it displays valid data, and, with the keypad sharing pins with the LCD, it does not, then I would think that mstanley assertion that such a scenario is possible is not valid.