Keypad Code


9 posts
by ibanezmatt13 » Sun Jan 13, 2013 10:14 am
Hi,

I have the Adafruit 3x4 Membrane Keypad which is of course in a matrix and has 7 pins. However, I have absolutely no idea how the membrane keypad should be connected to the Pi and plus I do not understand how I should even begin to start writing the code.

How are these things connected and how are they coded for?

Thanks in advance,
Matthew
Posts: 128
Joined: Fri Dec 28, 2012 9:49 am
by -rst- » Sun Jan 13, 2013 12:56 pm
Datasheet and/or example code/schematics on their site?
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'
Posts: 898
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland
by SiriusHardware » Mon Jan 14, 2013 2:21 am
ibanezmatt13 wrote:Hi,

I have the Adafruit 3x4 Membrane Keypad which is of course in a matrix and has 7 pins. However, I have absolutely no idea how the membrane keypad should be connected to the Pi and plus I do not understand how I should even begin to start writing the code.

How are these things connected and how are they coded for?

Thanks in advance,
Matthew


Adafruit should have given you some data. They didn't?

From what you've said I think you understand the concept of a keypad laid out in a matrix of rows and columns. Typically, you would define the GPIO pins connected to the keypad columns as outputs and make the GPIO pins connected to the keypad rows be normally low inputs. You'd then output a logic one to each keypad column in turn, each time reading the row lines to see if any of them were at logic one (if they were, this would indicate that the active column line and the active row line were connected together by a pressed switch).

The Pi has a small number of general purpose I/O pins which you can set to be either input or output - in Python you gain control of these pins by using an add-on library such as rpi.GPIO, and it's up to you which of those GPIO pins become inputs and which are outputs, hence there is no definitive way to connect the keypad. You just pick a wiring scheme yourself.

Search this forum for general information about using the GPIO library in a more basic way, to drive a led on and off, and to read a switch. Put those two basic elements together and you have the means of driving a kepad column line and reading a keypad row line to see if a particular switch is pressed.
Posts: 437
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK
by wirelessmonk » Mon Jan 14, 2013 3:22 am
http://www.adafruit.com/products/419
They normally include links to all related tutorials in their invoices.

Adafruit offer a tutorial for Arduino, maybe that and other resources will help explain how to adapt it for the RPi.
Posts: 47
Joined: Sat Sep 08, 2012 2:58 am
Location: The Colonies
by ibanezmatt13 » Mon Jan 14, 2013 9:08 am
SiriusHardware wrote:
ibanezmatt13 wrote:Hi,

I have the Adafruit 3x4 Membrane Keypad which is of course in a matrix and has 7 pins. However, I have absolutely no idea how the membrane keypad should be connected to the Pi and plus I do not understand how I should even begin to start writing the code.

How are these things connected and how are they coded for?

Thanks in advance,
Matthew


Adafruit should have given you some data. They didn't?

From what you've said I think you understand the concept of a keypad laid out in a matrix of rows and columns. Typically, you would define the GPIO pins connected to the keypad columns as outputs and make the GPIO pins connected to the keypad rows be normally low inputs. You'd then output a logic one to each keypad column in turn, each time reading the row lines to see if any of them were at logic one (if they were, this would indicate that the active column line and the active row line were connected together by a pressed switch).

The Pi has a small number of general purpose I/O pins which you can set to be either input or output - in Python you gain control of these pins by using an add-on library such as rpi.GPIO, and it's up to you which of those GPIO pins become inputs and which are outputs, hence there is no definitive way to connect the keypad. You just pick a wiring scheme yourself.

Search this forum for general information about using the GPIO library in a more basic way, to drive a led on and off, and to read a switch. Put those two basic elements together and you have the means of driving a kepad column line and reading a keypad row line to see if a particular switch is pressed.


Hi, thanks for your reply.

I connected the 7 pins of the keypad directly to different GPIO ports. Then, I tried to run the code that I found on a website and I got very strange results. If I hold the 1 key, I get 1s 2s and 3s being printed on the screen. If I hold the entire top row (1 2 amd 3), I get 1 printed repeatedly. Same applies for other rows. I am really confused. I simply want to press the number 5 and have it print a 5; not over and over again and not random numbers.

Any suggestions?
Thanks
Matthew
Posts: 128
Joined: Fri Dec 28, 2012 9:49 am
by texy » Mon Jan 14, 2013 11:17 am
A common method in the arduino world is to use a resistor ladder and one of the 'analogue' inputs. Of course there is no analogue input with the Pi, but you could wire up an ADC to do the same. Google is your friend, but one example :
http://forums.basicmicro.net/atomnano-f ... t9222.html

Texy
"2.8inch TFT LCD + Touch screen" add-on boards for sale here :
http://www.raspberrypi.org/phpBB3/viewtopic.php?f=93&t=65566
50p goes to the Foundation ;-)
Forum Moderator
Forum Moderator
Posts: 2355
Joined: Sat Mar 03, 2012 10:59 am
Location: Berkshire, England
by -rst- » Mon Jan 14, 2013 2:15 pm
ibanezmatt13 wrote:
SiriusHardware wrote:... keypad laid out in a matrix of rows and columns. Typically, you would define the GPIO pins connected to the keypad columns as outputs and make the GPIO pins connected to the keypad rows be normally low inputs. You'd then output a logic one to each keypad column in turn, each time reading the row lines to see if any of them were at logic one (if they were, this would indicate that the active column line and the active row line were connected together by a pressed switch). ...


I connected the 7 pins of the keypad directly to different GPIO ports. Then, I tried to run the code that I found on a website and I got very strange results. If I hold the 1 key, I get 1s 2s and 3s being printed on the screen. If I hold the entire top row (1 2 amd 3), I get 1 printed repeatedly. Same applies for other rows. I am really confused. I simply want to press the number 5 and have it print a 5; not over and over again and not random numbers.

Any suggestions?


Hmm, 'output a logic one to each keypad column in turn, each time reading the row lines' and 'I hold the 1 key, I get 1s 2s and 3s being printed on the screen' - if the 'previous' outputs are not set (or for some reason don't go) down/low this would seem logical?!

Show us your code! ;)
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'
Posts: 898
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland
by SiriusHardware » Mon Jan 14, 2013 6:55 pm
ibanezmatt13 wrote:
SiriusHardware wrote:
ibanezmatt13 wrote:Hi,

I have the Adafruit 3x4 Membrane Keypad which is of course in a matrix and has 7 pins. However, I have absolutely no idea how the membrane keypad should be connected to the Pi and plus I do not understand how I should even begin to start writing the code.

How are these things connected and how are they coded for?

Thanks in advance,
Matthew


Adafruit should have given you some data. They didn't?

From what you've said I think you understand the concept of a keypad laid out in a matrix of rows and columns. Typically, you would define the GPIO pins connected to the keypad columns as outputs and make the GPIO pins connected to the keypad rows be normally low inputs. You'd then output a logic one to each keypad column in turn, each time reading the row lines to see if any of them were at logic one (if they were, this would indicate that the active column line and the active row line were connected together by a pressed switch).

The Pi has a small number of general purpose I/O pins which you can set to be either input or output - in Python you gain control of these pins by using an add-on library such as rpi.GPIO, and it's up to you which of those GPIO pins become inputs and which are outputs, hence there is no definitive way to connect the keypad. You just pick a wiring scheme yourself.

Search this forum for general information about using the GPIO library in a more basic way, to drive a led on and off, and to read a switch. Put those two basic elements together and you have the means of driving a kepad column line and reading a keypad row line to see if a particular switch is pressed.


Hi, thanks for your reply.

I connected the 7 pins of the keypad directly to different GPIO ports. Then, I tried to run the code that I found on a website and I got very strange results. If I hold the 1 key, I get 1s 2s and 3s being printed on the screen. If I hold the entire top row (1 2 amd 3), I get 1 printed repeatedly. Same applies for other rows. I am really confused. I simply want to press the number 5 and have it print a 5; not over and over again and not random numbers.

Any suggestions?
Thanks
Matthew


Well, now that you have come this far, we need to see your code if we are to help you further: But basically, your programme will need to have three items of information available to it.

1) Which column line it is currently holding in the active state (the others should be inactive).
2) Which row line it is reading an active input state from.

The above, together, give you the information about which physical key is pressed. They are, if you like, the x-y co-ordinates of the key which is pressed.

What they won't tell you yet is the value written on that keytop.

For that, you need to have devised a method - typically a look up table - which you use to translate the row/column co-ordinates into the value written on the key. This may be what the code you found is attempting to do: Without looking at it we can't be sure, so please post it here and we'll see if we can get you up and running.
Posts: 437
Joined: Thu Aug 02, 2012 9:09 pm
Location: UK
by -rst- » Tue Jan 15, 2013 11:13 am
And once again, it might be best to break this problem into smaller pieces - start from a very simple tester setup:

* verify the pins/leads/whatever-connectors on the keypad with a multi-meter and mark them if possible as: column0 (left), column1 (center), column2 (right), row0 (top), row1, row2, row3 (if I understood the connector setup correctly)

* start python in interactive mode
- import gpio lib
- init gpio
- setup the pin connected to column0 as output
- setup the pin connected to row0 as input
- read the input + output result -> verify that low/off
- push the key 1 on the keypad down and repeat read -> should be high/on
- push other keys on the same column and row + read -> should be low/off

Then proceed designing the loops to test any key.

One most likely feasible trick to use, would be to name the outputs and inputs in your code - something like:
Code: Select all
column0 = <GPIO pin id for the one connected to col 0 here>
column1 = ...
column2 = ...
row0 = ...
row1 = ...
row2 = ...
row3 = ...

...
GPIO.setup(column0, GPIO.OUT)
...
GPIO.setup(row0, GPIO.IN)
...should make it easier to check/verify the setup!

When you get to the loops:
Code: Select all
columns = [column0, column1, column2]
rows = [row0, row1, row2, row3]

...
while True:
    for curcol in columns:
        GPIO.output(curcol, GPIO.HIGH)
        for currow in rows:
            inpval = GPIO.input(currow)
            if inpval == GPIO.HIGH:
                button_pressed = button_map[curcol, currow] # or something similar - so that if curcol == 0 and currow == 0, button_pressed = 1
...
(note that I haven't used RPi.GPIO, so some syntax errors more than likely, but should give the idea)
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'
Posts: 898
Joined: Thu Nov 01, 2012 12:12 pm
Location: Dublin, Ireland