Go Down

Topic: Help request interfacing a 4x4 keypad with Arduino (Read 10494 times) previous topic - next topic

Palliser

Hey everyone! Here my 1st. post here. I need help writing a code that makes an Arduino UNO to read the 16 keys of a 4x4 matrix keypad (0,1,2,3,4,5,6,7,8,9,A,B,C,D,#,*).

I am trying to find the electric schematic of the keypad but, in the meantime, I got a tested C code example that works for the keypad and a 8051 based board.

Code: [Select]
Code Example

#define MAX_ROWS 4
#define MAX_COLS 4

               
static char KeyTable[] = { '1', '2', '3', 'A',
'4', '5', '6', 'B',
'7', '8', '9', 'C',
'*', '0', '#', 'D'
};

static unsigned char RowTable[] = { 0xFE, 0xFD, 0xFB, 0xF7 };


char ScanKeypad();


main()
{
char key;

serinit(CBR_19200);
printf ("\nKeypad example");

for( ;; )
{
key = ScanKeypad();

if( key )
{
printf( "\nKey: '%c'", key );
}
}
}


char ScanKeypad()
{
char row;
char col;

col = 0;

for( row=0; row<MAX_ROWS; row++ )
{
P2 = RowTable[row];

if( !(P2 & 0x80) )
col = 4;

if( !(P2 & 0x40) )
col = 3;

if( !(P2 & 0x20) )
col = 2;

if( !(P2 & 0x10) )
col = 1;

if( col != 0 )
{
delay(500);
return KeyTable[col-1 + row*MAX_COLS];
}
}

return 0;
}


Thanks in advance for your help.

CrossRoads

Have you seen the keypad.h library in the Playground?
Makes this interface easy to add to a sketch.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Palliser

Hi Crossroads. Yes I've seen the keypad library. I really really need to put to work that particular keypad to do HEX data entry. I will check out a little more the library but at first glance it looked to me more focused to the 4x3 matrix.

PaulS

Quote
I really really need to put to work that particular keypad to do HEX data entry.

What I'm hearing is "I really need someone to tell me what code I need for an undefined 4x4 keypad,because I really can't be bothered figuring it out myself".

The number of rows and columns is not really relevant. The examples that come with the library have to assume some size. If yours is a different size, make the appropriate changes. Yours has more columns, so you need another entry in the cols array.

Of course, only you can determine what that entry should be, since you are the one with the hardware, and you are the only one that knows how you will connect the keypad to the Arduino.

Palliser

I did try to run the following code in my attempt to interface a 4X4 keypad and Arduino uno
and I did get the following errors:

Code: [Select]
#include <Keypad.h>

// 4 X 4 Keypad interfaced with Arduino UNO

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};

byte rowPins[ROWS] = { 5, 4, 3, 2 };
byte colPins[COLS] = { 8, 7, 6, 9 };

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  Serial.begin(9600);
}

void loop(){
  char key = keypad.getKey();

  if (key != NO_KEY){
    Serial.println(key);
  }
}


Quote
UNO_4X4:18: error: 'Keypad' does not name a type
UNO_4X4.cpp: In function 'void loop()':
UNO_4X4:25: error: 'keypad' was not declared in this scope
UNO_4X4:27: error: 'NO_KEY' was not declared in this scope


I did exactly what is said at: http://www.arduino.cc/playground/code/Keypad

Quote
Download, install and import

Download here: keypad.zip

Put the Keypad folder in "arduino\libraries\".
In the Arduino IDE, create a new sketch (or open one) and select from the menubar "Sketch -> Import Library -> Keypad".
Once the library is imported, an "#include <Keypad.h>" line will appear at the top of your Sketch.


My current IDE is arduino-1.0.1. I also checked that there is only one keypad folder and keypad files. Additionally, I found here some posts that brought to light this very same issue but at the end, nothing was clear to me about how to solve it.

Please, I should appreciate the assistance of anyone that RIGHT NOW has his Arduino running OK the kepad. Thanks in advance. 

CrossRoads

I did the same - unzipped the Keypad folder into the Arduino Libraries folder.
Opened the CustomKeypad from included Keypad/examples folder

Code: [Select]

/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
//define the cymbols on the buttons of the keypads
char hexaKeys[ROWS][COLS] = {
  {'0','1','2','3'},
  {'4','5','6','7'},
  {'8','9','A','B'},
  {'C','D','E','F'}
};
byte rowPins[ROWS] = {3, 2, 1, 0}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad

//initialize an instance of class NewKeypad
Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup(){
  Serial.begin(9600);
}
 
void loop(){
  char customKey = customKeypad.getKey();
 
  if (customKey){
    Serial.println(customKey);
  }
}

Compiles fine under 1.0.1
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

tuxduino

Quote
error: 'Keypad' does not name a type


This error message means that the keypad library is not imported into your sketch.

I suspect you also have an error about Keypad.h not found, that you didn't include in your post.

PS: all caps means shouting. I'm sure you understand that shouting while asking for help is the right way to obtain nothing RIGHT NOW.

PaulS

Quote
My current IDE is arduino-1.0.1.

Well, there's your problem. The stupid 1.0.1 release no longer thinks that a missing include file is a fatal error. It is. What idiot changed the code to ignore that I do not know, but it was a brain-dead move.

Palliser

don't ask but I just rebooted my computer and now the sketch is running. anyway, thank you for all your comments.

Nick Gammon


Well, there's your problem. The stupid 1.0.1 release no longer thinks that a missing include file is a fatal error. It is. What idiot changed the code to ignore that I do not know, but it was a brain-dead move.


I was wondering about that. Hadn't put my finger on it until you said it. After all, the missing include files had been reported in the past.

Reported now:

http://code.google.com/p/arduino/issues/detail?id=1057
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Palliser

Thank you Nick for your clarification.
Ok, after been resolved my issue with Arduino 1.0.1, I moved with my little interfacing and after a couple of changes in my sketch, I can read the keypad strokes perfectly. I have attached some specs of my keypad, a schematic of the connection that I made and the code.





Code: [Select]
// 4 X 4 Keypad interfaced with Arduino

#include <Keypad.h>

const byte ROWS = 4; // Four rows
const byte COLS = 4; // Four columns
// Define the Keymap
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};

byte rowPins[ROWS] = { 8, 7, 6, 9 };
byte colPins[COLS] = { 5, 4, 3, 2 };

// Create the Keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

void setup(){
  Serial.begin(9600);
}

void loop(){
  char key = keypad.getKey();

  if (key != NO_KEY){
    Serial.println(key);
  }

RandallR

Reading a 4x4 matrix keypad using 8 I/O pins if very common.  However, I can find very little on using resistors and an analog pin.  The typical method uses something like: (Matrix_Keypad.gif)
However, something like this uses fewer pins: (Matrix_Keypad(analog).gif)
The only problem is the number of different resistor values.

Does anyone have another design, maybe one which only uses 1 or 2 different resistor values?

Palliser

I am thinking to use a 4052 to interface the keypad with the Arduino and only uses 4 pins of the board. This idea crossed to my mind during my current project: Integrate a 4x4 keypad and a lcd in one Arduino.
http://arduino.cc/forum/index.php/topic,125178.0.html

This is the schematic:



Cycling the sequence 00, 01, 10, 11 in S0 and S1, the board reads the states of each row and column in 1Z and 2Z thus identifying which key has been pressed. I am working now in the pseudo-code and hope soon to start the sketch using the Keypad library. Any help will be very welcome.


CrossRoads

You should look at the keypad.h library, and add your mux control into it.
Pullup resistors on the row inputs.
Drive a column low, see if any row reads low, indicating a button was pushed.
Drive next column low, read the 4 rows.
etc.
Can use 4 diodes from Row pins to an interrupt pin (anode on interrupt) to signal a Low, read the matrix.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

kourpetis

well i was thinking in using photoresistors as inputs for my matrix keypad.

under a painted plexiglass surface living open spaces only for my 16 photoresistors.

so with every push of my finger over the photoresistor i would cover the light and get my push.

i am not really sure if those schematics are usable though. while it is not a button that allows current only when you press it.
photoresistors allow current all the time. how can i make a 4x4 matrix keypad with photoresistors ????????????

help !

Go Up