Notes About Working with Various Arduino & Netduino Microcontroller Boards

Wednesday, March 20, 2013

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

I've posted in the past about using the 4x4 universal 16 key keypad for Arduino and the I2C version of using the 4x4 universal 16 key keypad for Arduino. The SX1509 16 output I/O expander and breakout from Sparkfun provides another way to read input from this 4x4 membrane keypad. The SX1509 uses I2C for communication with the Arduino, plus 2 connections for the reset and interrupt lines.

There is an Arduino library for the SX1509 that includes a very nice example of using it for keypad/keyboard input with abundant comments.

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. The key repeat functionality is nicely done, even if it may not be as useful with a keypad as with a full keyboard. 

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


Code


Use the sx1509_keypad example code that comes with the library as a starting point. 

Edit the Global Variables section of the code so it reads like this -


//////////////////////////////////
//// 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 = 4;
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'}};

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

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