Notes About Working with Various Arduino & Netduino Microcontroller Boards

Thursday, March 28, 2013

Using Two 4x4 Universal 16 Key Keypads for Arduino with an Arduino Due

A couple days ago I posted about using a Sparkfun SX1509 16 Output I/O Expander Breakout to connect two 4 x 4 membrane keypads to an Arduino Leonardo or Uno. The breakout board helps by reducing the number of connections between the keypads and the Arduino. If you are working with an Arduino Due, you have 54 digital pins to work with, so connecting directly between the keypads and the Arduino Due is an option in many situations.

Connections


Since this example uses direct connections between the keypads and the Due's pins, there is no doubling up of connections to the same pins for the columns or rows. Two keypads could share connections to the same pins for either the rows or the columns, but not both.  The fact that no row or columns are shared results in the empty quadrants in the key mapping in the code below.

There is a ribbon with 8 wires running from the bottom of the keypad. With the keypad face up, the wires connect in sequence from left to right to Arduino digital pins 2 - 9. Don't use digital pins 0 and 1 on the Arduino Uno, since they are used for serial communication.


Keypad 1  Arduino Due
1         D21
2         D20
3         D19
4         D18
5         D17
6         D16
7         D15
8         D14


Keypad 2  Arduino Due

1         D2

2         D3
3         D4
4         D5
5         D6
6         D7
7         D8
8         D9

Code


The Arduino Keypad library is available from the Arduino Playground.

The following code simply prints the character corresponding to each key press to the serial console.

#include <Keypad.h>

const byte ROWS = 8; 
const byte COLS = 8; 
char keys[ROWS][COLS] = {
  {'1','2','3','A',' ',' ',' ',' '}, // Keypad 1 map
  {'4','5','6','B',' ',' ',' ',' '}, 
  {'7','8','9','C',' ',' ',' ',' '},
  {'*','0','#','D',' ',' ',' ',' '},
  {' ',' ',' ',' ','E','F','G','H'}, // Keypad 2 map
  {' ',' ',' ',' ','I','J','K','L'},
  {' ',' ',' ',' ','M','N','O','P'},
  {' ',' ',' ',' ','Q','R','S','T'}
};
byte rowPins[ROWS] = {21,20,19,18,2,3,4,5}; //connect to row pinouts 
byte colPins[COLS] = {17,16,15,14,6,7,8,9}; //connect to column pinouts

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);
  }
}

No comments:

Post a Comment