Josh Pigford

Josh Pigford

Maker. Dabbler. I can't stop starting things. Bearded.

Articles, Interviews, Music, Now, Past, Podcasts, Reading, Gear, Toys

How to build a custom media keypad using a Teensy 6-switch PCB

Teensy

I recently started dabbling in the world of mechanical keyboards and, naturally, I wanted to figure out how to make them myself. I didn't want to spend hundreds of dollars on components just to figure out the basics, so I found a handy little kit to get the ball rolling.

My how-to here is based around the PCB included in that kit, though it's not absolutely necessary to get this to work. You'll just have to do more work in attaching keys to the correct pins on the Teensy.

So, what all do you actually need?

  • Teensy 2.0
  • PCB
  • 4.7uF 50V capacitor (1)
  • 2.2K ohm resistors (6)
  • 6 PCB-mount switches (I used Cherry MX's)
  • USB cable (the Teensy uses a mini-USB connector)

Let's get in to the build!

Step 1: Capacitor & Resistors

First thing you need to do is drop in the capacitor in the upper right of the PCB. You'll notice a + and -. The long leg of your capacitor goes in the + and the short leg goes in the -.

Next, you'll see 6 RX blocks on the PCB. This is where those resistors go. Electrical current passes equally in both directions of a resistor (they're "blind to polarity"), so it doesn't matter which way you put them in. My OCD says that you at least need to put them in consistently but reality says otherwise.

Then just solder them in place!

Teensy

Step 2: Teensy

Next up is dropping in the Teensy. The header pins here can get a little tricky as not all of them are needed (and there aren't enough holes in the PCB to just drop all of them in anyways).

What I found worked best for me is to put the necessary header pins in to the PCB and then set the Teensy on top of them and then solder on top of the Teensy first.

After you've got the header pins soldered to the Teensy, you can flip the PCB over and solder them to the PCB itself.

For thoroughness sake, here are the specific pins on the Teensy that need to be soldered in.

  • VCC
  • F0
  • F1
  • F4
  • F5
  • F6
  • F7
  • B7
  • B6
  • B5
  • D7
  • D4
  • D0
  • C7
  • GND

Teensy

Teensy

Step 3: Switches

Now on to the switches! You can use any PCB-mount switches for this. I personally am partial to Cherry MX switches, but whatever clicks your keys.

With PCB-mount switches, there's only one direct you'll be able to put them in to the board, so you can't mess that part up.

Press the switches in to their individual spots. The base of the switch should fit snuggly in to the PCB.

Then, flip the PCB over and there will be 2 pins for each switch that you'll need to solder. You'll see these pins next to the SX labels.

Teensy

At this point, you're done soldering!

Teensy

Step 4: Programming

I default to Arduino when possible and the Teensy is very much compatible in that regard.

The easiest way to do this is to install Teensyduino.

Once you've installed Teensyduino, here's the code you'll want to run.

int pins[] = {16, 17, 18, 19, 20, 21};

#include <Bounce.h>
Bounce button1 = Bounce(16, 10); 
Bounce button2 = Bounce(17, 10);
Bounce button3 = Bounce(20, 10); 
Bounce button4 = Bounce(18, 10); 
Bounce button5 = Bounce(19, 10); 
Bounce button6 = Bounce(21, 10);

void setup() {
  for(int i=0;i<6;i++){
    pinMode(pins[i], INPUT_PULLUP);
  }
}

void loop() {
  button1.update();
  button2.update();
  button3.update();
  button4.update();
  button5.update();
  button6.update();

  if (button1.fallingEdge()) {
    Keyboard.press(KEY_MEDIA_REWIND);
    Keyboard.release(KEY_MEDIA_REWIND);
  }

  if (button2.fallingEdge()) {
    Keyboard.press(KEY_MEDIA_PLAY_PAUSE);
    Keyboard.release(KEY_MEDIA_PLAY_PAUSE);
  }

  if (button3.fallingEdge()) {
    Keyboard.press(KEY_MEDIA_FAST_FORWARD);
    Keyboard.release(KEY_MEDIA_FAST_FORWARD);
  }

  if (button4.fallingEdge()) {
    Keyboard.press(KEY_MEDIA_MUTE);
    Keyboard.release(KEY_MEDIA_MUTE);
  }

  if (button5.fallingEdge()) {
    Keyboard.press(KEY_MEDIA_VOLUME_DEC);
    Keyboard.release(KEY_MEDIA_VOLUME_DEC);
  }

  if (button6.fallingEdge()) {
    Keyboard.press(KEY_MEDIA_VOLUME_INC);
    Keyboard.release(KEY_MEDIA_VOLUME_INC);
  }

}

Now, let's walk through each of those sections.

int pins[] = {16, 17, 18, 19, 20, 21};

This creates a pins array so you can define which pins will be in using during setup().

#include <Bounce.h>
Bounce button1 = Bounce(16, 10); 
Bounce button2 = Bounce(17, 10);
Bounce button3 = Bounce(20, 10); 
Bounce button4 = Bounce(18, 10); 
Bounce button5 = Bounce(19, 10); 
Bounce button6 = Bounce(21, 10);

This includes the Bounce library. This library is already included as part of Arduino, so you won't need to download anything. While it's not absolutely necessary to use Bounce to get your keyboard working, it drastically simplifies things by detecting presses on the switches. So...just use it.

After you include the library, you'll initialize each button by assigning the corresponding pin to a button variable.

The syntax Bounce button1 = Bounce(16, 10); is saying pin 16 is assigned to variable button1 with a sensitivity of 10.

Next up is setup(). This gets run once as the Teensy is booting up.

void setup() {
  for(int i=0;i<6;i++){
    pinMode(pins[i], INPUT_PULLUP);
  }
}

This configures those pins you declared in the pins[] array as "inputs".

Next is the loop(). This is constantly running (10's of thousands of times per second).

You'll need to tell each button variable to update().

button1.update();
button2.update();
button3.update();
button4.update();
button5.update();
button6.update();

These basically make sure the state of each button is kept...updated.

Next is the fun part: assigning what each button does!

if (button1.fallingEdge()) {
    Keyboard.press(KEY_MEDIA_REWIND);
    Keyboard.release(KEY_MEDIA_REWIND);
}

The fallingEdge() function is what detects if the button has actually been pressed down. So if it's been pressed, then execute what's inside the if-statement.

Inside that if-statement are a couple of functions that use the Keyboard library (also included as part of Arduino). These functions tell your computer "press and release key X".

Where you see KEY_MEDIA_REWIND, you can basically drop in any key/scan codes (which you can find a full listing of here).

Once you've got all the code in, simply upload it to your Teensy and you'll be set!

Your computer will likely detect a new keyboard, so just go through the prompts and you'll be good to go!

Keycaps

Your switches obviously lack some keycaps and a case. Both of those are just for aesthetics, but these transparent Cherry MX caps have a removable top that lets you put in a stick/paper/label, which is great for these really custom applications.

Teensy

Next step for me is to model a case for this and 3D print it. One thing at a time. 🙂

Got something to add? Continue the conversation on Twitter: