0

I bought a 1x4 Matrix Keypad https://www.google.com/url?sa=i&url=https%3A%2F%2Fshopee.com.my%2F-Luminense-1x4-Matrix-Memberane-Switch-Keypad-Numpad-Arduino-Raspberry-Pi-i.63040732.4334990197&psig=AOvVaw3QWnV3IR_JBdNoAzRS-wYA&ust=1635615109861000&source=images&cd=vfe&ved=2ahUKEwjk25Wek_DzAhVCsksFHW_dBtsQr4kDegQIARBc for use in my Raspberry pi 4. The problem I'm facing is I do not know how to program it properly, I did try to make my own code (12 in total) but none of them seem to work properly. Sometimes the code runs but doesn't show the input when I press the button and sometimes it just shows different inputs that's not even the same with the keypad numbering. I need to fix this problem ASAP for my final year project as the due date is near. Anybody know how?

This is the 12th code I made for this keypad

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
button1=16
button2=37
button3=31
button4=29
GPIO.setup(button1,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(button2,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(button3,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(button4,GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
while (1):
  if GPIO.input(button1)==1:
    print("1")
  sleep(0.5)
  if GPIO.input(button2)==1:
    print("2")
  sleep(0.5)
  if GPIO.input(button3)==1:
    print("3")
  sleep(0.5)
  if GPIO.input(button4)==1:
    print("4")
  sleep(0.5)

[The input from the program, if i press 1 it gave me 3 as an input.1

I have other codes for this keypad too but this is the one that is close to working I guess. Anyone know how to program one properly?

Wiring:

I use BOARD numbering, white wire is ground (20),button 1 (purple wire) connected at (16),button 2 (blck wire) connected at (37),button 3 (gray wire) connected at (31) and button4 (blue wire) connected at (29).

enter image description here

5
  • 1
    why did you not describe how the keypad is conjected to the RPi? ... also, please include a link to the keypad datasheet ... the link you included tries to redirect to some unknown site ... i will not follow the link
    – jsotola
    Oct 29 at 18:51
  • 1
    pictures of the keypad may help ... top and underside
    – jsotola
    Oct 29 at 18:54
  • ooof my bad did not showed how i connected the RPi, was writing this out of frustration. Anyways i bought a keypad similar to this one adafruit.com/product/1332
    – Zabee123
    Oct 30 at 9:29
  • similar is kind of useless ... but, follow the tutorials at that link
    – jsotola
    Oct 30 at 20:24
  • start with two wires and connect only one switch ... then same two wires and next switch ... repeat until all four switches are tested ... that will also let you determine which pin is the common ... use same two pins on the RPi throughout this test
    – jsotola
    Oct 30 at 20:41
0

You have your GPIO pins configured with (internal) pull-down resistors. Then you are making a circuit where you ground the pin - which is already pulled down - with the keypad switch. Little wonder nothing much happens. Change the pin configs to GPIO.PUD_UP.

p.s. I use the gpiozero package quite a bit and prefer it over the older RPi.GPIO - there's a lot of built-in functionality in gpiozero so you might like to check that out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.