12 key Matrix-Enabled Keypad Project

— Update —
It only took two years (I’ve been a little busy) but I got this working on my Javelin stamp. The code is ugly. I have to refactor it to make it generic. But it works. Here’s the setup, and me pushing the 7 key (the keypad I got from All Electronics doesn’t have the numbers, so imagine this keypad looks like a telephone touch tone keypad):

The Setup

The code is so simple. Basically, I connect the 7 leads on the keypad to 7 pins on the BOE (0-6 in this case). Since it’s a matrix controller, I verified with my multimeter that pins 1-3 on the keypad are the columns, and 4-7 are the rows. So, if I send +5V down pin 1 and pin 4 has a signal, then I know the 1 key has been pressed. Here’s the matrix:

Column Pin Row Pin Number Pressed
1 4 1
2 4 2
3 4 3
1 5 4
2 5 5
3 5 6
1 6 7
2 6 8
3 6 9
1 7 *
2 7 0
3 7 #

Pins 1-3 will be sent +5V one at-a-time, and pins 4-7 will be checked to see if there is a voltage. I noticed there was some noise on pins 4-7 even when I made them low, so I put a pull down resistor (1k) on each of those pins. The algorithm is so simple. Of course, I leveraged the code I wrote for the Seven Segment LED project to control what number gets displayed on the LED. Note: I can’t really display an asterisk or a pound sign on a seven segment LED, so those keys result in nothing displayed.

If you’re interested, I’ll send you the Javelin code.

This is where my inner geek took over. Basically, the code to make pins 1-3 alternatively high and low, while checking 4-7 for a high signal is in a big loop in the main program. I decided to see how long it takes to execute each loop by making a free I/O pin (there were only two left) high during the execution of one loop, then low the next, and repeat that. I then connected the pin to my scope to measure the time. I should see a nice square wave since there are no inherent asymetries in the code loop. That’s what I (sort of) saw:


Looks like each loop excutes in about 6.75 seconds, since it takes roughly 13.5 seconds for a “cycle”. You just thought I was being geeky. Here’s where it gets scary geeky. Since the main loop does nothing special when no key is pressed, I figured that if I pressed a key, the code to display the appropriate number would be executed, which would slow down the main loop cycle. (hee hee) Sure enough, it looks like my LED display code adds some time to the loop:


About 4.15ms it looks like.

Man, I love this stuff. I think I want to get a job at Parallax.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: