Notes About Working with Various Arduino & Netduino Microcontroller Boards

Tuesday, March 26, 2013

Using Multiple 4x4 Universal 16 Key Keypads for Arduino with an SX1509 16 Output I/O Expander Breakout

I posted on March 20th about using the Sparkfun SX1509 16 Output I/O Expander Breakout to connect a 4 x 4 membrane keypad to an Arduino (Leonardo). If a project needs more than 16 keys, it is very easy to add a second, third, or or fourth keypad. I have two keypads, so this example shows how to use two, but it is easy to extend.

Connections


SX1509   Arduino Leonardo
3.3V     3.3V
nINT     D7
SCL      SCL
SDA      SDA
nRST     D8
GND      GND


The SX1509 breakout board has built-in pull-up resistors for the I2C connection, so external pull-ups aren't needed.


With the keypad face up and the ribbon cable pointing downward the connections are numbered 1 to 8 from left to right.


SX1509   Keypad 1

0        1
1        2
2        3
3        4
8        5
9        6
10       7
11       8


SX1509   Keypad 2
4        1
5        2
6        3
7        4
8        5   -- Note 2 connections to same column
9        6   -- Note 2 connections to same column
10       7   -- Note 2 connections to same column
11       8   -- Note 2 connections to same column

Code


//////////////////////////////////
//// Global Variables  ///////////
//////////////////////////////////
// Here we'll define the number of rows and columns our keypad
//  matrix has. Each of these values can be between 1 and 8.
//  4x4 = 16 buttons
const byte numRows = 8;
const byte numCols = 4;
// This key map will be used to define what keypress is sent to
//  the computer when a key on the keypad is pressed.
char keyMap[numRows][numCols] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'},
  {'E','F','G','H'},
  {'I','J','K','L'},
  {'M','N','O','P'},
  {'Q','R','S','T'}};

// Create a new sx1509Class object
sx1509Class sx1509(SX1509_ADDRESS, resetPin, interruptPin);


In this case, the second keypad serves as the fifth through eighth rows, sharing the same columns as the first keypad (hence the shared connections to the column pins on the SX1509 breakout). The third keypad would be added as columns 5 through 8 to the first four rows. the key map has to have columns and rows that line up, so that unused values are set to a character, even if they aren't used.

Because I wanted to print the output to the serial console and not use it a keyboard input to my computer, I edited these lines in the setup() method.


  #ifdef HID_ENABLED
  Keyboard.begin();
  #else
  Serial.begin(9600);
  #endif

and in the loop() method

  #ifdef HID_ENABLED      
  Keyboard.write(keyMap[activeRow][activeColumn]);
  #else
  Serial.print(keyMap[activeRow][activeColumn]);
  #endif


No comments:

Post a Comment