Page 1 of 1

4x4 membrane keypad

Posted: Sat Mar 12, 2016 7:54 pm
by PI_newbee
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 !

Re: 4x4 membrane keypad

Posted: Sun Mar 13, 2016 9:19 am
by ghellquist
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

Re: 4x4 membrane keypad

Posted: Sun Mar 13, 2016 11:56 am
by PI_newbee
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.

Re: 4x4 membrane keypad

Posted: Sun Mar 13, 2016 12:26 pm
by asandford
Sounds like you need to debounce the switches either in software or hardware.

Re: 4x4 membrane keypad

Posted: Sun Mar 13, 2016 12:40 pm
by davenull
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.

Re: 4x4 membrane keypad

Posted: Sun Mar 13, 2016 2:57 pm
by PI_newbee
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 ?

Re: 4x4 membrane keypad

Posted: Sun Mar 13, 2016 3:08 pm
by rzusman
Set all outputs to 1 before you go into your loop.

Re: 4x4 membrane keypad

Posted: Sun Mar 13, 2016 3:42 pm
by PI_newbee
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]

Re: 4x4 membrane keypad

Posted: Sun Mar 13, 2016 4:39 pm
by davenull
[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]

Re: 4x4 membrane keypad

Posted: Sun Mar 13, 2016 7:23 pm
by PI_newbee
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....

Re: 4x4 membrane keypad

Posted: Mon Mar 14, 2016 12:21 am
by Dr. Dante
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

Re: 4x4 membrane keypad

Posted: Mon Mar 14, 2016 12:43 am
by asandford
This video tutorial may help.

Re: 4x4 membrane keypad

Posted: Mon Mar 14, 2016 1:14 am
by mahjongg
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.

Re: 4x4 membrane keypad

Posted: Wed Dec 28, 2016 7:49 am
by KanoMaster22
I think Dr Dante has given you the answer!