102,179

58

17

Keypad used usually in most of projects to enable customer to deal with project such as (Real Time Clock (RTC) to adjust day,time ,access control,.....etc) but if you use it with keypad library of Arduino it's so easy and not good so here we will learn how to use it with Arduino without using keypad library in 3 Tutorials

## Step 1: Components

all components we need are :
1- Arduino Uno Or Mega
2- 8 x Arduino Connection Wires (Male-Male)

## Step 2: Calculation of Index of Any Element in 4x4 Matrix

if we look to the above picture we obtain that index of any element is equal to
index=Number of Row x4 +Number Of Column

## Step 3: Connection

First Here Is The Pin configuration of keypad ....
R .... indicates the Row
C .... indicates the Column

Second Connect Keypad To Arduino As Following

Keypad Pin R1 –> Arduino Pin 2

Keypad Pin R2 –> Arduino Pin 3

Keypad Pin R3 –> Arduino Pin 4

Keypad Pin R4 –> Arduino Pin 5

Keypad Pin C1 –> Arduino Pin 6

Keypad Pin C2 –> Arduino Pin 7

Keypad Pin C3 –> Arduino Pin 8

Keypad Pin C4 –> Arduino Pin 9

## Step 4: Upload the Code and Explaining

Here The code in the link below:

http://sh.st/TwENk

To illustrate the code i will explain it in few lines:
the following instructions are repeatedly executed every 50ms first it check if no button is pressed if that's true the no press flag will be set to ONE and counters h and v will be reset to ZERO then all outputs will be LOW then check all inputs if any one of them is LOW( h became the number of column) a for loop changes first output to HIGH if the input Which Was LOW is change to HIGH That's Mean that's is the correct row of ( v became the number of row) then make all output as low to disable an pressed until you released the pressed button Reset The no press flag to ZERO and calculate the index and return it while you hold any button the no press flag is ZERO and the function return 50 and switch case used to output on serial monitor according to the return form keypad function.

## Recommendations

Hi, all.

I just want share codes that can read float number from keypad and display on serial monitor. C is used to clear the data entered. Here you go.

/* the tutorial code for 3x4 Matrix Keypad with Arduino is as

This code prints the key pressed on the keypad to the serial port*/

//The program keys in float number and can be clear with C button to clear data. This is a working program

/////////////////////////////////////////////////////////////////////////////////////////////

const byte Rows= 4; //number of rows on the keypad i.e. 4

const byte Cols= 4; //number of columns on the keypad i,e, 4

static char outstr;

byte index = 0;

char numbers ; // Plenty to store a representation of a float

int key;

float len;

//we will definne the key map as on the key pad:

char keymap[Rows][Cols]=

{

{'1', '2', '3', 'A'},

{'4', '5', '6', 'B'},

{'7', '8', '9', 'C'},

{'.', '0', '#', 'D'}

};

// a char array is defined as it can be seen on the above

//keypad connections to the arduino terminals is given as:

byte rPins[Rows]= {9,8,7,6}; //Rows 0 to 3 connect to digital pins [Pins 8, 7, 6, 5 for row ]

byte cPins[Cols]= {5,4,3,2}; //Columns 0 to 3 [Pins 4, 3, 2, 1 for column]

//initializes an instance of the Keypad class

void setup()

{

Serial.begin(9600); // initializing serial monitor

}

//If key is pressed, this key is stored in 'keypressed' variable

//If key is not equal to 'NO_KEY', then this key is printed out

void loop()

{

char key = kpd.getKey();

if (key != NO_KEY)

{

if (key == 'C') //clear the array

{

index = 0;

numbers[index] = '\0';

}

else if (key == '.')

{

numbers[index++] = '.';

numbers[index] = '\0';

}

else if (key >= '0' && key <= '9')

{

numbers[index++] = key;

numbers[index] = '\0';

}

else if (key == '#')

{

float len = atof(numbers); //conversion from string to numeric

dtostrf(len, 7, 2, outstr); // print out on serial monitor as static char

index = 0;

numbers[index] = '\0';

/*The dtostrf() function converts the float value (len) passed in val into an ASCII representation that will be stored under outstr.

The caller is responsible for providing sufficient storage in outstr*/

/*char * dtostrf (double __val, signed char __width, unsigned char __prec, char *__s)*/

}

Serial.println(numbers); //print out on serial monitor

}

}

Hey Omar,

I thought I'd include these pictures of the keypad I'm using so If anyone has the same keypad as mine then they can use these pictures here (that I modified from yours) as a guide to wire theirs up and it should work with your code. And your code also works on a nano without any modifacations...

Thanks Again

jessey

alright it's awesome to hear that, and for the picture of keypad the keypad which i use it in this Instructable is like in attached image, thank you :D

Hi,
Thanks for the code but I can't seem to get it working on my Arduino Nano. I would really appreciate some help to figure out what I'm doing wrong. I hooked up
the keypad the same as it shows in your code using your pictures showing the rows and columns and corresponding pin numbers for the UNO. My Arduino Nano has the
same pins as the Uno (digital pins 2 to 9 are the same) so I thought it would work without doing anything to the code. I guess maybe I was wrong about something I did? One thing I don't understand is that the 2 pictures you have of the keypad showing R4 to R1 and C4 to C1, the keypad looks backwards, I mean the button lay outs looks upside down and inside out and you show the ribbon cable coming out the opposite end to where they should be according to the ones they sell on ebay? Any feed back
will be appreciated. Maybe I have my keypad wired wrong?
Thanks
jes

Here it is:

/*this keypad tutorial number one by omar tarek 29-06-2014 */

byte h=0,v=0; //variables used in for loops

const unsigned long period=50; //little period used to prevent error

unsigned long kdelay=0; // variable used in non-blocking delay

const byte rows=4; //number of rows of keypad

const byte columns=4; //number of columnss of keypad

const byte Output[rows]={2,3,4,5}; //array of pins used as output for rows of keypad

const byte Input[columns]={6,7,8,9}; //array of pins used as input for columnss of keypad

byte keypad() // function used to detect which button is used

{

static bool no_press_flag=0; //static flag used to ensure no button is pressed

for(byte x=0;x<columns;x++) // for loop used to read all inputs of keypad to ensure no button is pressed

{

else

break;

if(x==(columns-1)) //if no button is pressed

{

no_press_flag=1;

h=0;

v=0;

}

}

if(no_press_flag==1) //if no button is pressed

{

for(byte r=0;r<rows;r++) //for loop used to make all output as low

digitalWrite(Output[r],LOW);

for(h=0;h<columns;h++) // for loop to check if one of inputs is low

{

if(digitalRead(Input[h])==HIGH) //if specific input is remain high (no press on it) continue

continue;

else //if one of inputs is low

{

for (v=0;v<rows;v++) //for loop used to specify the number of row

{

digitalWrite(Output[v],HIGH); //make specified output as HIGH

if(digitalRead(Input[h])==HIGH) //if the input that selected from first sor loop is change to high

{

no_press_flag=0; //reset the no press flag;

for(byte w=0;w<rows;w++) // make all outputs as low

digitalWrite(Output[w],LOW);

return v*4+h; //return number of button

}

}

}

}

}

return 50;

}

void setup()

{

for(byte i=0;i<rows;i++) //for loop used to make pin mode of outputs as output

{

pinMode(Output[i],OUTPUT);

}

for(byte s=0;s<columns;s++) //for loop used to makk pin mode of inputs as inputpullup

{

pinMode(Input[s],INPUT_PULLUP);

}

Serial.begin(9600); //to use serial monitor we set the buad rate

}

void loop()

{

if(millis()-kdelay>period) //used to make non-blocking delay

{

kdelay=millis(); //capture time from millis function

switch (keypad()) //switch used to specify which button

{

case 0:

Serial.println(1);

break;

case 1:

Serial.println(2);

break;

case 2:

Serial.println(3);

break;

case 3:

Serial.println("F1");

break;

case 4:

Serial.println(4);

break;

case 5:

Serial.println(5);

break;

case 6:

Serial.println(6);

break;

case 7:

Serial.println("F2");

break;

case 8:

Serial.println(7);

break;

case 9:

Serial.println(8);

break;

case 10:

Serial.println(9);

break;

case 11:

Serial.println("F3");

break;

case 12:

Serial.println("Mode");

break;

case 13:

Serial.println(0);

break;

case 14:

Serial.println("Cancel");

break;

case 15:

Serial.println("Enter");

break;

default:

;

}

}

}