Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 1246

Python • RPi 5 GPIOD problems on 4x4 button matrix

$
0
0
Just got my RPi 5 a little while ago and decided to try setting up a 4x4 button matrix I had from my RPi 2 I have not touched in about 7/8 years trying to get back into python. I am trying to convert a script meant for the Raspberry Pico into a functioning script for the RPi5. Since this model can not use rpi.gbio like my RPi 2 and uses the GPIOD instead I have getting a bit confused. Can you no longer pull down pins or set them to high?

Script I am trying to convert:
https://github.com/Guitarman9119/Raspbe ... ad/main.py

Right now when my script runs (below), I can press and hold one of the four columns when I press and hold 8, the second column, when it cycles through the matrix looking for a button pressed, any row on the second column (2, 5, 8, and 0) will say button pressed. If I press the very bottom right button, which is D, when it checks rows A, B, C, D it will say button pressed.

If I set my row_pins type using type=gpiod.LINE_REQ_DIR_IN like my columns are set up, only button 1 works. In rpigbio you could use GPIO.PUD_DOWN/GPIO.PUD_UP and GPIO.LOW/GPIO.HIGH, is this not available with gpiod and can anyone help me fix this?

Code:

import gpiodimport timechip = gpiod.Chip('gpiochip4')# Corresponding buttons on the keypadmatrix_keys = [['1', '2', '3', 'A'],               ['4', '5', '6', 'B'],               ['7', '8', '9', 'C'],               ['*', '0', '#', 'D']]# Pin set up using GPIO pin numberskeypad_rows = [6,13,19,26]keypad_columns = [12,16,20,21]row_pins = []col_pins = []# Define input-rows and output-columnsfor x in range(0,4):    row_pins.append(keypad_rows[x])    row_pins[x] = chip.get_line(keypad_rows[x])    row_pins[x].request(consumer="RButton"[x], type=gpiod.LINE_REQ_DIR_OUT)    row_pins[x].set_value(1)        col_pins.append(keypad_rows[x])    col_pins[x] = chip.get_line(keypad_columns[x])    col_pins[x].request(consumer="CButton"[x], type=gpiod.LINE_REQ_DIR_IN)def scankeys():    for row in range(4):        for col in range(4):            print(matrix_keys[row][col])            time.sleep(.1)            if col_pins[col].get_value()==1:                print("You have pressed", matrix_keys[row][col])while True:    scankeys()for x in range(0,4):    col_pins[x].release()    row_pins[x].release()

Statistics: Posted by eflwi — Sun Nov 24, 2024 5:43 am



Viewing all articles
Browse latest Browse all 1246

Trending Articles