No announcement yet.

Another Arduino Keypad approach

  • Filter
  • Time
  • Show
Clear All
new posts

  • Another Arduino Keypad approach

    Yeah, I went onto the Arduino site and found a "sketch" for a keypad interface. Unfortunately, the sketch didn't work even though I'm sure that I configured the parameters correctly for the cheap membrane keypad that I am using.

    Sir John's thread was getting so long, and this is too, that I posted it separately.

    From Lawrence C on lake Michigan:
    I ended up writing my own sketch, which was a hell of a lot simpler and took up a lot less memory space than the one on the Arduino site.

    See the attached file which contains all of the 'C' code fragments that makes up the keypad interface that I used. The file holds all of the definitions, variables and code that I use to get input from the keypad.

    To use it, before compiling you set the "KEY_COLS" and "KEY_ROWS" definitions to match the keypad layout, then you set the "keymap" character array values to reflect how the character legends of the keys appear on the keypad and finally you identify the Arduino I/O pin numbers connected to the keypad_rowpins and the keypad_colpins.

    Somewhere in the Arduino initialization code you make a single call to the "keypad_init" function to establish which are the output pins and which are the input pins on the Arduino board. Then somewhere in your main execution loop you poll the keypad by calling the "keyin" function until you get a non-zero value returned. A non-zero value will be returned when a key is pressed and then released. The value returned will be one of the character codes assigned to one of the keys in the "keymap" array.

    Pretty simple huh?

    When I laid out my pc board I forgot to put 1K Ohm pulldowns on the input pins and got random trash on my inputs. So at first I thought it was the software, a little probing with an oscope proved it was a bad circuit design. Once I tacked in some pull-downs, it worked great.

    Don't know if the code will help the guy, but feel free to pass it on if you wish.
    // Define the number of keypad rows and columns
    #define KEY_COLS 4
    #define KEY_ROWS 4

    // ==================================

    // This is the row / column key map array
    char keymap[KEY_ROWS][KEY_COLS] = {
    { '1', '2', '3', 'A' },
    { '4', '5', '6', 'B' },
    { '7', '8', '9', 'C' },
    { '*', '0', '#', 'D' }
    // This byte array lists the keypad row pin numbers
    byte keypad_rowpins[KEY_ROWS] = { 2, 3, 4, 5 };
    // This byte array lists the keypad column pin numbers
    byte keypad_colpins[KEY_COLS] = { 6, 7, 8, 9 };

    // ==============================

    // keypad_init : Initialize pin modes
    void keypad_init(){
    int i, j;
    j = KEY_ROWS;
    if (KEY_COLS > j)
    j = KEY_COLS;
    for (i=0; i<j; i++) {
    pinMode( keypad_rowpins[i], OUTPUT);
    pinMode( keypad_colpins[i], INPUT);

    // keyin: Return the key code of a character being pressed on the keypad
    // If no key is being pressed, then return 0 (zero).
    char keyin() {
    char keycode;
    int irow, icol;
    // The default return keycode is 0 (zero) which
    // means no key is being depressed.
    keycode = 0;
    // Scan each row
    for(irow = 0; irow < KEY_ROWS; irow++) {
    // Place a logic one on a keypad row
    digitalWrite( keypad_rowpins[irow], HIGH);
    // Scan each column
    for(icol = 0; icol < KEY_COLS; icol++) {
    // If the current column is at a HIGH
    if ( digitalRead(keypad_colpins[icol]) ) {
    // Wait 100 milliseconds
    // If the column is still at a logic high
    if (digitalRead(keypad_colpins[icol]) ) {
    // Get the keymap code
    keycode = keymap[irow][icol];
    //Break from the column scan loop
    // If the keycode is greater than 0 (zero)
    if (keycode > 0) {
    // Wait for the key to be released
    while ( digitalRead(keypad_colpins[icol]) ) {
    //Wait for keypad noise to settle
    // Place a logic zero on the keypad row
    digitalWrite( keypad_rowpins[irow], LOW);
    // Return the keycode
    // Place a logic zero on the keypad row
    digitalWrite( keypad_rowpins[irow], LOW);
    } // End of "for(irow=0;i<KEY_ROWS; irow++)"

  • #2
    Sorry, reply was to wrong post.
    Last edited by chucketn; 04-05-2015, 10:18 AM.