Controls
Macro pad with a knob
Twelve keys that fire shortcuts on your computer such as copy, paste and undo, and a knob for the volume.
- Hard
- about 75 min
- ESP32-S3
- 8 parts
- 16 wires
- 108 lines of code
ESP32 packs are built on real parts: the simulator runs Arduino Uno circuits only.
Idea
The computer sees it as an ordinary USB keyboard, so there is nothing to install. Twelve keys need only seven wires because they are wired as a grid of rows and columns.
What you learn
Acting as a USB keyboard, scanning a key matrix, and sending media keys.
How it works
input
Keypad
Rotary knob
decides
ESP32-S3
output
OLED 0.91 in
Computer (USB keyboard)
Powered with 5 V through the board's USB-C port.
A reference design. The sketch is compiled with the real ESP32 toolchain, and its pins are checked against the wiring table automatically. It has not yet been built and tested on real parts by us, so wire it on the bench and run the checks before you build it into a case. Real modules vary: follow the labels printed on yours, not their position in the picture.
Parts
0 of 8 ready
No prices here: they change by shop and by week. Buy each part by its name and the note beside it.
You also need
A computer with Arduino IDE 2 installed (free)
3D wiring
One jumper per step, with the USB cable unplugged. Several wires may share 3V3, 5V or GND: the expansion board or breadboard gives each its own pin.
Step 0 of 16
Lay out the parts
Seat the board on its expansion board or breadboard and lay the modules out round it. Nothing is connected yet.
All connections
macro-pad.ino
// Macro pad with a knob
// Twelve keys that type shortcuts on your computer, and a knob for the volume.
// Arduino IDE: Tools > USB Mode > "USB-OTG (TinyUSB)", then plug the cable
// into the DevKit's port marked USB (not UART) to use it as a keyboard.
#include "USB.h"
#include "USBHIDKeyboard.h"
#include "USBHIDConsumerControl.h"
#include <Keypad.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const int PIN_KEYS_R1 = 4;
const int PIN_KEYS_R2 = 5;
const int PIN_KEYS_R3 = 6;
const int PIN_KEYS_R4 = 7;
const int PIN_KEYS_C1 = 15;
const int PIN_KEYS_C2 = 16;
const int PIN_KEYS_C3 = 17;
const int PIN_KNOB_CLK = 1;
const int PIN_KNOB_DT = 2;
const int PIN_KNOB_SW = 42;
const int PIN_OLED_SDA = 8;
const int PIN_OLED_SCL = 9;
USBHIDKeyboard Keyboard;
USBHIDConsumerControl Media;
Adafruit_SSD1306 oled(128, 32, &Wire, -1);
const byte ROWS = 4;
const byte COLS = 3;
char keyMap[ROWS][COLS] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}, {'*', '0', '#'}};
byte rowPins[ROWS] = {PIN_KEYS_R1, PIN_KEYS_R2, PIN_KEYS_R3, PIN_KEYS_R4};
byte colPins[COLS] = {PIN_KEYS_C1, PIN_KEYS_C2, PIN_KEYS_C3};
Keypad keypad = Keypad(makeKeymap(keyMap), rowPins, colPins, ROWS, COLS);
int lastClk = HIGH;
void show(const char* text) {
oled.clearDisplay();
oled.setTextSize(2);
oled.setCursor(0, 8);
oled.print(text);
oled.display();
}
// Hold Ctrl (Cmd on a Mac: change KEY_LEFT_CTRL to KEY_LEFT_GUI) and tap a key.
void shortcut(char key, const char* name) {
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press(key);
delay(40);
Keyboard.releaseAll();
show(name);
}
void runKey(char key) {
switch (key) {
case '1': shortcut('c', "Copy"); break;
case '2': shortcut('v', "Paste"); break;
case '3': shortcut('x', "Cut"); break;
case '4': shortcut('z', "Undo"); break;
case '5': shortcut('y', "Redo"); break;
case '6': shortcut('s', "Save"); break;
case '7': shortcut('a', "Select all"); break;
case '8': shortcut('f', "Find"); break;
case '9': shortcut('t', "New tab"); break;
case '*': Media.press(CONSUMER_CONTROL_SCAN_PREVIOUS); Media.release(); show("Previous"); break;
case '0': Media.press(CONSUMER_CONTROL_PLAY_PAUSE); Media.release(); show("Play/pause"); break;
case '#': Media.press(CONSUMER_CONTROL_SCAN_NEXT); Media.release(); show("Next"); break;
}
}
void setup() {
pinMode(PIN_KNOB_CLK, INPUT_PULLUP);
pinMode(PIN_KNOB_DT, INPUT_PULLUP);
pinMode(PIN_KNOB_SW, INPUT_PULLUP);
Wire.begin(PIN_OLED_SDA, PIN_OLED_SCL);
oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
oled.setTextColor(SSD1306_WHITE);
show("Macro pad");
Keyboard.begin();
Media.begin();
USB.begin();
lastClk = digitalRead(PIN_KNOB_CLK);
}
void loop() {
char key = keypad.getKey();
if (key) runKey(key);
int clk = digitalRead(PIN_KNOB_CLK);
if (clk != lastClk && clk == LOW) {
bool up = digitalRead(PIN_KNOB_DT) == HIGH;
Media.press(up ? CONSUMER_CONTROL_VOLUME_INCREMENT : CONSUMER_CONTROL_VOLUME_DECREMENT);
Media.release();
show(up ? "Volume +" : "Volume -");
}
lastClk = clk;
if (digitalRead(PIN_KNOB_SW) == LOW) {
delay(30);
while (digitalRead(PIN_KNOB_SW) == LOW) delay(5);
Media.press(CONSUMER_CONTROL_MUTE);
Media.release();
show("Mute");
}
delay(1);
}
Libraries
- Keypad: install it from Sketch > Include Library > Manage Libraries.
- Adafruit GFX Library: install it from Sketch > Include Library > Manage Libraries.
- Adafruit SSD1306: install it from Sketch > Include Library > Manage Libraries.
Uploading
First time with an ESP32: in Arduino IDE open Boards Manager, search “esp32” and install “esp32 by Espressif Systems”.
- Set Tools > USB Mode to “USB-OTG (TinyUSB)”. Upload through the port marked UART, then move the cable to the port marked USB to use it.
- Open the sketch in Arduino IDE.
- Plug the board in with the USB-C cable.
- Tools > Board > ESP32S3 Dev Module, then Tools > Port and choose the board's port.
- Press Upload (the arrow). Wait for “Done uploading”.