Electronics basics for custom keyboard builders
After reading this article you will know everything about electronics to be able to build a keyboard. I cover the topic from a practical point of view, focus on essential components, save optional parts for another post and skip all the irrelevant stuff for keyboard builders.
Let's dive right in!
As a preface, by custom keyboard I mean a unique, new design (as contrary to assemling one by putting switches on an existing PCB and putting this into a case). In this guide I will focus on a handwired prototype as a first custom keyboard.
Electricity, Voltage, Current
You don't need boring definitions and Ohm's law to build a keyboard.
All you have to know is that your computer's USB port (power source) puts out 5V and your keyboard is happy with that. (5V is too much voltage for LEDs though, but I will cover this later.)
Now you can skip to the next subtitle or can read some boring basics if you insist:
As you may know, electricity is the movement of electrons. In our case the electrons move from the USB port of your computer to the keyboard controller. This is much more predictable and safer than most other electronic projects.
Voltage (expressed in Volts [V]) is the difference in charge between two points. From a USB connector you get a regulated 5V voltage. Regulated simply means it's always 5V. This, again, makes life a lot easier. (On the contrary, eg. batteries' voltage changes during their lifetime.)
Current (expressed in Ampers [A]) is the rate of charge flow. We are working with quite small currents here (in the milliamper [mA] range), not a problem for our wires and components.
(Common microcontrollers are quite happy with any voltage between 2.7 and 5.5V so 5V is totally fine. Development boards (like a Pro Micro) containing a voltage regulator are even more resilient and can run on a 9V battery or even on 12V.)
Theoretically, your computer's USB port acts as an everlasting 5V battery. Thus, when building a keyboard, you can forget about voltage and current (unless you are tinkering with LEDs or wireless boards).
Circuit
A circuit is a bunch of electronic components connected with wires. Your whole keyboard is a rather dumb circuit with nothing else but lots of switches hooked up to a controller.
Electronic components in a circuit are connected with wires (or traces on a PCB). To make the connection permanent you have to solder. (For solderless testing and prototyping a breadboard is an option.)
Tools needed
Soldering iron and stuff (solder wire, resin, tweezer, knife).
A multimeter can come in handy but is not essencial.
I've covered this topic with prices in another article about all the tools needed for keyboard building.
(You definitely DON'T need expensive instruments like an SMD soldering/desoldering station, laboratory power supply or oscilloscope if you only want to build keyboards.)
Soldering & desoldering
Beginners are freaked out by soldering. I've overcomplicated this too. But you shouldn't. Just grab a soldering iron and try soldering some scrap wires or spare components together.
You do this by turning on / heating up your soldering iron (300-350°C), melting solder wire and connecting two components (wires or component legs/pads).
A good solder joint is pretty permanent but you can melt the solder to e.g. pull out and replace a switch. This is desoldering, which can be a mess. A solder sucker may help to get rid of the old solder.
When buyin a soldering iron or soldering station you need nothing fancy. A good pointy tip is more important than the brand, model and marketing bullshit.
Multimeter
A multimeter is your ultimate tool for debugging and problem solving in electronics. It can measure a lot of useful things (voltage, current, continuity, resistance etc.).
If you never make a mistake you don't need a multimeter. In any other case you may consider buying one. It's good to have one at home for non-keyboard related tasks and little fixes.
There are great and cheap multimeters below $20 or even $5. We work with small voltages and currents in a keyboard so almost any model will do the job. However, I wouldn't put a very cheap one in a 110/220V wall socket.
To investigate a switch, keyboard or soldering related problem the continuity feature will be your best friend.
Markings: VVC, GND
Current flows through the circuit so it has to come from a point and has head to another. Like the two ends of a battery.
VVC usually refers to the power source (battery +, USB + etc.). A red wire may indicate this.
GND refers to ground. If you imagine voltage as a rollercoaster or elevation, you see why the reference (zero) voltage is called ground. A black wire may indicate ground.
RST is a pin used to reset the microcontroller. If you connect it to GND (with a piece of wire) the microcontroller gets in bootloader mode for a short time and you are able to upload a new firmware.
Short
If you connect VVC and GND directly (without anything in between) that's called shorting. Try to avoid this since you can damage your components.
Shorting is also used to refer to simply connecting two pins. Like in "short RST and GND" to hard reset a keyboard/controller.
Electronic components
All the funny little things in a circuit or on a printed circuit board. Eg. microcontrollers, resistors, capacitors, diodes, LEDs, switches, displays etc.
You don't need to know all of them. Only a small fraction is needed to build a working keyboard. But you will recognize them in no time anyway.
The same electronic component can have different forms: components can be trough hole (THM) and surface mounted (SMD).
THM vs SMD
Through hole (THM) components end in long "legs" and are soldered directly to wires or are put and soldered into small holes of a PCB. These are the ones used in handwired projects.
Surface mounted (SMD) components can be really tiny, have little pads and are put and soldered on corresponding pads of a PCB. Because of their small size they are often put on PCBs by machines.
Soldering THM components is usually considered much easier while soldering SMD parts may need special tools or machines.
Wires
The sheer number of wire types, gauges, colors etc. can be intimidating.
Do yourself a favor and use single core wires. Much easiert to solder and work with.
Another tip is using wires in more colors. Choosing different colors for rows and columns in a keyboard matrix helps to identify them when things get crowded.
Wire gauge
The thinner the wire the bigger its resistance: it hinders the flow of current. A very thin wire can heat up, melt or burn.
However, in a keyboard we work with 5V and with few mAs so the gauge may be quite small.
The US gauge system uses AWG: the lower the number the thicker the wire.
I use 26 AWG wires because that's what I have at hand. Anything between 22 and 30 AWG would work probably the same.
Handwiring vs. PCB
You do handwiring mostly for prototyping or if you intend to build just a single keyboard. This way you test your concept by soldering the keyboard matrix (see later) with wires. By hand. Handwiring is cheap but tedious and time consuming.
Once you finished and tested your marwellous handwired prototype, you may feel the urge to produce it in larger quantities. Tired of handwiring, you can order a printed circuit board (PCB) from a manufacturer.
That's the same circuit but optimized for manufacturing by machines.
If you are reading this basic guide, PCB design is most likely advanced stuff for you. Nonetheless, you will come across a small PCB even in handwired projects since the microcontroller sits on a small piece of PCB on development boards.
"Controller"
The term "controller" is used quite ambiguously in keyboard building. Both microcontrollers and development boards are referred to by this name.
Microcontroller
The microcontroller is the central element of your circuit. The brain of your project, a really fascinating little thing.
A microcontroller is like a computer on its own with integrated CPU, memory and storage space all in a tiny socket (7x7mm). It runs the firmware (your program) to detect keypresses.
The most popular microcontroller for keyboard projects is the Atmega32U4. It was the first popular one offering sufficient USB capabilities for your electronic project to be recognized as an actual keyboard by your operating system.
Other microcontrollers are available but their features (higher frequency, larger memory, more pins) are mostly overkill for a keyboard project.
Development board (also "controller")
Development boards make programming the microcontroller and access of I/O pins very easy.
Colloquially known as "controllers", they are microcontrollers put on a small piece of PCB surrounded by all the necessary stuff (USB connector, voltage regulator, clock crystal, status LEDs, resistors, capacitors etc.) to be able to work and communicate with it right out of the box.
There are several development boards for keyboard building with Pro Micro being the cheapest ($3) and probably most popular. More expensive ones (eg. Elite-C, Proton C) differ mostly in the connector type (USB micro vs. USB-C) and the number of pins.
The number of easily accessible pins is the most crucial property of a controller/development board.
Pins
I/O pins of the microcontroller have many uses, but in keyboards they are generally communication channels used to detect the pressed state of keys.
I/O (input-output) or GPIO (genaral purpose input-output) hints at the ability to both send and detect signals (some electricity).
The number of pins defines the maximal number of usable keys on your keyboard. While the Atmel32U4 has 26 I/O pins, only 18 of them is made easily accessible on the Pro Micro. To build larger keyboards you may need to hack the Pro Micro or choose another development board with more pins.
Pinout
A pinout is a map of pin names and functions.
Pins are referenced by names (eg. A2, E5) in your firmware code, in documentations and data sheets.
Confusingly, pin names of the microcontroller and markings on the development board may differ. Fortunately, nice pinouts are there to help.
Here is a Atmel32U4 pinout with pin names.
Switches
Keyswitches used in keyboard building are normally open (electricity flow is interrupted) and the circuit is closed (electricity is flowing) only when a key is pressed (and hold).
The controller detects the pressed state of a switch through its pins and guesses the exact key pressed based on its column and row in the keyboard matrix.
Keyboard matrix
The keyboard matrix is a clever concept to detect keypresses of a bunch of switches. The matrix is a grid of colums and rows built from switches and wire hooked up with the controller through its pins.
In most other electronic projects you dedicate a pin to detect the state (on/off) of a single switch. However, in a keyboard we may have 100+ keys and surely more than the 18 pins available on a Pro Micro development board by default.
This is when keyboard matrices come in handy. Instead of wasting one pin for a single key we use one pin for a whole column or row of switches.
This is an important topic and I have articles covering how keyboard matrices work, how many pins you need for a specific matrix and how to optimize matrix structure to save on pins.
–-
The microcontroller sends a burst of voltage through one set of pins (usually row pins) and another set of pins (usually column pins) listens for these signals. Since keyswitches close the circuit when pressed, detection of the sent voltage on another pin indicates a keypress somewhere in the matrix. When nothing is detected no key was pressed (no electricity flows).
The firmware repeats this detection cycle continuously with high frequency and sends the specific keycodes to your OS.
The potential number of switches handled by a matrix is equal to columns x rows, thus with 18 pins you can theoretically handle 9x9=81 keys max. With 20 pins you are up to 100 keys (10x10). Generally, keyboards have less rows than columns. In this case the number of manageable switches would be less (eg. 8x10=80, 7x11=77, 6x12=72, 5x13=65 etc.). Keeping the number of colums close to the number of rows can lead to quite creative matrices. More about keyboard matrices.
Diodes
Diodes let current flow only in one direction. They are one-way conductors and are used to control the direction of current-flow in our keyboard matrix.
We need them to eliminate some flaws of the matrix in certain cases (eg. ambiguous key combinations, ghost keys).
In practice you need to place a diode next to each switch.
1N4148 is the most popular diode type used by keyboard builders. It's dirt cheap btw, $1/200pcs.
When soldering, watch out for the diode direction indicated by a small ring.
Depending on your firmware, both directions may work until you are consistent and they point in the same direction. (You can set this direction as a parameter in QMK or other firmware.)
Firmware
Once your keyboard is finished, you have to upload your firmware ("flash"). This is not electronics anymore but your next step.
You reset your keyboard (bootloader mode) and upload the firmware generated by QMK. This upload/flashing is managed by QMK or you do this with AVRDude/AVRDudess etc.
More on making the keyboard firmware and flashing the keyboard.
Further reading
Pretty much that's everything you'll need to know about electronics to build your first macropad or even a custom keyboard.
In a second part of this article I cover optional electronic components for keyboard builders, like LEDs, displays, rotary encoders, batteries and wireless solutions etc.
In a third part I summarize electronics basics to building split keyboards.