Build it step by step
0 of 6 doneWire the matrix
Solder each key between a column and a row with a diode.
Test the scan
Print the pressed key number on the Serial Monitor first.
Add HID
Upload the sketch and open a text editor.
Test the combinations
Press a key and check the Ctrl shortcut it sends.
Add the knob
Turn for volume, press to switch to the media layer.
Print a case
Design a case for the 3 × 3 grid and the knob.
How do you build an USB macro keypad?
Nine keys and a knob send shortcuts to your computer, acting as a USB keyboard and media controller. You need Arduino Micro, Mechanical keyboard switch (MX style) × 9, Rotary encoder module (KY-040), Signal Diode 1N4148 × 9 and USB A to micro-USB cable (data). It takes 6 steps and about 120 minutes.
- Level
- Beginner
- Time
- About 120 min
- Parts
- 5
Parts list
- Development BoardsArduino Micro × 1
- Switches
- SensorsBuy
- SemiconductorsBuy
- Cables & WiresBuy
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| Each key switch | In a 3 × 3 grid: one leg to the row wire through a 1N4148, the other leg to the column wire | Matrix (diode cathode toward the row) |
| Row wires 1, 2, 3 | Micro D2, D3, D4 | Driven low one at a time |
| Column wires 1, 2, 3 | Micro D5, D6, D7 | Read with pull-ups |
| Encoder CLK, DT | Micro D8, D9 | Rotation |
| Encoder SW | Micro D10 | Press changes layer |
| Encoder + and GND | Micro 5V and GND | Power |
| USB cable | Micro USB port | Power and keyboard connection |
Code
Arduino IDE, board "Arduino Micro" (its ATmega32u4 can act as a USB keyboard). Install "HID-Project" by NicoHood, and check its ConsumerAPI.h for the media key names if one fails to compile on your version. The diodes stop ghost keys when several keys are pressed together; each diode's band (cathode) faces the row wire. Plug in only while testing, because a macro keypad types straight into whatever window has focus; keep an ordinary keyboard to hand.
Runs on a virtual Arduino Leonardo in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).
#include <HID-Project.h>
const byte ROWS[3] = { 2, 3, 4 }, COLS[3] = { 5, 6, 7 };
const byte ENC_A = 8, ENC_B = 9, ENC_SW = 10;
// Layer 0: ctrl shortcuts, one letter per key. Layer 1: media keys.
const char LETTERS[9] = { 'c', 'v', 'x', 'z', 'y', 's', 'a', 'f', 'w' };
const ConsumerKeycode MEDIA[9] = { MEDIA_PLAY_PAUSE, MEDIA_NEXT, MEDIA_PREVIOUS, MEDIA_VOLUME_MUTE, MEDIA_VOLUME_UP,
MEDIA_VOLUME_DOWN, CONSUMER_BRIGHTNESS_UP, CONSUMER_BRIGHTNESS_DOWN, MEDIA_PLAY_PAUSE };
bool layer = false, last[9] = { false };
int lastA = HIGH;
void setup() {
for (byte i = 0; i < 3; i++) { pinMode(ROWS[i], INPUT); pinMode(COLS[i], INPUT_PULLUP); }
pinMode(ENC_A, INPUT_PULLUP); pinMode(ENC_B, INPUT_PULLUP); pinMode(ENC_SW, INPUT_PULLUP);
Keyboard.begin();
Consumer.begin();
}
void loop() {
for (byte r = 0; r < 3; r++) {
pinMode(ROWS[r], OUTPUT); digitalWrite(ROWS[r], LOW); // select one row
for (byte c = 0; c < 3; c++) {
byte k = r * 3 + c;
bool down = digitalRead(COLS[c]) == LOW;
if (down && !last[k]) { // new key press
if (layer) Consumer.write(MEDIA[k]);
else { Keyboard.press(KEY_LEFT_CTRL); Keyboard.press(LETTERS[k]); delay(10); Keyboard.releaseAll(); }
}
last[k] = down;
}
pinMode(ROWS[r], INPUT); // release the row
}
int a = digitalRead(ENC_A);
if (a != lastA && a == LOW) Consumer.write(digitalRead(ENC_B) ? MEDIA_VOLUME_UP : MEDIA_VOLUME_DOWN);
lastA = a;
if (!digitalRead(ENC_SW)) { layer = !layer; delay(300); } // press the knob to change layer
delay(5);
}Test checklist
What you should see at each check. If a reading is far off, stop and use the troubleshooting list.
| Check | Expected |
|---|---|
| Key 1 on layer 0 | Ctrl+C |
| Knob turned right | Volume up |
| Knob pressed | Switches to the media layer |
| Key 1 on layer 1 | Play or pause |
If it does not work
Wrong keys or ghosts
Check the diode direction and that all diodes are in.
Nothing typed
The Micro must be selected as a board and appear as a keyboard.
Knob jumps
Add a 10 nF capacitor on each encoder pin to ground.
Compile error on media keys
Use the key names listed in your HID-Project version.
Still stuck? Find the fault step by step with your multimeter →
Common questions
What parts do I need for the USB macro keypad?
Arduino Micro, Mechanical keyboard switch (MX style) × 9, Rotary encoder module (KY-040), Signal Diode 1N4148 × 9 and USB A to micro-USB cable (data).
How long does the USB macro keypad take to build?
About 120 minutes for a beginner maker, in 6 steps.
How do I know the USB macro keypad works?
Key 1 on layer 0: Ctrl+C. Knob turned right: Volume up. Knob pressed: Switches to the media layer.
Wrong keys or ghosts: what should I check?
Check the diode direction and that all diodes are in.
Nothing typed: what should I check?
The Micro must be selected as a board and appear as a keyboard.
Knob jumps: what should I check?
Add a 10 nF capacitor on each encoder pin to ground.
Compile error on media keys: what should I check?
Use the key names listed in your HID-Project version.
