loading

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:

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.

<p>hello Omar,</p><p>how does the keypad function works? thanks in advance your reply will be very much appreciated</p>
<p>oh! there's no need for a response Sir. i figured it out. thank you so much for the code :D</p>
<p>why do you put return 50 at the end of the keypad function? thanks in advance.</p>
just &quot;dummy number&quot; as indication no button pressed and you welcome ☺
<p>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! </p>
<p>yes yes you are right i forgot to mention this in previous comment and it must be greater than 15 not 8, you welcome </p>
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.
<p>You welcome any time, thank you ?</p>
<p>Hey Omar,</p><p>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...</p><p>Thanks Again</p><p>jessey</p>
<p>Hello Again,</p><p>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....</p>
<p>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 </p>
<p>Hi,<br>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 <br>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 <br>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<br>will be appreciated. Maybe I have my keypad wired wrong?<br>Thanks<br>jes</p>
<p>cool thanks</p>
<p>Thanks for sharing, would you mind posting the code here in instructables? the link provided is full of adware and hijack your browser.</p><p>Thanks in advance</p>
<p>Here it is:<br>//code of Keypad With Arduino Without Using Keypad library (First Tutorial)</p><p>/*this keypad tutorial number one by omar tarek 29-06-2014 */</p><p>byte h=0,v=0; //variables used in for loops</p><p>const unsigned long period=50; //little period used to prevent error</p><p>unsigned long kdelay=0; // variable used in non-blocking delay</p><p>const byte rows=4; //number of rows of keypad</p><p>const byte columns=4; //number of columnss of keypad</p><p>const byte Output[rows]={2,3,4,5}; //array of pins used as output for rows of keypad</p><p>const byte Input[columns]={6,7,8,9}; //array of pins used as input for columnss of keypad</p><p>byte keypad() // function used to detect which button is used</p><p>{</p><p>static bool no_press_flag=0; //static flag used to ensure no button is pressed</p><p>for(byte x=0;x&lt;columns;x++) // for loop used to read all inputs of keypad to ensure no button is pressed</p><p>{</p><p>if (digitalRead(Input[x])==HIGH); //read evry input if high continue else break;</p><p>else</p><p>break;</p><p>if(x==(columns-1)) //if no button is pressed</p><p>{</p><p>no_press_flag=1;</p><p>h=0;</p><p>v=0;</p><p>}</p><p>}</p><p>if(no_press_flag==1) //if no button is pressed</p><p>{</p><p>for(byte r=0;r&lt;rows;r++) //for loop used to make all output as low</p><p>digitalWrite(Output[r],LOW);</p><p>for(h=0;h&lt;columns;h++) // for loop to check if one of inputs is low</p><p>{</p><p>if(digitalRead(Input[h])==HIGH) //if specific input is remain high (no press on it) continue</p><p>continue;</p><p>else //if one of inputs is low</p><p>{</p><p>for (v=0;v&lt;rows;v++) //for loop used to specify the number of row</p><p>{</p><p>digitalWrite(Output[v],HIGH); //make specified output as HIGH</p><p>if(digitalRead(Input[h])==HIGH) //if the input that selected from first sor loop is change to high</p><p>{</p><p>no_press_flag=0; //reset the no press flag;</p><p>for(byte w=0;w&lt;rows;w++) // make all outputs as low</p><p>digitalWrite(Output[w],LOW);</p><p>return v*4+h; //return number of button</p><p>}</p><p>}</p><p>}</p><p>}</p><p>}</p><p>return 50;</p><p>}</p><p>void setup()</p><p>{</p><p>for(byte i=0;i&lt;rows;i++) //for loop used to make pin mode of outputs as output</p><p>{</p><p>pinMode(Output[i],OUTPUT);</p><p>}</p><p>for(byte s=0;s&lt;columns;s++) //for loop used to makk pin mode of inputs as inputpullup</p><p>{</p><p>pinMode(Input[s],INPUT_PULLUP);</p><p>}</p><p>Serial.begin(9600); //to use serial monitor we set the buad rate</p><p>}</p><p>void loop()</p><p>{</p><p>if(millis()-kdelay&gt;period) //used to make non-blocking delay</p><p>{</p><p>kdelay=millis(); //capture time from millis function</p><p>switch (keypad()) //switch used to specify which button</p><p>{</p><p>case 0:</p><p>Serial.println(1);</p><p>break;</p><p>case 1:</p><p>Serial.println(2);</p><p>break;</p><p>case 2:</p><p>Serial.println(3);</p><p>break;</p><p>case 3:</p><p>Serial.println(&quot;F1&quot;);</p><p>break;</p><p>case 4:</p><p>Serial.println(4);</p><p>break;</p><p>case 5:</p><p>Serial.println(5);</p><p>break;</p><p>case 6:</p><p>Serial.println(6);</p><p>break;</p><p>case 7:</p><p>Serial.println(&quot;F2&quot;);</p><p>break;</p><p>case 8:</p><p>Serial.println(7);</p><p>break;</p><p>case 9:</p><p>Serial.println(8);</p><p>break;</p><p>case 10:</p><p>Serial.println(9);</p><p>break;</p><p>case 11:</p><p>Serial.println(&quot;F3&quot;);</p><p>break;</p><p>case 12:</p><p>Serial.println(&quot;Mode&quot;);</p><p>break;</p><p>case 13:</p><p>Serial.println(0);</p><p>break;</p><p>case 14:</p><p>Serial.println(&quot;Cancel&quot;);</p><p>break;</p><p>case 15:</p><p>Serial.println(&quot;Enter&quot;);</p><p>break;</p><p>default:</p><p>;</p><p>}</p><p>}</p><p>}</p>
<p>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 :</p><p>http://www.haberocean.com/2015/05/circuit-for-44-button-keypad-using-74165-and-74595-part-1/</p>

About This Instructable

82,726views

57favorites

License:

More by Omar Tarek:Keypad With Arduino Without Using Keypad library (First Tutorial) 
Add instructable to: