Friday, November 23, 2012

Using a Keypad for Input

Keypads are handy for a variety of projects, from security access to telephone dialing. In this tutorial, we use a Adafruit membrane keypad, and output the button presses to the serial monitor. Nothing fancy, just a platform for future development. Expand the Library in your sketchbook/libraries/ folder. The next step will be to multiplex the lines so we don't use up 7 inputs with a SN74LS151 (reduces Arduino pins to 3, expands inputs up to 16).

You'll need:
Arduino Uno
Breadboard Kit
Membrane Keypad

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
  {'1','2','3'},
  {'4','5','6'},
  {'7','8','9'},
  {'*','0','#'}
};
byte rowPins[ROWS] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
  Serial.begin(9600);
}

void loop(){
  char key = keypad.getKey();

  if (key != NO_KEY){
    Serial.println(key);
  }
}