Feedback!

DIY Keyboard

Views: 22402 Difficulty: 2 Status: Complete
Keyboard_kit_closeup

Keyboard kit using CD4021 shift registers and big through hole buttons.

Make your own keyboard! Wiring up 26 buttons to an Arduino can be a big pain and you're almost sure to run out of pins. You can grab a DIY Keyboard in our store and save yourself the trouble! Use it to add text to your next project or hook it up to an Arduino Leonardo and you've got yourself a palm-sized USB keyboard that works with virtually every computer (even and especially a raspberry PI). This kit exclusively uses through-hole components, so it is easy-to-solder and makes a great DIY project for electronics beginners. Want a keyboard, but don't want to solder it? No problem, just select the ready-made version from the drop down menu on the product page and we will solder it for you!
This keyboard uses the CD4021 parallel-in serial-out shift register. Each shift register gives us 8 inputs so to accomodate the English alphabet, we daisy chain four shift registers together to get 32 inputs (the keyboard buttons). We only need 3 pins on the Arduino to comunicate with the keyboard: data, clock, and latch. Want to know more about shift registers? See our shift register tutorial.

The Keyboard DIY

Keyboard_kit_closeup
Commonly used letters like E and T use larger (12mm) buttons and less common letters (in English) use smaller buttons.

Video of the DIY Keyboard

Watch the DIY keyboard interact with an Arduino and a regular computer. You can build this guy and have a little keyboard in your next project.

Step-by-Step Instructions

Step 0 Get the Kit

Grab the DIY Keyboard kit from the LucidTronix store. You will also need a soldering iron, solder, pliers and an Arduino (or your favorite microcontroller) to test it out.

Step 1 Insert Shift Register in the Correct Orientation

Start by inserting one of the CD4021 shift registers. You may want to fold the legs together a little bit using a ruler or stapler so that it slides easily into the PCB. Make sure that pin 1 on the shift register is lined up with the silkscreen 1 label on the PCB. Pin 1 is the pin directly to the left of the U-shaped depression at the top of the PCB.

Step 2 Insert the 3 Remaining Shift Registers

Now insert the other 3 shift registers. Again be careful to ensure that all the orientations are correct. All of the shift registers should be facing in the same direction. Use the image as a guide.

Step 3 Insert 10K Resistor Array in Correct Orientation

Now insert one of the four 10K resistor arrays into the proper place. The resistor array has 9 pins in one row. You must be careful to insert the array in the correct orientation. The pin labelled one on the PCB must be aligned with the triangle arrow on one side of the resistor array. Use the image to guide you.

Step 4 Insert the 3 Remaining Resistor Arrays

Insert the other 3 resistor arrays. Again be careful to make sure you are placing them in the correct direction with the pin labelled 1 lined up with the triangle graphic on the resistor array. All the arrays should be facing in the same direction. Use the image to guide you.

Step 5 Tape the Resistor Arrays Down

This step is optional, but it will probably make your life easier. Tape the resistor arrays down to the PCB so that when you flip it over to solder it they don't fall out. You may want to tape the shift registers down as well, although usually they fit pretty snugly in the PCB.

Step 6 Insert the 300ohm Resistor

Now insert the 300 ohm resistor in the stop labelled 300. This resistor protects the power LED from receiving too much current. Its color bands are orange black brown, and then gold.

Step 7 Solder Components

Now flip the PCB over and solder all of those components down. Make sure you solder every pin, but don't put any solder in the holes for the buttons... we'll get to those next.

Step 8 Insert a Button

Now begin inserting the button in the front of the PCB. Put the larger 12mm buttons in their proper places.

Step 9 Insert All the Buttons

Now put a button in every available slot. Don't worry if some of the buttons stick out a little because of the components coming through the other side. Just try to get everything sitting level and when you solder the buttons down they will stay. We threw in some extra buttons so don't worry if oyu have some left over.

Step 10 Solder The Buttons

Now solder down all of the buttons pins that are sticking through the back of the PCB. Not every button pin will be accessible from the back of the PCB, but most are. The pins that are covered up by the shift registers and be solder from the fron in the next step.

Step 11 Solder Buttons from the Front

Solder those pins that were covered up by shift registers in the back of the PCB from the front of the PCB as shown in the image. Remember its okay if the button is sticking up a little, just try to get i t level and when you solder it down it will be stable.

Step 12 Insert and Solder the Power LED

Insert the power LED in the top of the PCB in the spot labelled "POWER". Make sure to have the shorter leg, the cathode side of the LED closest to POWER label.

Step 13 Insert Headers

Now depending on what you want to do with your keyboard you might want to solder in the 5 male header pins directly to the board. Alternatively, you can solder in 5 hook-up wires and connect these to the Arduino/ or other MCU.

Step 14 Done!

You're all set! Now hook up your DIY Keyboard to any Arduino and make sure the 3 signal wires are set correctly in the code file. You can grab the code from the next section to get started.

Arduino Code for DIY Shift Register based keyboard

Below is a little Arduino Sketch to read keys from the keyboard and print them out on the serial port. If you're doing your own setup, and not using the LucidTronix DIY Keyboard then you may have to fiddle with the array:
char* letters[] ={"yz ---.-","qrstuvwx","ijklmnop","abcdefgh" };
So that the right buttons correspond to the right letters. The heart of the code is the read_keyboard() function which goes like this:
void read_keyboard(){
  digitalWrite(latchPin,0);
  for (int i = 0; i < num_registers; i++){
    button_states[i] = shiftIn(dataPin, clockPin);
  }
  digitalWrite(latchPin,1);
  boolean something_in = false;
  for (int i = 0; i < num_registers; i++){    
    something_in = translate_buttons_2_keys(button_states[i], letters[i]);
    if (something_in) last_key_press = millis(); 
  }
}
This function iterates over the 4 shift registers and reads the data in from each of them. For each shift register it call the translate_buttons_2_keys() function to map a button to a particular character. The translate_buttons_2_keys() function goes like this:
boolean translate_buttons_2_keys(byte buttons, char* keys){
  for(int i = 0 ; i < 8; i++){
     if ( (1 << i) & buttons ){
       Serial.println(keys[i]);
       return true;
     }     
  }
 return false; 
}
This code receives a byte of button states from the read_keyboard() function as well as a string of 8 letters from letters array. If a particular button has been pressed then that bit in the button state byte will be set to 1, otherwise it will be 0. If we encounter a one we print out the corresponding character to the serial port.
Grab the code below:
/* LucidTronix DIY Keyboard
// Print keys to the serial port
// read 4 shift registers in
// http://lucidtronix.com/tutorials/44
*/

int dataPin = 3;
int clockPin = 4;
int latchPin = 5;

unsigned long last_press = 0;
unsigned long last_key_press = 0;

char* letters[] ={"yz ---.-","qrstuvwx","ijklmnop","abcdefgh" };

int num_registers = 4;
byte button_states[] = {0x00,0x00,0x00,0x00};  //01001000

void setup() {
  Serial.begin(9600);
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT); 
  pinMode(dataPin, INPUT);
}

void loop() {
  read_keyboard();
  //Serial.println("Read keyz");
  delay(350);
}

void read_keyboard(){
  digitalWrite(latchPin,0);
  for (int i = 0; i < num_registers; i++){
    button_states[i] = shiftIn(dataPin, clockPin);
  }
  digitalWrite(latchPin,1);
  boolean something_in = false;
  for (int i = 0; i < num_registers; i++){    
    something_in = translate_buttons_2_keys(button_states[i], letters[i]);
    if (something_in) last_key_press = millis(); 
  }
}

boolean translate_buttons_2_keys(byte buttons, char* keys){
  for(int i = 0 ; i < 8; i++){
     if ( (1 << i) & buttons ){
       Serial.println(keys[i]);
       return true;
     }     
  }
 return false; 
}

byte shiftIn(int myDataPin, int myClockPin) { 
  int i;
  int temp = 0;
  int pinState;
  byte myDataIn = 0;
  pinMode(myClockPin, OUTPUT);
  pinMode(myDataPin, INPUT);
  for (i=7; i>=0; i--)
  {
    digitalWrite(myClockPin, 0);
    delayMicroseconds(20);
    temp = digitalRead(myDataPin);
    if (temp) {
      pinState = 1;
      myDataIn = myDataIn | (1 << i);
    }
    else {
      pinState = 0;
    }
    digitalWrite(myClockPin, 1);
  }
  return myDataIn;
}

Parts

Title Description # Cost Link Picture
PCB Keyboard DIY Through Hole Printed circuit board for our DIY Shift register based keyboard. 1 $6.0 Link Keyboard_kit_pcb_back_naked
Resistor Array 8 10K 9 pins Through Hole Resistor Array with 9 pins and 8 resistors through hole, Used in our DIY Keyboard. Value: 10k 4 $0.62 Link Csc_series_b-9-pin
Shift Register CD4021 16-DIP Parallel-in Serial-Out CD4021 Shift Register from Texas Instruments. Through-hole DIP package. Value: Parallel to Serial 4 $0.52 Link Dip16_sot38-1_pkg
Button Through Hole 12mm Through Hole button with 4 legs square 12mm package and black depressor. Value: SPST-NO 8 $0.37 Link Pts125sm85_lfs
Button 6mm Through Hole Tactile Switch Through Hole SPST-NO 0.05A 12V Value: 0.05A @ 12VDC 24 $0.1 Link Screen_shot_2012-12-28_at_7.41.40_pm
Permalink: http://lucidtronix.com/tutorials/44
A QWERTY keyboard you build yourself....
4 Daisy chained CD4021 shift registers gives us 32 inputs....
32 buttons for a fully-functioning hand-held USB keyboard....
Connect shift registers together to get more output and input pins from your microcontroller....
Two LED matrices give 70 LEDs to light up a necklace with messages....
RGB Flashlight, wearable sound art, rainbow lamp....
Shift Registers help you multiply your inputs and outputs....
The heartduino is a heart-shaped arduino clone complete with onboard sensors and a 70 pixel LED matri...