Dear Arduino community,
I'm working on my first Arduino project and I will need and appreciate your help with the hardware part.
Currently I have a problem with a 4x4 Keypad Matrix, which I got from eBay. It came with no documentation and I couldn't find much online. I understand the row-column nature of the board and know how to calculate which button is pressed based on the two HIGH readings.
My question is how to properly plug it into Arduino board. I tried to connect each of the 8 Keyboard pins to the digital inputs through 1kOhm and 100Ohm resistor, however I'm getting unexpected output.
This is the listing of the Serial output without anything actually pressed on the keypad:
try duplicate the statement :
buttonState = digitalRead(gridPins*);* buttonState = digitalRead(gridPins*);* It will act as a CPU clock cycle delay for the digitalRead, and the first reading will simply be discarded.
He doesn't have a 74LS922 on that though - that's just a straight-up matrix.
My approach would be to put columns in input_pullup mode, and the rows as outputs. Then, loop through the rows, driving each one low and reading the inputs until an input gets pulled low.
DrAzzy:
He doesn't have a 74LS922 on that though - that's just a straight-up matrix.
My approach would be to put columns in input_pullup mode, and the rows as outputs. Then, loop through the rows, driving each one low and reading the inputs until an input gets pulled low.
That explains the problem.
A passive matrix will never supply any signals at all.
DrAzzy:
He doesn't have a 74LS922 on that though - that's just a straight-up matrix.
exactly
DrAzzy:
My approach would be to put columns in input_pullup mode, and the rows as outputs. Then, loop through the rows, driving each one low and reading the inputs until an input gets pulled low.
do i get it right, that this way you only identify in which row is the pressed button by comparing a changed state from HIGH to LOW in the column pin? that actually sounds smart.
but i think my current problem is some noise i'm getting from the pins. if you look into the Serial log i posted, there are random HIGH readings from the pins. honestly, I don't know how to use the resistors properly, nor what is their purpose.
i tried to do something like this, but it only works in one row, disabling all other rows (or maybe i'm doing something wrong:
DrAzzy:
My approach would be to put columns in input_pullup mode, and the rows as outputs. Then, loop through the rows, driving each one low and reading the inputs until an input gets pulled low.
DrAzzy:
He doesn't have a 74LS922 on that though - that's just a straight-up matrix.
My approach would be to put columns in input_pullup mode, and the rows as outputs. Then, loop through the rows, driving each one low and reading the inputs until an input gets pulled low.
THANK YOU! It works. All the pins are connected to the Arduino directly.
This is my code
const int gridRows[4] = {8,7,6,5};
const int gridColumns[4] = {9,10,11,12};
int buttonState = 0;
const int ledPin = 2;
int counter = 0;
void setup() {
Serial.begin(9600);
Serial.println ("Begin");
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
delay (3000);
digitalWrite(ledPin, LOW);
for (int i=0; i<4; i++){
pinMode(gridColumns[i], INPUT_PULLUP);
}
for (int i=0; i<4; i++){
pinMode(gridRows[i], OUTPUT);
}
}
void loop() {
Serial.println((int)counter);
counter++;
for (int row=0; row<4; row++){
digitalWrite (gridRows[row],LOW);
for (int col=0; col<4; col++){
buttonState = digitalRead(gridColumns[col]);
if (buttonState == LOW) {
Serial.print ("1");
digitalWrite(ledPin,HIGH);
delay(100);
} else {
Serial.print ("0");
digitalWrite(ledPin,LOW);
}
}
Serial.println ();
digitalWrite (gridRows[row],HIGH);
}
delay(500);
}
however only one button per column can be pressed at a time. a second loop which switches inputs and outputs solves the problem. both results should go through a logical OR then. see my next post.
thanks, I'm basically doing the same. also, this gives me the exact same error - while I press buttons 2+5+6 it also indicates button 1 as pressed. I guess this is a flaw of the hardware solution.
I don't think a keypad matrix row/column schemes is designed for multiple simultaneous keypresses. If you made your own keypad with two pins per button you could do multiple keypresses using a non-matrix scheme. Never tried it myself but that's my guess.
Try my version. I tested it and works correctly.
Connect your rows to Arduino 2-5 pin and columns to 6-9 pins. Don't forget connect 10K resistors as you can see here: http://arduino.cc/en/tutorial/button
Here is the code
// Made by Atis
/* You can read your pressed button value on serial monitor.
0-9 ==> 0-9
A-D ==> 10-13
* ==>14 // Its only because my keypad numbers 0-9, letters A-D and symbols #,*
# ==> 15
*/
const int sor1 = 2; //sor mean row
const int sor2 = 3;
const int sor3 = 4;
const int sor4 = 5;
const int osz1 = 6; //osz mean column
const int osz2 = 7;
const int osz3 = 8;
const int osz4 = 9;
void setup() {
// put your setup code here, to run once:
pinMode(sor1, OUTPUT);
pinMode(sor2, OUTPUT);
pinMode(sor3, OUTPUT);
pinMode(sor4, OUTPUT);
pinMode(osz1, INPUT);
pinMode(osz2, INPUT);
pinMode(osz3, INPUT);
pinMode(osz4, INPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
int key = 0;
int i = 0;
int sor = 0;
int state = 0;
for (int sor = 2; sor < 6; sor++) {
digitalWrite(sor, HIGH);
for (int osz = 6; osz < 10; osz++) {
state = digitalRead(osz);
if (state == HIGH) {
digitalWrite(13, HIGH);
digitalWrite(sor, LOW);
if (sor == 2 && osz == 6) {
key = 1;
}
if (sor == 2 && osz == 7) {
key = 2;
}
if (sor == 2 && osz == 8) {
key = 3;
}
if (sor == 3 && osz == 6) {
key = 4;
}
if (sor == 3 && osz == 7) {
key = 5;
}
if (sor == 3 && osz == 8) {
key = 6;
}
if (sor == 4 && osz == 6) {
key = 7;
}
if (sor == 4 && osz == 7) {
key = 8;
}
if (sor == 4 && osz == 8) {
key = 9;
}
if (sor == 5 && osz == 7) {
key = 0;
}
if (sor == 2 && osz == 9) {
key = 10;
}
if (sor == 3 && osz == 9) {
key = 11;
}
if (sor == 4 && osz == 9) {
key = 12;
}
if (sor == 5 && osz == 9) {
key = 13;
}
if (sor == 5 && osz == 6) {
key = 14;
}
if (sor == 5 && osz == 8) {
key = 15;
}
Serial.println(key);
}
}
}
}