Sven Krasser’s Blog

Musings on technology, security, and sundries.

USB Mini Media Keyboard

For my latest electronics project, I’ve built a small USB keyboard input device for media control. The final version has three keys: play/pause, previous track, and next track. In addition, a rotary encoder allows adjusting the volume (or mutes sound when pressed). This is also my first project involving a custom circuit board — my previous electronics projects mainly involved breadboards of varying sizes.

The project is based on a Pro Micro, an Arduino-compatible ATmega 32u4 board (the 32u4 allowing to easily create USB devices). For iteration 1, I added a couple of buttons and a rotary encoder to a breadboard alongside the Pro Micro to see how I can get media controls over USB to work. Turns out, Nico Hood’s HID library makes that quite simple.

Iteration 2 involved the final circuitry and parts on a breadboard. The switches are Cherry MX Blue, and the rotary encoder for this iteration has an RGB LED built in. SparkFun sells breakout boards to make these parts breadboardable. Besides the media controls, holding the play/pause button and turning the rotary encoder adjusts the brightness of the key backlights.

The application notes for the rotary encoder suggest filtering circuits for the outputs used for measuring rotation. It works fine without those, so I omitted them. However, the code on the Arduino adds a small delay when the encoder is pressed and used as a button (i.e. for the mute function) to debounce the signal. The Cherry MX switches don’t show any bouncing, likely because the MCU is sufficiently slow when reading the input pins. The Arduino digitalRead() function adds a good amount of overhead, for what it’s worth.

My initial plan was to transfer this design to a stripboard and solder it in place as a semi-permanent solution. Eventually, I decided that it’s time to learn how to print my own circuit boards, and I spent a day digging through KiCad. Iteration 3 is on a custom PCB, which holds the Pro Micro, the switches, the rotary encoder, and a handful of resistors. SparkFun has footprints for KiCad for the Pro Micro, Cherry MX switches, and for their RGB LED rotary encoder. The process of getting boards manufactured was surprisingly painless. I ordered the PCBs last Tuesday from a company in Shenzhen and received them last Friday at my home in Los Angeles.

Adafruit has a great tutorial for a similar project. It also covers designing and 3D-printing custom keycaps (mine are clear keycaps that SparkFun sells) and creating a 3D-printed case for the board and switches (mine simply sits on 4 nylon standoffs).

Arduino, its framework, and the libraries are great for getting started, but having a whole small Arduino board mounted on the PCB is wasteful and looks clunky. As the next step, it’s time to learn to work with AVR MCUs directly. In addition to the keyboard PCB, I also created and ordered a few boards to make an ATmega 16u2 in a TQFP-32 package breadboardable for prototyping — and that also requires beefing up my soldering skills to surface mount the quad flatpack to the board.