sunrays

Just another WordPress.com site

A Keypad for the Arduino!

on April 10, 2012

After the LCD Display was connected and working, I decided to connect a keypad to the Arduino.

Mark Stanley and Alexander Brevig have developed an entire library for keypads with functions and examples neatly listed out. All you have to do is download it. The tutorial on “How to Connect the Keypad to the Arduino” looks very simple and straightforward too. So, well armed with the theory and a 3X4 matrix keypad from Element14, I got down to work.

3x4 Matrix Keypad from Element14

3x4 Matrix Keypad from Element14

That’ how my keypad looks. It has 12 keys arranged in a 3X4 matrix. What that basically means is that we need only 7 pins to access the 12 keys. For example, from the connection diagram (below), one can see that if the key ‘1’ is pressed, column 1 and row 1 are connected. Similarly, pressing ‘5’ connects column 2 and row 2. So by looking for the combination of row and column connected, you know which key is pressed. The code does all this by itself, so that is not our worry.

Connection Matrix

Connection Matrix

In my case, I hooked up the keypad output pins from 1 -7 to Arduino digital pins 15 – 21 so the connections are like this:

Pin Connections

Pin Connections

The code for declaring the keypad looks like this:

/**********************************************************************/
// start of creating the keypad//
const byte ROWS = 4; //Four rows//
const byte COLS = 3; //Three columns//
char keys[ROWS][COLS] =
{
{‘1′,’2′,’3’},
{‘4′,’5′,’6’},
{‘7′,’8′,’9’},
{‘*’,’0′,’#’}
}; // Define the Keymap//
byte rowPins[ROWS] = { 20, 15, 16, 18 };// Connect keypad ROW1, ROW2, ROW3 and ROW4 to these Arduino pins//
byte colPins[COLS] = { 19, 21, 17 }; // Connect keypad COL1, COL2 and COL3 to these Arduino pins//
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );//end of creating the Keypad//
/**********************************************************************/

(The entire code for printing a punched key onto the LCD Diplay is at the end of this post.)

But then, things never work at the first go.

Instead of getting numbers printed on my LCD, I got two vertcical lines ‘||’.

Irrespective of which key i pressed, I got this – ‘||’ displayed on the LCD. Also, I noticed, it was not able to differentiate which key was being pressed – only the key punches were being detected. I changed the char key in the code to int key and started getting ‘0’ for everything.

After searching and hunting for hours, I realised I had missed an important step – resistors. The Arduino mentioned “…no external diodes or resistors are needed…” so I had given this no thought (I am not saying I understood what they said. I just skipped over it after these words. Oops!). Turns out, resistors need to be connected to either on the row pins or the column pins.

Instead of attaching external resistors, I used the internal pull-up resistors of the Arduino itself.

/**********************************************************************/

pinMode (21, INPUT);//Column 1
pinMode (19, INPUT);//Column 2
pinMode (17, INPUT);//Column 3
digitalWrite(21, HIGH);//Pull up resistor of Arduino
digitalWrite(19, HIGH);// Pull up resistor of Arduino
digitalWrite(17, HIGH);// Pull up resistor of Arduino

/**********************************************************************/

Just this simple step and yay! I had numbers on my LCD!

Numbers on the LCD!

Numbers on the LCD!

Complete code for printing keys on LCD:

/**********************************************************************/

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

// start of creating the keypad//
const byte ROWS = 4; //Four rows//
const byte COLS = 3; //Three columns//
char keys[ROWS][COLS] =
{
{‘1′,’2′,’3’},
{‘4′,’5′,’6’},
{‘7′,’8′,’9’},
{‘#’,’0′,’*’}
}; // Define the Keymap//
byte rowPins[ROWS] = { 20, 15, 16, 18 };// Connect keypad ROW1, ROW2, ROW3 and ROW4 to these Arduino pins//
byte colPins[COLS] = { 19, 21, 17 }; // Connect keypad COL1, COL2 and COL3 to these Arduino pins//
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );//end of creating the Keypad//

// LCD Display//
LiquidCrystal lcd(30, 31, 32, 33, 34, 35); //LiquidCrystal lcd(RS, E, D4, D5, D6, D7), initialize the library with the numbers of the interface pins//
void setup()
{
lcd.begin(20,4); //20 columns and 4 rows in the lcd display//
pinMode (21, INPUT);//Column 1
pinMode (19, INPUT);//Column 2
pinMode (17, INPUT);//Column 3
digitalWrite(21, HIGH);//Pull up resistor of Arduino
digitalWrite(19, HIGH);// Pull up resistor of Arduino
digitalWrite(17, HIGH);// Pull up resistor of Arduino
lcd.setCursor(0,0);
lcd.print(“The Keypad Test”);
delay(1000);
lcd.setCursor(0, 1);
}

void loop()
{
char key = keypad.getKey();
if (key != NO_KEY)
{
lcd.print(key);
delay(500);
}
}
/**********************************************************************/

Advertisements

8 responses to “A Keypad for the Arduino!

  1. Ana Mariano says:

    can you post a tutorial like this but instead of using LCD Display, use a bluetooth shield and android phone

    • sunraysme says:

      Hey! Really sorry I havent worked with the bluetooth shield. but do try the arduino forum. They have a lot of help and are very quick in replying. http://arduino.cc/forum/

      • Ana Mariano says:

        Thanks for replying. I was able to modify your code but is it also possible to have an alphabet input for example, when you press 2 for the first time it will have an output of “a” in the lcd?the second time it is pressed, the output will be “b”. something like that

      • sunraysme says:

        As I see it, the library is made such that each key can only have one character to it. You can make 1 be ‘a’ and 2 be ‘b’ and so on but the library won’t cater to the multiple presses. you’ll have to pick up an alphanumeric keypad.

  2. Kent says:

    OMG thank you so much! Your internal pull-up resistor tip works! I have a 4×4 keypad; its first column and last row didn’t work for whatever I tried, until I found your blog.

  3. Christian says:

    can you change your tutorial so that instead of using LCD Displa you use a SD Breakout Module where the keypad information is directly then saved?

    • sunraysme says:

      Hi! I don’t really have the Arduino anymore so I wouldn’t be able to put up a tutorial I am sure of 😦 I hoep the Arduino forums help you on that front!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: