How to use simple keypad with Arduino

Submitted by CAAadmin on Sun, 07/24/2016 - 07:12
arduino keypad

In this example, we will use Simple Keypad to send the data to arduino when someone presses the keys

Connect keypad leads to 9,8,7,6 ,5,4,3,2 of arduino Digital IO pins (Right Most pin to Arduino D I/O 2 and so on)

In this case, the variable key1 returns the value equivalent to the key pressed. this variable can be used to sent the values to VB interface (which will be explained in next article)

These codes are tried and tested - without any errors, if any error occurs please report it under comments section.

Arduino Code


#include <Keypad.h>
const byte numRows= 4;
const byte numCols= 4;
char keymap[numRows][numCols]=
{
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
byte rowPins[numRows] = {9,8,7,6};
byte colPins[numCols]= {5,4,3,2};
Keypad myKeypad= Keypad(makeKeymap(keymap), rowPins, colPins, numRows, numCols);
void setup()
{
Serial.begin(9600);
}

void loop()
{
char key1 = myKeypad.getKey();
if ( key1 != NO_KEY)
{
Serial.print( key1);
}
}

Arduino keypad library files can be downloaded at https://drive.google.com/file/d/0B-H_CXR7-MD9MEFXaUZvX2NvVGc/view?usp=sharing

Tags