Build a rhythm game with the keypad

Turn the 4×4 keypad into a music game controller. Coloured notes fall down four lanes on your computer screen, and you press A, B, C or D just as each note reaches the line. Perfect timing scores double.
  • Level:Intermediate
  • Duration: 50 min
  • Ages 10+
  • Platform: Arduino

Tools for this lesson

Helpful for this build. Open a tool to see tips and more projects that use it.

Build a rhythm game with the keypad – EEMENTOR STEM™ lesson

In rhythm games like Guitar Hero or osu!, notes slide towards a line and you press the matching button exactly when they arrive. The game measures how many hundredths of a second you were early or late. In this lesson the four letter keys of the keypad, A, B, C and D, are your four buttons. The UNO watches the keypad and sends a message the moment a key goes down. The Rhythm keys game in the Arduino Playground draws the falling notes, plays the music and judges your timing.

What you'll learn

  • How a keypad matrix finds the pressed key with only 8 wires
  • How to send an event (something that happened) instead of a value
  • Why response time matters in games, and how fast the UNO really is
  • How a game decides between "Perfect", "Good" and "Miss"

What you need

  • UNO R3 board and USB cable (UNO R3 Starter Kit)
  • 4×4 keypad module (UNO R3 Starter Kit)
  • A computer with Chrome or Edge and speakers or headphones
  • Breadboard and jumper wires (UNO R3 Starter Kit)

Wiring

Breadboard wiring diagram: Build a rhythm game with the keypad
Breadboard view. Check each connection against the wiring table below.
From To
Keypad R1, R2, R3, R4 UNO pins A0, A1, A2, A3
Keypad C1, C2, C3, C4 UNO pins 10, 8, 7, 6

The keypad is wired the same way as in the keypad safe. Lay it flat in front of you, with the letter column on the right: A is the top key, D the bottom one.

Example screen for: Build a rhythm game with the keypad
Example screen with sample values. Yours may look different.

The sketch

In the Arduino IDE, open Library Manager and install Keypad by Mark Stanley and Alexander Brevig.

#include <Keypad.h>

const byte ROWS = 4, COLS = 4;
char keys[ROWS][COLS] = {
  {'1', '2', '3', 'A'},
  {'4', '5', '6', 'B'},
  {'7', '8', '9', 'C'},
  {'*', '0', '#', 'D'}
};
byte rowPins[ROWS] = {A0, A1, A2, A3};
byte colPins[COLS] = {10, 8, 7, 6};
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);

void setup() {
  Serial.begin(115200);
  keypad.setDebounceTime(5);              // react within a few milliseconds
}

void loop() {
  char key = keypad.getKey();             // a key that was just pressed, or 0
  if (key >= 'A' && key <= 'D') {
    Serial.print("k:");
    Serial.println(key - 'A' + 1);        // lane 1 to 4
  }
}

Play it

  1. Upload the sketch. Open the Serial Monitor at 115200 baud for a moment to check that the numbers change when you press A, B, C and D, then close the Serial Monitor: only one program can use the USB port at a time.
  2. Open the Arduino Playground in Chrome or Edge on a computer. These browsers can talk to a USB board through a feature called Web Serial.
  3. Click Connect your board, choose the port of your UNO (often called "USB Serial" or "CH340") and click Connect.
  4. Play! The status line says "Connected", and "Connection help" under the game shows the last line your UNO sent.

No board at hand? Click Play with the keyboard on the Playground page to try the game first.

Press any lane key to start the song. Hit a note when its middle touches the white line.

How it works

The keypad has 16 keys but only 8 wires: 4 rows and 4 columns. The Keypad library switches one row at a time to LOW and checks which column becomes LOW too. The key at that crossing is the pressed one. It does this many hundreds of times a second, so a press is noticed within a few milliseconds.

This sketch doesn't send a value all the time like a joystick. It sends an event: one short line, at the moment the key goes down. key - 'A' + 1 turns the letters into lane numbers, because characters are numbers inside the computer: 'A' is 65 and 'B' is 66, so 'B' - 'A' + 1 is 2.

The game knows the exact time of every note in the song. When a "k" message arrives, it looks for a note in that lane that is due within a quarter of a second. Closer than 0.08 seconds counts as Perfect, closer than 0.25 seconds as Good, and anything else is a Miss. Every 10 hits in a row raise the score multiplier, which is the combo. The whole trip from key to screen, through the keypad scan, the USB cable and the browser, takes only a few hundredths of a second, short enough for a fair game.

Try this next

  • Send lane 1 to 4 from the number keys 1 to 4 as well, so two players can share the keypad.
  • Add the passive buzzer on pin 12 and play a click with tone(12, 2000, 10) on every key press. Does it help your timing?
  • Measure your own reaction: send the time in milliseconds with every press using millis() and compare two presses in the Serial Monitor.

Troubleshooting

  • The wrong lane lights up: two column wires are swapped. Check C1 to C4 on pins 10, 8, 7 and 6.
  • Nothing happens: open the Serial Monitor at 115200 baud and press A: you should see k:1. If not, check the eight keypad wires.
  • "Could not connect" or "Failed to open serial port": another program is using the port. Close the Serial Monitor and the Serial Plotter in the Arduino IDE, then click Connect again.

Build it yourself

Everything used in this lesson comes in one kit, with parts tested by our engineers.

UNO R3 Starter Kit | Arduino IDE Compatible | Free Tutorials $56.00

Shop the kit