1x4 keypad strip: which key?, on an Arduino Uno
A four-key membrane strip on five straight wires: the Keypad library scans it as one row of four and prints each 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 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $28. On Mokxi they're free.
What you'll learn
- Controllers
- Using the Keypad
- Reading and changing a sketch
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 2
How it works
A four-key membrane strip on five straight wires: the Keypad library scans it as one row of four and prints each key you press.
Arduino Uno R3 runs the sketch below. The circuit around it: Keypad, 1x4 strip. Serial.begin opens the serial port at 9600 baud.
Parts list
2 parts, plus the jumper wires. They retail for about $28; on Mokxi they're free.
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, 1x4 strip pin K2 | Arduino Uno R3 pin 6 |
| Keypad, 1x4 strip pin K1 | Arduino Uno R3 pin 5 |
| Keypad, 1x4 strip pin K4 | Arduino Uno R3 pin 4 |
| Keypad, 1x4 strip pin K3 | Arduino Uno R3 pin 3 |
| Keypad, 1x4 strip pin COM | Arduino Uno R3 pin 2 |
The note on the canvas
Word for word, as it sits beside the circuit in the editor.
Try it
1. Press Run, then click a key on the strip. The serial monitor prints key 1, key 2, key 3 or key 4.
2. Click the strip and press 1 to 4 on your own keyboard instead.
3. Hold two keys at once: both close, but getKey() only hands back the first one it scans. getKeys() reads them all.
4. Look at the ribbon: its pins run key 2, key 1, key 4, key 3, COM. The sketch's pin lists sort that out.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// A 1x4 membrane keypad strip: every key you press, printed.
//
// ribbon pin: 1 2 3 4 5
// strip line: key 2 key 1 key 4 key 3 COM
// Uno pin: 6 5 4 3 2
//
// Four keys on five wires. Every key joins its own wire to the common one, so
// to the Keypad library the strip is a matrix of one row (COM) and four
// columns (the keys). It drives each key wire low in turn and reads COM.
//
// The ribbon is not in key order, so five straight wires into pins 6 to 2
// put the keys on 5, 6, 3 and 4. The pin lists below say which is which.
//
// Click a key on the canvas.
#include <Keypad.h>
const byte ROWS = 1;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1', '2', '3', '4'},
};
byte rowPins[ROWS] = {2}; // COM
byte colPins[COLS] = {5, 6, 3, 4}; // keys 1, 2, 3 and 4
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: 1x4 keypad, COM on 2, keys 1 to 4 on 5, 6, 3, 4. Press a key.");
}
void loop() {
char key = keypad.getKey();
if (key) {
Serial.print("key ");
Serial.println(key);
}
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once when the board starts. It gets the pins and libraries ready.
void setup() {
Serial.begin(9600);
Serial.println("Mokxi Uno: 1x4 keypad, COM on 2, keys 1 to 4 on 5, 6, 3, 4. Press a key.");
}loop sketch.ino
Runs over and over for as long as the board has power. This is where the project does its work.
void loop() {
char key = keypad.getKey();
if (key) {
Serial.print("key ");
Serial.println(key);
}
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- 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.