What is a MacroPad?
I get this question a lot when I talk about this project, which is a lot. A macro is a script with a trigger or hotkey assigned to it. This can allow you to do any number of things with a single push of a button. It's a great productivity tool that we have available to us in almost any computing environment, but people rarely take advantage of (probably because it invloves some light programming).
The keypad that I built does not have to be a MacroPad, however. It could really be anything that inputs information into a computer. So it could be a custom mini-keyboard, or even some weird push-button mouse controller, or a joystick. I chose to make mine a keyboard with virtual keys which are not normally available, but I'll get into that more in the second half of this build.
Why do I want one?
I regularly get this question as a followup. I've been scripting a lot of my work recently, inspired by a video by Taran from Linus Media Group. In the video, Taran discusses the use of macros vs manual entry for regular tasks (02:00 mark), and makes a pretty clear point: seconds can add up.
I'm a big fan of productivity, in that I like getting a lot done in a little time. I also enjoy not being stressed by working my butt off to accomplish that amount of work. Work smarter, not harder, right? Macros enable me to do just that, and a MacroPad will allow me to do that even easier.
Why did I make one?
I started by researching lots. Would it be cheaper to buy one? Could I just buy a second keyboard and repurpose it? Do I want to make one by wiring it directly or get a PCB? Should I get a kit or order components separately?
Those are all very viable options. There are custom-programmable keyboards. There are gaming keyboards that allow you to remap keys to macros, or to launch scripts when pressed. There are lots of kits for building a programmable keyboard. There are pre-made MacroPads.
I decided, after boring my wife to tears by talking to her about it for a week, that I would get the most enjoyment and the best experience out of building one. If I built it, I would know how it all works. I would be able to make sure that it does exactly what I want it to. I would know the code going into it, what it does, and how to change it if I want it to do something else later. And also, I would have another fun electronics project to do at home.
How did I do it?
I'm going to cut through all of the bajillion choices I had to make about what components to buy and where to buy them from, and get right to what I got. The parts I gathered were:
# Items Needed 1 3D printer 16 Mechanical Key switches 16 1N4148 Switching Diodes 6 Salvaged M4/M6 screws 1 Arduino Pro Micro / Leonardo 1 Micro USB Cable
I didn't design the case. I found a great design by another maker over on Thingiverse and asked my friend to print it. We printed the top at 100% infill so it wouldn't flex when using it, and did the rest at 25%. I think the printing went alright, he didn't mention any fails or issues and it is pretty solid now.
Wiring it up
I learned a lot about HIDs (human interface devices) in doing this. If you look at a standard keyboard you'll notice it has a lot of keys. A LOT. Imagine if each one of those keys had it's own dedicated circuit to the controller. It would be a ton of wires. Sure, you could simplify it by having a common ground: You would still have a ton of wires.
Keyboards don't do that, though. There is a better way. Enter the matrix.
Since we are using a microcontroller to gather the input data and pass it on to the computer, we can use a little bit of logic to make the design a little easier. We don't have to cut and solder a bunch of wires, or design a complicated PCB with multiple layers. Ok, I lied earlier, we don't have to make the logic. It is already done and can be repurposed, but you could make your own code for it if you wanted. Keyboards and keypads have been using matrices for circuits for a long time.
Basically, each row is given a charge from the controller. Each column has the pullup resistor enabled, so it can accept a charge and notice that it's there. When a key is depressed, it makes a connection between the row and column and the controller knows the exact location of the key pressed. Neat, huh?
If you want a coded example and some good documentation of the code check out this post over at The Bald Engineer blog. It was really helpful in understanding how keyboards figure it out, even if I didn't structure my code the same way.
By using a matrix, you can have lots of keys but need only a few connections. A 2x2 matrix still needs 4 connections, but a 3x3 only needs 6, a 4x4 only needs 8, a 5x5 only needs 10...the number of keys grows exponentially but the connections to the controller only grows linearly. It's a great system.
The diodes have to be introduced into the matrix to prevent ghosting, which could happen from the charge flowing back another direction. It keeps everything going the correct way.
I saw lots of videos of people doing direct-wire keyboard soldering. One thing they commonly did was to solder the diodes together. I didn't want to do that, because I was worried about uninsulated wires possibly contacting each other inside the build. It could save time, though.
I carefully wrapped one side of the diode around one of the contacts on each key switch, making sure that the diode was pointing in the direction that I wanted the current flow. In retrospect, I should've taken more time to plan out the soldering and which pin to make the diode pin on the switches. It made it a bit difficult to fit the whole thing into the case, and connect to the controller later. The only advice I can give is to draw pictures of what you want, and make sure you are quite aware of how everything is going to go together before you start soldering.
After wrapping the diodes (to create a nice little pocket for solder), I soldered them in place. Then I cut a length of 24AWG electronics wire for the rows. Solid-core works best for this, but I had stranded so I worked with it. I decided it would be easier to burn the insulation with the soldering iron to get to the wire. You can cut it and trim it, but it took way too much time and calculation for me.
I tested each connection by taking aligator clips and connecting them to an LED circuit, introducing the row-end to one side of the circuit and wiring the keyswitch into the circuit. I needed to check the row wire, the diode connection, and the keyswitch itself. So after moving down each row, each switch, and resoldering some weak connections, I decided it was safe to move on.
Then I just had to solder in the wires for the columns. I carefully cut them to length, then weaved them inbetween both the row wires and the pins on the key switches. It helped hold everything in place while I burned off the insulation and soldered the connection. I checked the soldering connections in the same way, this time using the row wire and the column wire instead of using a pin on the switch. This way I could check continuity of the entire system before introducing the microcontroller.
Last, I had to pick some pins on my microcontroller to solder each row and column to. I decided to use opposing sides of the board for ease-of-use. It did make things a bit simpler, but again you should plan this out well in advance. You won't want to change it afterwards, even if you did just solder a pain in your side for actually closing the case.
Solder the connections, secure the microcontroller, secure the top plate of the case, and get ready to code! Continued in "Building a 16-Key MacroPad - Firmware & Software"...