ibanezmatt13
Posts: 128
Joined: Fri Dec 28, 2012 9:49 am

Membrane Keypad Circuit Help

Sat Jan 19, 2013 10:24 pm

Hi,

I have had much struggle trying to get my Adafruit 3x4 membrane keypad to work recently, and I am still no further forward. All I need to know now is how I actual connect the thing. It is attached to a 7 pin header: 3 pins for the 3 columns and 4 pins for the 4 rows.

Various tutorials have said that I need to check each row in turn; thankfully, I know the code pretty well. However, I do not know how to connect the keypad.

Firstly, does the keypad require power and ground. Does it need any resistors. At the moment I have simply connected each pin on the header to a different GPIO pin on the Raspberry Pi's GPIO. This seems to give me very weird results.

Am I connecting it wrong; does anybody know of a better way to connect the Adafruit 3x4 membrane keypad with 7 pin header?

Any suggestions will be greatly appreciated.
Regards
Matthew

User avatar
mahjongg
Forum Moderator
Forum Moderator
Posts: 12592
Joined: Sun Mar 11, 2012 12:19 am
Location: South Holland, The Netherlands

Re: Membrane Keypad Circuit Help

Sun Jan 20, 2013 2:04 pm

Matrix keyboards do not need their own power, as they are just switches.
You read them out by outputting an active low signal from the GPIO's on either the row or the column line (whatever is convenient) and read the opposite (column, or row).

Lets say in your case we output on the row lines, and input on the column lines, then the column lines are inputs, and should be pulled up to the GPIO's high voltage, that is in this case 3,3V.
And outputs (for the row lines) are either an output, and active low, or they are not an active low output and then are programmed as passive, that is either disconnected, or simply as inputs!

So normally without any keys pressed all the column lines read as high.
But if you press a key, and the key is in the row which is actively pulled low, then the column in which it is also will be pulled low too.

A trick that is often used is that initially all row lines are pulled low, so if any key is pressed one of the column lines will go low. This means that to wait for a keypress you do not need to scan the rows constantly! After you "see" a keypress you turn off all row lines (return them to inputs, so they no longer pul any row lines low, and then one by one turn a row line low to check in which row the keypress is. after that you can put all rows low and wait for the release of the keypress.
Note that this method demands that only one key at a time is pressed!

ibanezmatt13
Posts: 128
Joined: Fri Dec 28, 2012 9:49 am

Re: Membrane Keypad Circuit Help

Mon Feb 18, 2013 12:43 pm

mahjongg wrote:Matrix keyboards do not need their own power, as they are just switches.
You read them out by outputting an active low signal from the GPIO's on either the row or the column line (whatever is convenient) and read the opposite (column, or row).

Lets say in your case we output on the row lines, and input on the column lines, then the column lines are inputs, and should be pulled up to the GPIO's high voltage, that is in this case 3,3V.
And outputs (for the row lines) are either an output, and active low, or they are not an active low output and then are programmed as passive, that is either disconnected, or simply as inputs!

So normally without any keys pressed all the column lines read as high.
But if you press a key, and the key is in the row which is actively pulled low, then the column in which it is also will be pulled low too.

A trick that is often used is that initially all row lines are pulled low, so if any key is pressed one of the column lines will go low. This means that to wait for a keypress you do not need to scan the rows constantly! After you "see" a keypress you turn off all row lines (return them to inputs, so they no longer pul any row lines low, and then one by one turn a row line low to check in which row the keypress is. after that you can put all rows low and wait for the release of the keypress.
Note that this method demands that only one key at a time is pressed!
Hi, many thanks for your reply.

I now understand these keypads a little better, so thank you. However, despite my bettered understanding, when I run the program I still get numbers printed repeatedly when no numbers are being pressed.

Code: Select all

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(11, GPIO.OUT)
GPIO.setup(12, GPIO.IN)
GPIO.setup(15, GPIO.IN)
GPIO.setup(16, GPIO.IN)
GPIO.setup(18, GPIO.IN)

while True:
    GPIO.output(11, True)

    if GPIO.input(12):
        print '1'
    if GPIO.input(15):
        print '4'
    if GPIO.input(16):
        print '7'
    if GPIO.input(18):
        print '*'
I have set so that for now I only use the first column in my program. GPIO pin 11 is my column 1, and the rest are inputs for rows 1, 2, 3 and 4. The issue is that I don't even know if I have connected it correctly.

I have the column 1 pin connected straight to the GPIO pin, nothing else. I have each of the four rows connected to the Pi's 3.3V each with a 10K pull up resistor. Also, I have put them all to ground. I have no idea whether this is right but something is not right.

I'd be very grateful if you could get back to me on this.

Many thanks
Matt

User avatar
penguintutor
Posts: 376
Joined: Tue May 08, 2012 9:11 am
Location: UK
Contact: Website

Re: Membrane Keypad Circuit Help

Tue Feb 19, 2013 10:20 am

Firstly GPIO 16?

The ports (on revision 2 are)
2, 3, 4, 7, 8, 9, 10, 11, 14, 15, 17, 18, 22, 23, 24, 25, 27

There is no 16.

There are different ways of wiring the switches up depending upon whether you are wanting the pins normally high or low. I think you may have wired for negative input for button pressed, but coded assuming positive input (you didn't say which side of your switch is connected to the input). I'm going to stick to the same logic of high on GPIO11 to enable the column as I think that makes it appear more logical from the programming side.

From GPIO11 connect to column 1 (top end of 4 switches)
The rows (bottom end of the switches) then connect to the GPIO IN ports (12 15 <del>16</del>17 18)

This means that when GPIO is high and the switch is pressed then there will be connection between out and in and hence a high signal on the corresponding input.

But that's not the only thing. If the switch is not pressed then the IN pins are floating, which can give false values. So you need to connect the input pins through a pull-down resistor to ground so that when the switch is not pressed it has a connection to ground. I believe that the GPIO pins all have pull-down resistors that can be enabled internally (the Wiki explicitly lists some of the ports having pull up / down resistors, but doesn't state for the others). Or you can use a real resistor from each of the columns to ground.

Pull down can be enabled during the setup as below:

Code: Select all

GPIO.setup(12, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

rainierez
Posts: 7
Joined: Wed Apr 03, 2013 7:29 pm

Re: Membrane Keypad Circuit Help

Thu May 16, 2013 1:37 pm

I just finished a part of a project using a 3x4. The code on here could be easily expanded to a 4x4 too. Code and details here: http://crumpspot.blogspot.com/2013/05/u ... berry.html

Return to “Beginners”