Level 2Unit C · Session 18
18

Unit C · Telling a machine what you want

Typing a number in

What we are making: a keypad that reports which key you pressed. The first time your machine takes real input from a person.

The idea

Sixteen keys, but only eight wires. The keypad is wired as a grid: four rows crossing four columns. When you press the key at row 2, column 3, it connects that row wire to that column wire, and nowhere else. The library checks every crossing point, hundreds of times a second, and works out which one you are holding down.

This is called matrix scanning, and it is how every computer keyboard works, including the one in front of you.

New words
MatrixA grid of rows and columns, so few wires can serve many keys.
charA variable holding one single character, written in single quotes.
getKey()Asks the library which key is down. Gives 0 if none is.

Wiring

The keypad's ribbon has eight pins in a row. With the keys facing you, they go into the Arduino left to right:

4
Keypad pins 1–4 → Arduino 9, 8, 7, 6 (the rows)
4
Keypad pins 5–8 → Arduino 5, 4, 3, 2 (the columns)
0
Power wires — a keypad is only switches
Which key did you press?
1#include <Keypad.h>
2 
3const byte ROWS = 4;
4const byte COLS = 4;
5 
6char keys[ROWS][COLS] = {
7 {&#x27;1&#x27;,&#x27;2&#x27;,&#x27;3&#x27;,&#x27;A&#x27;},
8 {&#x27;4&#x27;,&#x27;5&#x27;,&#x27;6&#x27;,&#x27;B&#x27;},
9 {&#x27;7&#x27;,&#x27;8&#x27;,&#x27;9&#x27;,&#x27;C&#x27;},
10 {&#x27;*&#x27;,&#x27;0&#x27;,&#x27;#&#x27;,&#x27;D&#x27;}
11};
12 
13byte rowPins[ROWS] = {9, 8, 7, 6};
14byte colPins[COLS] = {5, 4, 3, 2};
15 
16Keypad pad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
17 
18void setup() {
19 Serial.begin(9600);
20 Serial.println("Press a key.");
21}
22 
23void loop() {
24 char key = pad.getKey();
25 if (key) { // getKey() gives 0 when nothing is pressed
26 Serial.print("You pressed: ");
27 Serial.println(key);
28 }
29}

Do it

  1. Install the Keypad library.
  2. Wire the eight pins in order. Getting one wrong swaps rows and columns, which gives you the wrong letters — a useful clue.
  3. Upload, open the Serial Monitor, and press every key in turn.
  4. Check that what is printed matches what is printed on the key.

Change it

  1. Make pressing A turn the built-in LED on, and B turn it off.
  2. Collect digits into a variable until # is pressed, then print the whole number.
  3. Pick a four-digit code and make the machine say OPEN when it is typed in.
💡 Tip
If the letters are wrong but consistent, your rows and columns are swapped. Do not start rewiring at random — just swap rowPins and colPins in the code and upload again. Ten seconds, and it tells you whether that was the problem.
What you seeWhyWhat to do
Nothing prints at allThe ribbon is in backwards.Turn the connector round and try again.
Every key gives the wrong characterRows and columns are swapped.Swap rowPins and colPins in the code, not the wiring.
One whole row is deadOne pin is not making contact.Push the ribbon fully home and check that pin on the board.
One press prints several timesSwitch bounce.The library usually handles it. Add a short delay if it does not.

What you learned

  • A grid of rows and columns lets eight wires serve sixteen keys.
  • A char holds one character, in single quotes: '7'.
  • Getting input from a person is its own problem, separate from sensing the world.
Challenge optional — only if you finish early
  • Build a four-digit combination lock. Wrong code, it says so and starts again.
17. A knob, and then a joystick