Keypad With Arduino Without Using Keypad Library (First Tutorial)

91,369

58

18

Posted

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:

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.

Share

Recommendations

  • Epilog Challenge 9

    Epilog Challenge 9
  • Gluten Free Challenge

    Gluten Free Challenge
  • First Time Author Contest 2018

    First Time Author Contest 2018
user

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

Tips

Questions

17 Comments

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

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.

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

jessey

My Keypad.jpg