Projects / USB macro keypad

USB macro keypad

Beginner2 hGuided buildReviewed

Build it step by step

0 of 6 done
  1. ARDUINO

    Wire the matrix

    Solder each key between a column and a row with a diode.

  2. ARDUINO

    Test the scan

    Print the pressed key number on the Serial Monitor first.

  3. ARDUINO

    Add HID

    Upload the sketch and open a text editor.

  4. ARDUINO

    Test the combinations

    Press a key and check the Ctrl shortcut it sends.

  5. ARDUINO

    Add the knob

    Turn for volume, press to switch to the media layer.

  6. ARDUINO

    Print a case

    Design a case for the 3 × 3 grid and the knob.

In short

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

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Each key switchIn a 3 × 3 grid: one leg to the row wire through a 1N4148, the other leg to the column wireMatrix (diode cathode toward the row)
Row wires 1, 2, 3Micro D2, D3, D4Driven low one at a time
Column wires 1, 2, 3Micro D5, D6, D7Read with pull-ups
Encoder CLK, DTMicro D8, D9Rotation
Encoder SWMicro D10Press changes layer
Encoder + and GNDMicro 5V and GNDPower
USB cableMicro USB portPower 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.

CheckExpected
Key 1 on layer 0Ctrl+C
Knob turned rightVolume up
Knob pressedSwitches to the media layer
Key 1 on layer 1Play 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 →

Ask Lark AI

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.