PI_newbee
Posts: 169
Joined: Mon Aug 26, 2013 7:41 pm

4x4 membrane keypad

Sat Mar 12, 2016 7:54 pm

Hi to all.

Am playing whole afternoon with this problem and i can not figure out what's wrong.
The funny part is that i can not print out numbers or letters, accept characters from one row and one column.

E.g.
If i press 4 , output will be ( depending on button press timer ) 1,4,1,4,7,*,1,4,4,7,* etc.
If i press 5 , output will be ( depending on button press timer ) 2,2,5,8,2,0,5,8,0,5,* etc.

I have try ed different connections,checked the flat cable, wires, resistor ( on input rows ) etc. but i always get the same situation. I guess that the char print out can be controlled with sleep time, but why can i read just the ones from one column and one row ?

Test code:

Code: Select all

#!/usr/bin/python

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

MATRIX = [ [1,2,3,'A'],
           [4,5,6,'B'],
           [7,8,9,'C'],
           ['*',0,'#','D']]

ROW = [33,32,31,29]
COL = [23,21,19,15]

for j in range(4):
    
    GPIO.setup(COL[j], GPIO.OUT)
    GPIO.output(COL[j], 1)

for i in range(4) :
    GPIO.setup(ROW[i], GPIO.IN, pull_up_down = GPIO.PUD_UP)

try:
    while (True):

        # test #
        for j in range(4):
            GPIO.output(COL[j], 0)

            for i in range(4) :
                    if GPIO.input (ROW[1]) == 0:
                         print MATRIX[i][j]
                         while(GPIO.input(ROW[i]) == 0):
                             pass   
            GPIO.output(COL[j],1)
            #time.sleep(0.2)
                                
except KeyboardInterrupt:
    GPIO.cleanup()


Regards !

ghellquist
Posts: 68
Joined: Thu Aug 02, 2012 8:47 am
Location: Stockholm Sweden

Re: 4x4 membrane keypad

Sun Mar 13, 2016 9:19 am

I believe what you experience here is real world vis computer program. In order to read the ROW input you need to wait for the electrical signals to calm down. So insert a short delay after outputting COL until reading ROW.

/Gunnar

PI_newbee
Posts: 169
Joined: Mon Aug 26, 2013 7:41 pm

Re: 4x4 membrane keypad

Sun Mar 13, 2016 11:56 am

Thank you for your suggestion.
Unfortunately this helped a bit and the problem is the same.

If i add some sleep time and if i press 4 i will get 1,4,1,4,1,4,4,1,1,4 etc.
If i add some sleep time and if i press 5 i will get 2,5,5,2,2,5,2,5,5,2 etc.
If i add some sleep time and if i press B i will get A,A,B,A,B,B,A,B,B,A etc.

I can still read just one column and just one row regardless what i press and how fast/slow i press it.
It looks like that the code is not scanning thou whole matrix or there is something wrong with HW.
According the HW connection, all wires are connected to RPI2B GPIO's

G.

asandford
Posts: 1997
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: 4x4 membrane keypad

Sun Mar 13, 2016 12:26 pm

Sounds like you need to debounce the switches either in software or hardware.

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: 4x4 membrane keypad

Sun Mar 13, 2016 12:40 pm

do you think you could mimic or port this Arduino C++ lib (which actualy works fine on all Arduinos, both AVRs and ARM Cortex M3 (DUE):
http://playground.arduino.cc/Code/Keypad#Download
http://playground.arduino.cc/uploads/Code/keypad.zip
:?:

edit - sorry, I was missing that you use Python, not C like I do.
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

PI_newbee
Posts: 169
Joined: Mon Aug 26, 2013 7:41 pm

Re: 4x4 membrane keypad

Sun Mar 13, 2016 2:57 pm

Eh, now am really depressed :-).
I removed the resistors and i connect the wires directly to the RPI board.
I added sleep time, but am still in the same problem.

Maybe it's hw problem ( but i try with mehanic 3x4 and the situation is the same ( just one row and one column can be read out)). I use standard 4x4 membrane keypad and the first 4 lines (from left to right ) are conected to ROW pins (37,35,33,31), other 4 lines to COL pins (40,38,36,32).

I can still read just one column and one row and this is the output:
Pressing * --> A B A B C D A B A B A B A B A B
Pressing 0 --> 6 3 6 3 6 3 6 3 6 3 6 3 6 3 6
Pressing # --> 2 5 8 2 5 2 5 2 5 2 5
Pressing D --> 1 4 1 4 1 4 1 4 7 1 4

Code:

Code: Select all

#!/usr/bin/python

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

MATRIX = [ [1,2,3,'A'],
           [4,5,6,'B'],
           [7,8,9,'C'],
           ['*',0,'#','D']]

ROW = [37,35,33,31]
COL  = [40,38,36,32]

for j in range(4):
    
    GPIO.setup(COL[j], GPIO.OUT)
    GPIO.output(COL[j], 1)

for i in range(4) :
    GPIO.setup(ROW[i], GPIO.IN, pull_up_down = GPIO.PUD_UP)

try:
    while (True):

        # test #
        for j in range(4):
            GPIO.output(COL[j], 0)
            time.sleep(0.1)
            for i in range(4) :
                    if GPIO.input (ROW[1]) == 0:
                         print MATRIX[i][j]
                         while(GPIO.input(ROW[i]) == 0):
                             pass   
            GPIO.output(COL[j],1)
            
                                
except KeyboardInterrupt:
    GPIO.cleanup()

Any ideas ?

rzusman
Posts: 346
Joined: Fri Jan 01, 2016 10:27 pm

Re: 4x4 membrane keypad

Sun Mar 13, 2016 3:08 pm

Set all outputs to 1 before you go into your loop.

PI_newbee
Posts: 169
Joined: Mon Aug 26, 2013 7:41 pm

Re: 4x4 membrane keypad

Sun Mar 13, 2016 3:42 pm

II followed you suggestion but unfortuntally the problem is thisl there.
Just one row / column can be read and in the manner that i described before.

btw. this is the keypad that am using: [url]http%3A%2F%2Festore.digxtech.com%2Fimage%2Fcache%2Fdata%2FProductImages%2FInterfaceBoards%2FKeypads%2F4x4_Custom_Membrane_Keypad_7-600x600.jpg&f=1[/url]

//-------------------------------------
[code]#!/usr/bin/python

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

MATRIX = [ [1,2,3,'A'],
[4,5,6,'B'],
[7,8,9,'C'],
['*',0,'#','D']]

ROW = [37,35,33,31]
COL = [40,38,36,32]

for j in range(4):

GPIO.setup(COL[j], GPIO.OUT)
GPIO.output(COL[j], 1)

for i in range(4) :
GPIO.setup(ROW[i], GPIO.IN, pull_up_down = GPIO.PUD_UP)


try:

GPIO.output(40, 1 )
GPIO.output(38, 1 )
GPIO.output(36, 1 )
GPIO.output(32, 1 )

while True:
# test #
for j in range(4):
GPIO.output(COL[j], 0)
#time.sleep(0.1)
for i in range(4) :
if GPIO.input (ROW[1]) == 0:
print MATRIX[i][j]
while(GPIO.input(ROW[i]) == 0):
pass
GPIO.output(COL[j],1)
#time.sleep(0.2)

except KeyboardInterrupt:
GPIO.cleanup()
[/code]

davenull
Posts: 1159
Joined: Thu Oct 22, 2015 7:22 am
Location: a small planet close to Betelgeuze

Re: 4x4 membrane keypad

Sun Mar 13, 2016 4:39 pm

[offtopic]
if you don't get it working and you don't want to struggle with the libs any more, as there perhaps are no libs at all which work for the Raspi:
I am using this device on my Raspi (and the keypad only for my Arduinos):
http://www.ebay.de/itm/111837318734?_tr ... EBIDX%3AIT
[/offtopic]
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;int main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PointOut(x,y);}}}for(;;);}

PI_newbee
Posts: 169
Joined: Mon Aug 26, 2013 7:41 pm

Re: 4x4 membrane keypad

Sun Mar 13, 2016 7:23 pm

Thank you for suggestion, but i can not use one of those. It must be 3x4 or 4x4 keypad that i can built into existing metal case.
Any other suggestions ?

Btw. i have try to execute one other program that should read 4x4 matrix and in this case i can not get any output....

Dr. Dante
Posts: 1
Joined: Sun Mar 13, 2016 11:59 pm
Location: Aalborg, Denmark

Re: 4x4 membrane keypad

Mon Mar 14, 2016 12:21 am

I have not studied you code in detail, but

Code: Select all

if GPIO.input (ROW[1]) == 0:
looks wrong, try to change 1 (one) to i

asandford
Posts: 1997
Joined: Mon Dec 31, 2012 12:54 pm
Location: Waterlooville

Re: 4x4 membrane keypad

Mon Mar 14, 2016 12:43 am

This video tutorial may help.

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

Re: 4x4 membrane keypad

Mon Mar 14, 2016 1:14 am

please repost your code using the code tag, as without it Pythons indentation becomes lost, which means the meaning of the code gets lost. also you need to repost your url, it seems duckduckgo scrambled it, beyond my ability to repair it.

KanoMaster22
Posts: 196
Joined: Wed Oct 05, 2016 6:06 pm

Re: 4x4 membrane keypad

Wed Dec 28, 2016 7:49 am

I think Dr Dante has given you the answer!

Return to “General discussion”