Keypad on pins 11 to 4, on an Arduino Uno
A 4x4 keypad with its rows on pins 11, 10, 9 and 8 and its columns on 7, 6, 5 and 4: the Keypad library scans it and prints every key you press. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 15 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $29. On Mokxi they're free.
What you'll learn
- Matrix keypads: 16 keys on 8 wires
- Using the Keypad library
- Mapping rows and columns to characters
- Reading key presses from the serial monitor
- Difficulty: Beginner
- Time: About 15 minutes
- Board: Arduino Uno R3
- Parts: 2
How it works
A 4x4 keypad has sixteen keys but only eight wires. The rows R1 to R4 go to pins 11, 10, 9 and 8. The columns C1 to C4 go to pins 7, 6, 5 and 4. Pressing a key joins its row to its column.
The Keypad library scans those pins and works out which row and column joined. The sketch's KEYS table turns that crossing into a character. When a key is pressed, the sketch prints its character to the serial monitor.
Parts list
2 parts, plus the jumper wires. They retail for about $29; on Mokxi they're free.
Kits that can build it
These 4 starter kits have every part in the circuit. Each kit's page lists what else it can build.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Keypad, 4x4 pin R1 | Arduino Uno R3 pin 11 |
| Keypad, 4x4 pin R2 | Arduino Uno R3 pin 10 |
| Keypad, 4x4 pin R3 | Arduino Uno R3 pin 9 |
| Keypad, 4x4 pin R4 | Arduino Uno R3 pin 8 |
| Keypad, 4x4 pin C1 | Arduino Uno R3 pin 7 |
| Keypad, 4x4 pin C2 | Arduino Uno R3 pin 6 |
| Keypad, 4x4 pin C3 | Arduino Uno R3 pin 5 |
| Keypad, 4x4 pin C4 | Arduino Uno R3 pin 4 |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// A 4x4 keypad: sixteen keys on eight pins.
//
// keypad rows 1..4 -> pins 11, 10, 9, 8 columns 1..4 -> pins 7, 6, 5, 4
//
// The library drives one row at a time and reads the columns: a pressed key
// joins its row to its column, so the row being driven shows up on that
// column. That finds the key with eight wires instead of sixteen.
//
// Click a key on the keypad.
#include <Keypad.h>
const char KEYS[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'},
};
byte rowPins[4] = {11, 10, 9, 8};
byte colPins[4] = {7, 6, 5, 4};
Keypad keypad = Keypad(makeKeymap(KEYS), rowPins, colPins, 4, 4);
void setup() {
Serial.begin(9600);
Serial.println("UNO is ready! Press a key.");
}
void loop() {
char key = keypad.getKey();
if (key) Serial.println(key);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Starts serial at 9600 and prints "UNO is ready! Press a key." The keypad itself was already set up at the top, by Keypad(makeKeymap(KEYS), rowPins, colPins, 4, 4), which tells the library the layout and the pins.
void setup() {
Serial.begin(9600);
Serial.println("UNO is ready! Press a key.");
}loop sketch.ino
Calls keypad.getKey() over and over. It returns 0 when nothing is pressed, so if (key) is false and nothing prints. When a key is pressed, its character is printed on its own line.
void loop() {
char key = keypad.getKey();
if (key) Serial.println(key);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Pressing a key prints the wrong character This project
Check the order ofrowPinsandcolPins. Row 1 is pin 11 and column 1 is pin 7. - Nothing prints This project
Make sure all eight wires are connected, and click a key on the keypad while the sketch runs. - Pressing one key reports several keys, or the wrong key. Keypad
Match the keymap and the row and column pin order to your keypad's pinout. Without diodes, pressing three keys at once can ghost a fourth. - No keys are detected. Keypad
Rows need pull-ups, or use INPUT_PULLUP, while one column is driven low at a time. The Keypad library does this for you if you pass the pins in the right order. - Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.