Feedback!

DIY Qwerty Keyboard

Views: 13855 Difficulty: 2 Status: Development
Qwerty_kit_main

A QWERTY keyboard you build yourself.

Build your own QWERTY keyboard! You can use it to type messages on an LCD or hook it up to an Arduino Leonardo and have yourself a fully-functioning USB Keyboard that will work with any computer. This DIY kit gives you 32 buttons and only requires 3 wires from your microcontroller. The kit comes with through-hole components that are easy to solder and our tutorial will walk you through the building process step by step. We supply Arduino code below. Grab the DIY Kit in our store, and follow the instructions below to build it yourself. Or just buy it ready-made.

The QWERTY Keyboard Kit

Qwerty_kit_angle
The QWERTY keyboard kit comes with brilliant blue buttons and beautifully silkscreened PCB.

Crazy Fast forward typing!

The QWERTY Keyboard in action! it is hooked up to an HD44780 LCD Screen running code from our word processor tutorial.

Step-by-Step Instructions

Step 0 Insert Shift Register in Correct Orientation

First, insert one of the four CD4021 shift registers into its correct spot on the PCB. Notice the U-shaped depression on one end of the chip, this indicates where Pin 1 is and it should be right next to 1 that is silkscreened onto the PCB. See the image for guidance.

Step 1 Insert the rest of the Shift Registers in the right Orientation

Now insert the 3 remaining shift registers in the proper slots again ensuring that the U-shaped depression lines up with the white 1 silkscreened on the board.

Step 2 Insert the Resistor Array in the Correct Orientation

Now insert one of the 10K ohm resistor arrays in the slot. The resistor arrays are black and have 9 legs in a line. On one end of the resistor array there is a yellowish arrow pointing down. This yellow arrow indicates pin 1. Align this pin with the small 1 silkscreened onto the PCB.

Step 3 Insert the rest of the Resistor Arrays in the Correct Orientation

Now insert the remaining 3 10K ohm resistor arrays in their respective spots. Again be careful to line pin 1 with the 1 silkscreened on the board. The image shows where pin 1 should be for each of the resistor arrays.

Step 4 Tape the Resistor Arrays Down

This step is optional but will make your life easier. Tape the resistor arrays onto the PCB so that when you flip the board over to solder it they don't fall out. The shift registers can be taped down to although they will probably stay in the board because of the tension of their legs.

Step 5 Solder the Components

Now solder the components to the PCB. It is a good idea to use gloves and eye protection. Also make sure your soldering area is well-ventilated.

Step 6 Insert a Button

Insert one of the 6mm push buttons into its spot on the PCB. It should snap in nicely and fit snugly against the board.

Step 7 Insert the rest of the Buttons

Now insert the rest of the 6mm buttons as well as the one larger 12mm button for the spacebar.

Step 8 Solder the Buttons

Flip the PCB over again and solder all of the buttons into place.

Step 9 Add the 300 ohm Resistor

Add the 300 ohm resistor to the bottom corner of the PCB. This resistor protects the power LED from getting too much current. Solder it in then clip off the long legs.

Step 10 Add the Power LED

Add the power LED so that you know when your keyboard has power. The long leg of the LED (the anode end) should be right next to the 'P' in POWER. The cathode end of the LED (the shorter leg) should be closer to the corner of the PCB.

Step 11 Solder the Headers

Now depending on how you want to use your keyboard you can solder some hookup wire to the 5 broken out pins at the top of the PCB. If you want to use your keyboard with a solderless breadboard than you should solder male headers into these pins as shown in the image. Or, you can also solder female headers or hook up wire directly to these pins.

Shift Register Keyboard Arduino code

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.
The last piece of code to point out is the button debouncing achieved in the line:
     if (millis() - last_key_press > 300 ) read_keyboard();
The variable last_key_press holds the milliseconds elapsed since the start of the sketch when a button was pressed. Before we read from the keyboard we check if at least 300 milliseconds have passed since we last recorded a press of a button. This ensures we wont send mulitple copies of the same key just because the Arduino called the read_keyboard() function before the user had a chance to lift their finger from the keyboard.
Grab the code below:
/* LucidTronix DIY QWERTY Keyboard
// Print keys to the serial port
// read 4 shift registers in
// http://lucidtronix.com/tutorials/53
*/

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

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() {
  if (millis() - last_key_press > 300 ) read_keyboard();

}

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]);
       // If using Arduino Leonardo then following 
       // line will make the Arduino act as a USB Keyboard
       // Keyboard.print(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;
}

QWERTY Keyboard EAGLE Files

Click on the button below to download EAGLE files for the DIY QWERTY Keyboard. We include both the electrical schematic file (.sch) and the Board outline file (.brd).
Click Here to Download: QWERTY Keyboard EAGLE Files

Parts

Title Description # Cost Link Picture
PCB QWERTY keyboard kit This board holds the 32 buttons arranged in the common QWERTY format. 1 $6.0 Link Qwerty_kit_pcb
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 1 $0.37 Link Pts125sm85_lfs
LED GREEN 3MM 568NM 1 $0.13 Link Wp710a10gd_sml
Button 6mm Through Hole Tactile Switch Through Hole SPST-NO 0.05A 12V Value: 0.05A @ 12VDC 31 $0.1 Link Screen_shot_2012-12-28_at_7.41.40_pm
Resistor RES 300 OHM 1/4W 5% CARBON FILM Value: 300 1 $0.08 Link Screen_shot_2012-12-28_at_7.28.01_pm
Permalink: http://lucidtronix.com/tutorials/53
Keyboard kit using CD4021 shift registers and big through hole buttons....
32 buttons for a fully-functioning hand-held USB keyboard....
Two LED matrices give 70 LEDs to light up a necklace with messages....
Connect shift registers together to get more output and input pins from your microcontroller....
4 Daisy chained CD4021 shift registers gives us 32 inputs....
Write messages on Liquid Crystal Displays with potentiometers or keyboards....
RGB Flashlight, wearable sound art, rainbow lamp....
The heartduino is a heart-shaped arduino clone complete with onboard sensors and a 70 pixel LED matri...