Keypad With Arduino Without Using Keypad Library (First Tutorial)




Posted in TechnologyArduino

Introduction: Keypad With Arduino Without Using Keypad Library (First Tutorial)

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)
3- 4x4 matrix keypad

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:

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.



    • Space Challenge

      Space Challenge
    • Science of Cooking

      Science of Cooking
    • Spotless Contest

      Spotless Contest

    We have a be nice policy.
    Please be positive and constructive.




    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


    #include "Keypad.h"

    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[15];

    byte index = 0;

    char numbers [16]; // 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]

    // command for library forkeypad

    //initializes an instance of the Keypad class

    Keypad kpd= Keypad(makeKeymap(keymap), rPins, cPins, Rows, Cols);

    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



    hello Omar,

    how does the keypad function works? thanks in advance your reply will be very much appreciated

    1 reply

    oh! there's no need for a response Sir. i figured it out. thank you so much for the code :D

    why do you put return 50 at the end of the keypad function? thanks in advance.

    5 replies

    just "dummy number" as indication no button pressed and you welcome ☺

    Thanks for your answer Omar. I've tried changing that number and received different behaviours. My conclusion was that i have to put a number bigger than the last number of the list of switch (bigger than 8).. very funny. Thank you very much Omar!

    yes yes you are right i forgot to mention this in previous comment and it must be greater than 15 not 8, you welcome

    Yes you are right Omar! Thank you very much for the fast answer. I said 8 cause i coded a 3x3 keypad. Your code is perfect and its a good example for inputs using the pull up resistor build up in the micro.

    You welcome any time, thank you ?

    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


    My Keypad.jpg

    Hello Again,

    I figured it out. Part of the code didn't paste properly and I just reversed the wiring on the rows and columns and it works like a treat. Thanks again for the code....

    1 reply

    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


    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 for sharing, would you mind posting the code here in instructables? the link provided is full of adware and hijack your browser.

    Thanks in advance

    1 reply

    Here it is:
    //code of Keypad With Arduino Without Using Keypad library (First Tutorial)

    /*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


    if (digitalRead(Input[x])==HIGH); //read evry input if high continue else break;



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







    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


    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


    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


    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




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




    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:



    case 1:



    case 2:



    case 3:



    case 4:



    case 5:



    case 6:



    case 7:



    case 8:



    case 9:



    case 10:



    case 11:



    case 12:



    case 13:



    case 14:



    case 15:








    I have made a simple technique to connect an 4*4 button pad to arduino using four digital pins of arduino instead of 8. Read more at :