# Technique to use a matrix keypad from GPIO

**URL:** <https://discourse.nodered.org/t/technique-to-use-a-matrix-keypad-from-gpio/63439>\
**Category:** Hardware\
**Created:** [4 June 2022 06:51 UTC](https://discourse.nodered.org/t/technique-to-use-a-matrix-keypad-from-gpio/63439 "2022-06-04T06:51:41Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![aduroch](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@aduroch](https://discourse.nodered.org/u/aduroch)\
**Post date:** [4 June 2022 06:51 UTC](https://discourse.nodered.org/t/technique-to-use-a-matrix-keypad-from-gpio/63439/1 "2022-06-04T06:51:41Z")

</div>

Hello,  
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.  
Thanks.

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [4 June 2022 07:09 UTC](https://discourse.nodered.org/t/technique-to-use-a-matrix-keypad-from-gpio/63439/2 "2022-06-04T07:09:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![aduroch](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@aduroch](https://discourse.nodered.org/u/aduroch)\
**Post date:** [5 June 2022 03:28 UTC](https://discourse.nodered.org/t/technique-to-use-a-matrix-keypad-from-gpio/63439/3 "2022-06-05T03:28:58Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![aduroch](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@aduroch](https://discourse.nodered.org/u/aduroch)\
**Post date:** [5 June 2022 05:52 UTC](https://discourse.nodered.org/t/technique-to-use-a-matrix-keypad-from-gpio/63439/4 "2022-06-05T05:52:00Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/7/978e6f507e4b67c39b2d5ee5ae953bc67f861d22.png)

It works in mimutes, thanks a lot my friend!

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [5 June 2022 09:40 UTC](https://discourse.nodered.org/t/technique-to-use-a-matrix-keypad-from-gpio/63439/5 "2022-06-05T09:40:59Z")

</div>

I thought that would be the way to do it 🙂

And very impressive that you got it working so quickly 🙂

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

---

<div class="post-metadata">

**Author:** ![aduroch](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@aduroch](https://discourse.nodered.org/u/aduroch)\
**Post date:** [5 June 2022 16:01 UTC](https://discourse.nodered.org/t/technique-to-use-a-matrix-keypad-from-gpio/63439/6 "2022-06-05T16:01:02Z")

</div>

```auto
# 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.setwarnings(False)
GPIO.setmode(GPIO.BCM)

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):
        print(characters[0])
    if(GPIO.input(C2) == 1):
        print(characters[1])
    if(GPIO.input(C3) == 1):
        print(characters[2])
    if(GPIO.input(C4) == 1):
        print(characters[3])
    GPIO.output(line, GPIO.LOW)

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

```

---

<div class="post-metadata">

**Author:** ![aduroch](https://avatars.discourse-cdn.com/v4/letter/a/f14d63/32.png) [@aduroch](https://discourse.nodered.org/u/aduroch)\
**Post date:** [5 June 2022 16:09 UTC](https://discourse.nodered.org/t/technique-to-use-a-matrix-keypad-from-gpio/63439/7 "2022-06-05T16:09:25Z")

</div>

Here is the Node Red related flow code:

```auto
[{"id":"3592981aefea6ca4","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"fe3a5e56bab36dac","type":"exec","z":"3592981aefea6ca4","command":"python3 /home/pi/4x4MatrixKeypad.py","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"]]}]

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [4 August 2022 16:09 UTC](https://discourse.nodered.org/t/technique-to-use-a-matrix-keypad-from-gpio/63439/8 "2022-08-04T16:09:43Z")

</div>

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