I have trouble reading out an button matrix with my raspberry pi zero w.
I am using pigpio for reading out the gpios, but I don't think this is a software issue.
This is my source code:
#include <stdio.h>
#include <pigpio.h>
#include <unistd.h>
int main() {
// 2, 3, 4, 14, 15, 18, 17
int gpio_colums[] = {15, 18, 17};
int gpio_rows[] = {2, 3, 4, 14};
if (gpioInitialise() < 0) {
fprintf(stderr, "pigpio initialisation failed\n");
return -1;
}
for (int i = 0; i < 3; i++) {
gpioSetMode(gpio_colums[i], PI_OUTPUT);
}
for (int i = 0; i < 4; i++) {
gpioSetMode(gpio_rows[i], PI_INPUT);
gpioSetPullUpDown(gpio_rows[i], PI_PUD_DOWN);
}
while (1) {
// power every colomn
for (int i = 0; i < 3; i++) {
gpioWrite(gpio_colums[i], 1);
// find pressed button
for (int j = 0; j < 4; j++) {
//printf("gpioRead retunrned %d\n", gpioRead(gpio_rows[i]));
if (gpioRead(gpio_rows[j]) == 1) {
printf("column gpio pin %d, row gpio pin %d\n", gpio_colums[i], gpio_rows[j]);
printf("column %d row %d\n", i, j);
usleep(200);
}
}
gpioWrite(gpio_colums[i], 0);
}
}
return 0;
}
As ouput i get an endeless output.
column 1 row 2
column gpio pin 18, row gpio pin 14
column 1 row 3
column gpio pin 15, row gpio pin 2
column 0 row 0
column gpio pin 15, row gpio pin 3
column 0 row 1
column gpio pin 15, row gpio pin 4
column 0 row 2
column gpio pin 15, row gpio pin 14
column 0 row 3
column gpio pin 18, row gpio pin 2
column 1 row 0
column gpio pin 18, row gpio pin 3
column 1 row 1
column gpio pin 18, row gpio pin 4
column 1 row 2
column gpio pin 18, row gpio pin 14
column 1 row 3
column gpio pin 15, row gpio pin 2
column 0 row 0
column gpio pin 15, row gpio pin 3
column 0 row 1
column gpio pin 15, row gpio pin 4
column 0 row 2
and so on ...
This is the button matrix i am using:
I conneted it from left to right with the gpio pins 2, 3, 4, 14, 15, 18, 17.
I checked for electrical continuity and the button matrix and found with the folloring result.
Button matrix pins (from 0 to 6) continuity if button pressed:
Afer the '|' are the gpios that should be active
0 + 6 = '3' | 2 + 17
1 + 6 = '6' | 3 + 17
2 + 6 = '9' | 4 + 17
3 + 6 = '#' | 14 + 17
0 + 5 = '2' | 2 + 18
1 + 5 = '5' | 3 + 18
2 + 5 = '8' | 4 + 18
3 + 5 = '0' | 14 + 18
0 + 4 = '1' | 2 + 15
1 + 4 = '4' | 3 + 15
2 + 4 = '7' | 4 + 15
3 + 4 = '*' | 14 + 15
To compile the source gcc -o key main.c -lpigpio -lrt -lpthread
What am i doing wrong?
Thx for your help.
if (gpioRead(gpio_rows[i]) == 1)
a typo? Should that beif (gpioRead(gpio_rows[j]) == 1)
(with a j).column 2 row 0
etc.etc. Do you get any compilation errors? Can you post a more complete source listing that can be compiled?