# Custom keyboard – The problem of power, and backlight

In the previous post, I explained how the backlight can easily be added to our keyboard by using the built-in feature of QMK, and built a prototype that could even change the backlight level based on the active layer by using on single LED – resistor pair on the signal pin.
This setup will not work with a full backlight however. To understand why, we need to do a bit of basic electronics.

Take a 70-key board, with one LED per key. There are many kinds of LEDs out there, but most of the common ones have a 20mA forward current, which means that they reach their maximum brightness at 20mA. If we wanted 70 LEDs at 20mA, we would need 1.4A, which is way more than what the Teensy can deliver on a single pin (60mA at the most).

It actually exceeds the maximum current a USB 2.0 port can deliver (500mA) by a factor of 3! Even USB 3.1 will struggle, as it delivers 1.5A but the Teensy itself will need some current to function. We need to solve for those two issues.

# The power issue

But this led me (pun intended?) to a fact I hadn’t considered before: if I want to build a high-end keyboard will a screen, a trackball, case and backlight RGB LEDs etc, each of those features will consume current, so I need to make sure that I don’t exceed 500mA for a USB 2.0 port or 900mA for a USB 3.0 port. Each feature will have to be designed in a way that doesn’t consume too much energy.

Usually, this is the kind of consumption one can expect:

• A normal keyboard: 50 – 100mA
• A mouse: 15 – 50mA
• Backlight LEDs (single color): to be discussed further
• Backlight LEDs (RGB): to be discussed further
• A USB hub (nothing plugged in): 10-50mA
• A small LCD screen, backlit: 20-150mA

And then there is anything plugged in to the other ports of the USB hub. 500mA is quickly reached! Many devices, such as webcams, register directly as 500mA devices on the computer.

For every iteration of the keyboard, and for each feature added, we’ll have to take the current consumption into account. If we need higher currents, we will have to:

• Have two USB 2.0 plugs in one cable
• Make sure that the keyboard is plugged into USB 3.0, or 3.1

For now, we can simply make sure that we’re not using too much current.

# Back to LEDs

Armed with this knowledge, we can now define what power we will give the LEDs. It will probably be higher than what the Teensy can deliver, but we’ll find a way around this.

## How much should they consume?

I decided that my 70 LEDs (it will depend on the keyboard) will consume 100mA. This means that each LED will have an allowance of 1.43mA. We’re well below the 20mA many common LEDs can use!

A bit of research showed me a few interesting things:

• LEDs vary greatly in their intensity (measured in millicandelas, or mcd). For example, for T-1 package, through-hole LEDs (the ones that fit our switches), Mouser lists LEDs going from 1 mcd to 15000 mcd
• The listed intensity is reached at the forward current rating
• They also vary greatly in their forward current rating (If), the same search yields results from 1mA to 100mA (40% have the 20mA forward current rating)
• The brightness is proportional to the current fed into the LED

From that, we can see that a 20mA, 10000 mcd LED and a 2mA, 500 mcd LED will have the same brightness if we give them both 1mA.
So it looks like brightness is the real question here, and is also a matter of taste. But for a keyboard, we will have to find LEDs which have a decent intensity at the maximum current we want to give them, since QMK supports brightness levels via PWM.

## Experiment

I want to experiment with this, try different LEDs with different ratings, and test the brightness. So I ordered 10 of the Kingbright WP710A10SEC/J4  (20mA, 10000 mcd) and 10 of their WP710A10LSECK/J4 (2mA, 550 mcd).

A quick application of Ohm’s law: I want 1.43mA in 10 LEDs, or 14.3mA total, and the Teensy has a 5V pin.

$\displaystyle R = \frac{U}{I} = \frac{5}{0.0143} = 349.65 \Omega$.

We will round it up to 350Ω.

And let’s build this simple circuit.

Result: both kinds of LEDs produce very similar brightness with the given current. The assumption was right: we can severely under-feed the LEDs and still get a great result.

# Full backlight power

Ok now we have a way to have good backlight with just 100mA of current. This is still to much for just one Teensy pin. We could drop the current even more, but it would limit our choices for LEDs later, and it is generally discouraged to drive power-hungry payloads with a MC pin.

What we need is a way to power the LEDs directly via the USB power line, and have the pin activate a switch that would complete the circuit when we want light. The PWM aspect would still work. Something like this:

Luckily, this kind of thing is pretty common, it’s a simple transistor. There is a very particular kind of transistor that is popular for logic-level voltages with relatively high payloads: MOSFETs. Metal Oxyde Semiconductor Field-Effect Transistors, especially N-channel ones (for reasons I won’t get into in this post, it took some learning), are very simple to use and work perfectly for us. I chose to buy the ON FQP30N06L, a popular model, which is activated by a 5V signal and can handle large payload currents and voltages.

For N-channel MOSFETs, the drain is connected to the payload, and the source is grounded. Our circuit becomes this:

Now we have a LED backlight completely disconnected from the logic circuit. We can easily handle 100mA this way, and have our bright backlight!

This is a good solution for a single-color backlight. In the future, we’ll think about RGB versions.