Wednesday, October 19, 2011

How To Connect A Keypad To Arduino: Simple Example

Overview
To connect a 12 button keypad to Arduino only requires 7 digital pins, thereby saving resources if you don't need to press more than one button at one time. The keypad has 12 pins - each button makes a connection between a given row pin and a given column pin. For example, if I press button 1 (which is in row 1, column 1), then the pin for row 1 is connected to the pin for column 1. If I let go of button 1, then this connection is broken. In a similar way, if I press the 0 button (which is in row 4, column 2), then the pin for row 4 is connected to the pin for column 2.

By connecting each row and each column pin to the Arduino, we can read which button we are pressing. Imagine that each row pin is an output and each column pin is an input. If we can control which of our row pins are HIGH and which are LOW, then we can read our column pins and see which ones are HIGH and which ones are LOW, thereby indicating the row and the column that is currently pressed (i.e. the key number).

See the code for more detail. Please note that this code is computationally inefficient, and is written the way it is for the sake of clarity.

Hardware Setup




Connect the pin for row 1 to digital pin 2 on the Arduino.
Connect the pin for row 2 to digital pin 3 on the Arduino.
Connect the pin for row 3 to digital pin 4 on the Arduino.
Connect the pin for row 4 to digital pin 5 on the Arduino.
Connect the pin for column 1 to digital pin 6 on the Arduino.
Connect the pin for column 2 to digital pin 7 on the Arduino.
Connect the pin for column 3 to digital pin 8 on the Arduino.



Software
Note that the following code should be copied and pasted into a new Arduino sketch. For ease of reading, please format the code once it has been pasted (Tools > Autoformat).

// How To Connect A Keypad To Arduino: Simple Example
// by Sebastian Tomczak 19 October 2011

// setup value for serial printing
byte value;

// setup pin names and numbers for cols and rows
byte row1 = 2;
byte row2 = 3;
byte row3 = 4;
byte row4 = 5;
byte col1 = 6;
byte col2 = 7;
byte col3 = 8;


void setup() {
Serial.begin(57600);

// setup row outputs
pinMode(row1, OUTPUT);
pinMode(row2, OUTPUT);
pinMode(row3, OUTPUT);
pinMode(row4, OUTPUT);

// setup col inputs
pinMode(col1, INPUT);
pinMode(col2, INPUT);
pinMode(col3, INPUT);

// setup internal resistors
digitalWrite(col1, HIGH);
digitalWrite(col2, HIGH);
digitalWrite(col3, HIGH);

// initialise rows
digitalWrite(row1, HIGH);
digitalWrite(row2, HIGH);
digitalWrite(row3, HIGH);
digitalWrite(row4, HIGH);
}

void loop() {
// read row 1
digitalWrite(row1, LOW); // activate row 1
value = digitalRead(col1); // read row 1 & col 1 = button 1
if(value == 0) { // if the button is pressed...
Serial.println("Button 1 Pressed");
}
digitalWrite(row1, HIGH); // deactivate row 1

// repeat above for buttons 2 - 12 (i.e. rows 1 - 4 and cols 1 - 3)

digitalWrite(row1, LOW);
value = digitalRead(col2);
if(value == 0) {
Serial.println("Button 2 Pressed");
}
digitalWrite(row1, HIGH);


digitalWrite(row1, LOW);
value = digitalRead(col3);
if(value == 0) {
Serial.println("Button 3 Pressed");
}
digitalWrite(row1, HIGH);


digitalWrite(row2, LOW);
value = digitalRead(col1);
if(value == 0) {
Serial.println("Button 4 Pressed");
}
digitalWrite(row2, HIGH);


digitalWrite(row2, LOW);
value = digitalRead(col2);
if(value == 0) {
Serial.println("Button 5 Pressed");
}
digitalWrite(row2, HIGH);


digitalWrite(row2, LOW);
value = digitalRead(col3);
if(value == 0) {
Serial.println("Button 6 Pressed");
}
digitalWrite(row2, HIGH);


digitalWrite(row3, LOW);
value = digitalRead(col1);
if(value == 0) {
Serial.println("Button 7 Pressed");
}
digitalWrite(row3, HIGH);


digitalWrite(row3, LOW);
value = digitalRead(col2);
if(value == 0) {
Serial.println("Button 8 Pressed");
}
digitalWrite(row3, HIGH);


digitalWrite(row3, LOW);
value = digitalRead(col3);
if(value == 0) {
Serial.println("Button 9 Pressed");
}
digitalWrite(row3, HIGH);


digitalWrite(row4, LOW);
value = digitalRead(col1);
if(value == 0) {
Serial.println("Button * Pressed");
}
digitalWrite(row4, HIGH);


digitalWrite(row4, LOW);
value = digitalRead(col2);
if(value == 0) {
Serial.println("Button 0 Pressed");
}
digitalWrite(row4, HIGH);


digitalWrite(row4, LOW);
value = digitalRead(col3);
if(value == 0) {
Serial.println("Button # Pressed");
}
digitalWrite(row4, HIGH);

}

1 comments:

Anonymous said...

Hello, I like your Sketch it works very well. but how do i stop it repeating when i hold a button for to long, can it be made to only print one time until the button is released.
Thanks for a great Sketch, Bob