4x4 button board scheme

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:

Begin
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
Pressed: A
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
Pressed: A
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
Pressed: A
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
Pressed: A

And this is my program:

const int PIN_COUNT = 8;

const int gridPins[PIN_COUNT] = {5,6,7,8,9,10,11,12};     
const String gridStrings[PIN_COUNT] = {"A","B","C","D","E","F","G","H"};  

const int ledPin = 2;

int buttonState = 0;

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<PIN_COUNT; i++){
    pinMode(gridPins[i], INPUT);
  }
}

void loop() {
  Serial.println((int)counter);
  counter++;
 
  for (int i=0; i<PIN_COUNT; i++){
    buttonState = digitalRead(gridPins[i]);
    if (buttonState == HIGH) {
      Serial.println ("Pressed: "+gridStrings[i]);
      digitalWrite(ledPin, HIGH);
      delay (1000);
    } else {
      digitalWrite(ledPin, LOW);
    }
  }
}

some pictures

thanks for any help!

I can't read the button labels. Which are the columns (letters or numbers) ?

That program appears to be for an 8x8 matrix, not 4x4.
The 74ls922 is a 16 key encoder chip.
16-key keypad encoder tutorial

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.

raschemmel:
I can't read the button labels. Which are the columns (letters or numbers) ?

stating S1-S16.

raschemmel:
That program appears to be for an 8x8 matrix, not 4x4.

nope, i'm reading 8 pins of the board.

raschemmel:
The 74ls922 is a 16 key encoder chip.
16-key keypad encoder tutorial

there is no controller on the board

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.

oh, i got it! now i need to try

That program

appears to be for an 8x8 matrix, not 4x4.

nope, i'm reading 8 pins of the board.

Of course your are. 4 columns + 4 rows = 8 pins.

That program

(the software, not the board)

has 8 columns + 8 rows = 16 pins. You only have 8.

How are you going to correlate 16 pins in the software to 8 pins on the board ?

almost solved!

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.

now it works also with more buttons pressed in the same row/column at the same time.
however

from 0100 to 0100 results in 0000
from 1000 to 1100 results in 0000
from 0000 to 0000 results in 0000
from 0000 to 0000 results in 0000

any idea how to solve it?

my current code

const int gridRows[4] = {8,7,6,5};
const int gridColumns[4] = {9,10,11,12};

boolean grid[4][4];

int buttonState = 0;

const int ledPin = 2;
int counter = 0;

void setup() {
  Serial.begin(9600); 
  pinMode(ledPin, OUTPUT);
}

void loop() {
  Serial.println((int)counter);
  counter++;
  
  //read horizontally
  for (int i=0; i<4; i++){pinMode(gridColumns[i], INPUT_PULLUP);}
  for (int i=0; i<4; i++){pinMode(gridRows[i], OUTPUT);}
  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) {
        grid[col][row]=true;
      } else {
        grid[col][row]=false;
      }
    }
    digitalWrite (gridRows[row],HIGH);
  }
  
  //read vertically
  for (int i=0; i<4; i++){pinMode(gridColumns[i], OUTPUT);}
  for (int i=0; i<4; i++){pinMode(gridRows[i], INPUT_PULLUP);}
  for (int col=0; col<4; col++){
    digitalWrite (gridColumns[col],LOW);
    for (int row=0; row<4; row++){    
      buttonState = digitalRead(gridRows[row]);
      if (buttonState == LOW) {
        grid[col][row]=grid[col][row]||true;
      } else {
        grid[col][row]=grid[col][row]||false;
      }
    }
    digitalWrite (gridColumns[col],HIGH);
  }
  
  //print output
  for (int row=0; row<4; row++){
    for (int col=0; col<4; col++){    
      if (grid[col][row]) {
        Serial.print("1");
      } else {
        Serial.print("0");
      }
    }
    Serial.println ();
  }
  
  delay(500);
}

thanks again!

Have you read this

http://playground.arduino.cc/Main/KeypadTutorial

Good call BillHo !

BillHo:
Have you read this

Arduino Playground - KeypadTutorial
How to Connect and Read a Keypad with an Arduino

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.

Yes, there are limitations.

http://www.dribin.org/dave/keyboard/one_html/

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.

@Paulcet,
Great tutorial !

Hi,

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);

      }
    }
  }
}