Technique to use a matrix keypad from GPIO

I'm having a hardtime configuring an hardware matrix keypad 4x4 within Node Red, using GPIO on a raspberry PI 3B+ All the hardware is set and works good. I use Gpio Input pull-down for rows and Gpio Output for Columns, initial level high, but I need some logic to use it in Red Node. I just want to press on digit on the keypad and get a payload of that digit in a debug node to start. A working solution example would be great. I tried join payload to create a binary string that I can compare to a known value but it always finish with a lot of complexity and it is not so much reactive.

I'm thinking that if I wanted to do this, I'd write a little python program to do the actual matrix polling (should be examples of that out there) and then launch it via exec node and monitor it's output

1 Like

I've never used anything outside node red using simple node for programming but I'll have a look on your suggestion. If I remembered, I saw something programmed with an array and interfacing directly with gpio, using terminal console but I don't know if it was programmed in Python, which I don't know nothing about but I'm very open to put my hands on it. Thanks!

It works in mimutes, thanks a lot my friend!


I thought that would be the way to do it :slight_smile:

And very impressive that you got it working so quickly :slight_smile:

Can you post the python code here just in case someone needs it in future?

# This example is a hello world example
# for using a keypad with the Raspberry Pi

import RPi.GPIO as GPIO
import time

L1 = 5
L2 = 6
L3 = 13
L4 = 26

C1 = 23
C2 = 24
C3 = 25
C4 = 12


GPIO.setup(L1, GPIO.OUT)
GPIO.setup(L2, GPIO.OUT)
GPIO.setup(L3, GPIO.OUT)
GPIO.setup(L4, GPIO.OUT)

GPIO.setup(C1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(C4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)

def readLine(line, characters):
    GPIO.output(line, GPIO.HIGH)
    if(GPIO.input(C1) == 1):
    if(GPIO.input(C2) == 1):
    if(GPIO.input(C3) == 1):
    if(GPIO.input(C4) == 1):
    GPIO.output(line, GPIO.LOW)

    while True:
        readLine(L1, ["1","2","3","A"])
        readLine(L2, ["4","5","6","B"])
        readLine(L3, ["7","8","9","C"])
        readLine(L4, ["*","0","#","D"])
except KeyboardInterrupt:
    print("\nApplication stopped!")

Here is the Node Red related flow code:

[{"id":"3592981aefea6ca4","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"fe3a5e56bab36dac","type":"exec","z":"3592981aefea6ca4","command":"python3 /home/pi/","addpay":"","append":"","useSpawn":"true","timer":"","winHide":false,"oldrc":false,"name":"","x":450,"y":240,"wires":[["6b2163f63c9d2678"],[],[]],"info":"# This example is a hello world example\r\n# for using a keypad with the Raspberry Pi\r\n\r\nimport RPi.GPIO as GPIO\r\nimport time\r\n\r\nL1 = 5\r\nL2 = 6\r\nL3 = 13\r\nL4 = 26\r\n\r\nC1 = 23\r\nC2 = 24\r\nC3 = 25\r\nC4 = 12\r\n\r\nGPIO.setwarnings(False)\r\nGPIO.setmode(GPIO.BCM)\r\n\r\nGPIO.setup(L1, GPIO.OUT)\r\nGPIO.setup(L2, GPIO.OUT)\r\nGPIO.setup(L3, GPIO.OUT)\r\nGPIO.setup(L4, GPIO.OUT)\r\n\r\nGPIO.setup(C1, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)\r\nGPIO.setup(C2, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)\r\nGPIO.setup(C3, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)\r\nGPIO.setup(C4, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)\r\n\r\ndef readLine(line, characters):\r\n    GPIO.output(line, GPIO.HIGH)\r\n    if(GPIO.input(C1) == 1):\r\n        print(characters[0])\r\n    if(GPIO.input(C2) == 1):\r\n        print(characters[1])\r\n    if(GPIO.input(C3) == 1):\r\n        print(characters[2])\r\n    if(GPIO.input(C4) == 1):\r\n        print(characters[3])\r\n    GPIO.output(line, GPIO.LOW)\r\n\r\ntry:\r\n    while True:\r\n        readLine(L1, [\"1\",\"2\",\"3\",\"A\"])\r\n        readLine(L2, [\"4\",\"5\",\"6\",\"B\"])\r\n        readLine(L3, [\"7\",\"8\",\"9\",\"C\"])\r\n        readLine(L4, [\"*\",\"0\",\"#\",\"D\"])\r\n        time.sleep(0.1)\r\nexcept KeyboardInterrupt:\r\n    print(\"\\nApplication stopped!\")\r\n\r\n"},{"id":"3db7029b33f11981","type":"debug","z":"3592981aefea6ca4","name":"Result","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":1010,"y":220,"wires":[]},{"id":"03bfe09bf33f8b06","type":"inject","z":"3592981aefea6ca4","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payloadType":"date","x":160,"y":240,"wires":[["fe3a5e56bab36dac"]]},{"id":"6b2163f63c9d2678","type":"split","z":"3592981aefea6ca4","name":"","splt":"↵","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":790,"y":220,"wires":[["3db7029b33f11981"]]}]
1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.