Arduino

Arduino Tutorial #19 How to connect keyboard with Arduino using single wire

How to connect keyboard with arduino using single wire

Hi guys in this tutorial we will learn how to connect keyboard with Arduino using single wire. And sketch we are using in this, is not depend upon the library.  So let’s get started.

For this you will need

  1. Arduino,
  2. 4 X 4 keypad,
  3. Breadboard,
  4. Jumper wires,
  5. Assorted Resistors.

 

keypad pin Rows and Columns
keypad pin Rows and Columns

Let us first understand how keyboard works. It has four rows and four column. The leftmost pins are for Rows and rightmost for Columns. For example if you pressed five number on keypad it will make connection between column 2 and row 2.  Similarly if you pressed number 8 it will make connection between column 2 and row 3. This column and row connection gives signal to Arduino which button is pressed. In this tutorial we will see how to connect keyboard with single wire in Arduino.

keypad connection when 5 pressed
keypad connection when 5 pressed

 

keypad connetion when 8 pressed
keypad connetion when 8 pressed

 

Circuit diagram for keyboard connection using single wire
Circuit diagram for keyboard connection using single wire

Do connection as shown in diagram.  You can see there are various resistor is used. Because we want to connect with signal wire. So we need to differentiate output between each button connection. You can use any resistor but make sure its output value should not matched with any other keypad button value. I will show you exactly what I am talking about later in this lesson. If you are using different resistors you need lots of trial and error for getting particular output for each keypad character. When you pressed number 1 on keypad it will connect row 1 and column 1. From our circuit diagram we can see current is going through two resistor then a fixed resistor 10k ohms. By voltage divider we can differentiate each key pressed value. Similarly it is done for other keypad character.

 

Sketch:
char* keypressed = 0;
int keyboardPin = A0;
int keyboardValue = 0;

void setup() {
 Serial.begin(9600);
}

void loop() {

keyboardValue = analogRead(A0);

while(keyboardValue<25) {
 // do nothing utnil key is pressed
 keyboardValue = analogRead(A0);
 delay(50); 
 }
 readkeyboard();
}
void readkeyboard(){
 keyboardValue = analogRead(keyboardPin);
 Serial.print("pin value");
 Serial.println(keyboardValue);
 if ((keyboardValue <599) && (keyboardValue> 583)){keypressed = "0";}
 if ((keyboardValue <124) && (keyboardValue> 106)){keypressed = "1";}
 if ((keyboardValue <160) && (keyboardValue> 150)){keypressed = "2";}
 if ((keyboardValue <150) && (keyboardValue> 137)){keypressed = "3";}
 if ((keyboardValue <210) && (keyboardValue> 183)){keypressed = "4";}
 if ((keyboardValue <314) && (keyboardValue> 303)){keypressed = "5";}
 if ((keyboardValue <300) && (keyboardValue> 288)){keypressed = "6";}
 if ((keyboardValue <276) && (keyboardValue> 250)){keypressed = "7";}
 if ((keyboardValue <555) && (keyboardValue> 537)){keypressed = "8";}
 if ((keyboardValue <505) && (keyboardValue> 480)){keypressed = "9";}
 if ((keyboardValue <290) && (keyboardValue> 264)){keypressed = "*";}
 if ((keyboardValue <526) && (keyboardValue> 513)){keypressed = "#";}
 if ((keyboardValue <68) && (keyboardValue> 58)){keypressed = "A";}
 if ((keyboardValue <86) && (keyboardValue> 70)){keypressed = "B";}
 if ((keyboardValue <93) && (keyboardValue> 85)){keypressed = "C";}
 if ((keyboardValue <106) && (keyboardValue> 92)){keypressed = "D";}

while (keyboardValue> 25) {
 delay (100);
 keyboardValue = analogRead((A0));
 }
 Serial.println(keypressed);
 delay(1000);
}

 

Now come to the programming part. Define keypressed is character. A0 pin will read the keyboard value. Define keyboardValue as an integer. In setup function serial.begin will start serial communication between Arduino and computer. In loop function keyboardvalue equal to analogread A0. Whatever value A0 will get it will assign to keyboard value. While loop will run until the condition inside parenthesis become false.  So we have set condition keyboardvalue is less than 25 then execute the code inside the curly bracket. It will again check the value of keyboardvalue and take a delay of 50 millisecond. This code execute over and over again until the keyboard value will be greater than 25. Serial.print will print pin value and serial.println will print keyboard value. This two line is added in sketch for determining the range of each key value in keyboard. As we are using single wire so our output for each key should be different therefore Arduino can recognize particular character is pressed. So we will find the range. Upload this sketch and pressed the key for example I am pressing 1 it is showing some pin value. It is not exact same for each time. So take its range where it is highest value and lowest value. Simultaneously edit the code for 1 and 2 and so on. After giving range for each key you can delete this two line if you want only character as an output.  Here we are using if condition. If keyboard value ranges between particular values is true then Keypressed is set to show particular character. This values may be different for you if you use different resistor then this. I have also check values with voltage divider formula and with serial monitor it was showing 5 to 10 values difference between them. Values may be different because of resistor physical accuracy. So check each value properly. Otherwise you will get error like you are pressing 1 you got 3. If you are getting this error. Correct the range in code for that particular character. If this is not working try to change the resistor in that particular row or column. Finally serial.println will print the key pressed value. Delay is for 1 second. If you reduce below one second you will see your value is printed more than one time. So one second is sufficient for this.  As you can see when I pressed the key it is showing exact character which I pressed.

 

LIST OF COMPONENT BUY ONLINE:

http://amzn.to/2fvSRJq   (Arduino)

http://amzn.to/2wjd7p4 (Keypad 4×4)

http://amzn.to/2wxPmWz  (Breadboard)

http://amzn.to/2vJ3lvo   (Jumper wire)

http://amzn.to/2vmSK8l  (Resistor)

 

KEEP LEARNING KEEP MAKING 🙂

 

Leave a Reply

Your email address will not be published. Required fields are marked *