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.


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


//// 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] = {

// 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

and in the loop() method

  #ifdef HID_ENABLED      

No comments:

Post a Comment