Jump to content

Need help in using 4x4 matrix Keypad on Orangepi Zero GPIO

Recommended Posts

Hi All,


I am trying to use a 4x4 matrix keypad on Orange pi GPIO ,I am connecting the keypad with 8 GPIO's 4 GPIOs for rows, 4 for coloumns ,the problem I am facing is when I try to test the keypad few keys are read only on repeated attempts or pressing. The following is my code. Is this because of Hardware debouncing?. I don't have much knowledge on debouncing. I suspect there is a delay happening in reading and writing GPIOs , because of user process scheduling can be delayed. I don't think accesing GPIO at faster rate is not possible from user application. Please suggest me the best approach.   Please suggest me where I am doing wrong. I appreciate any help, thanks in advance.



#include "gpio_lib.h"
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>


#define SUNXI_PULL_NONE (0)
#define SUNXI_PULL_UP (1)
#define SUNXI_PULL_DOWN (2)

#define  R1  SUNXI_GPG(06)
#define  R2  SUNXI_GPG(07)
#define  R3  SUNXI_GPA(07)
#define  R4  SUNXI_GPA(19)
#define  C1  SUNXI_GPA(18)
#define  C3  SUNXI_GPA(02)
#define  C2  SUNXI_GPA(13)
#define  C4  SUNXI_GPA(10)

#define ROWS 4

#define COLS 4

#define LOW (0)
#define HIGH (1)

#define digitalwrite(pin,val)  sunxi_gpio_output(pin,val)
#define digitalread(pin)  sunxi_gpio_input(pin)
#define pinmode(pin,mode)    sunxi_gpio_set_cfgpin(pin,mode)
#define digitalread(pin) sunxi_gpio_input(pin)


char pressedkey = '\0';
char keys[ROWS][COLS] = {
   {'1', '2', '3', 'A'},
   {'4', '5', '6', 'B'},
   {'7', '8', '9', 'C'},
   {'*', '0', '#', 'D'}

int keypad_init(){
 pinmode(R1, INPUT);
        pinmode(R2, INPUT);
        pinmode(R3, INPUT);
        pinmode(R4, INPUT);
        pinmode(C1, OUTPUT);
        pinmode(C2, OUTPUT);
        pinmode(C3, OUTPUT);
        pinmode(C4, OUTPUT);




int findlowRow()

      if (digitalread(R1) == LOW)
         return 0;
      else if (digitalread(R2) == LOW)
         return 1;
      else if (digitalread(R3) == LOW)
         return 2;
      else if (digitalread(R4) == LOW)
         return 3;
   return -1;

char get_key()
   int rowIndex,ret=1;

      digitalwrite(C1, LOW);

      rowIndex = findlowRow();
      if (rowIndex > -1)
 if (!pressedkey){
            pressedkey = keys[rowIndex][0];
            printf("row is %d\n",rowIndex);
         return pressedkey;

      digitalwrite(C1, HIGH);

      digitalwrite(C2, LOW);

      rowIndex = findlowRow();
      if (rowIndex > -1)
         if (!pressedkey){
            pressedkey = keys[rowIndex][1];printf("row is %d\n",rowIndex);
         return pressedkey;

      digitalwrite(C2, HIGH);
 digitalwrite(C3, LOW);

      rowIndex = findlowRow();
      if (rowIndex > -1)
         if (!pressedkey)
            pressedkey = keys[rowIndex][2];
         return pressedkey;

      digitalwrite(C3, HIGH);

      digitalwrite(C4, LOW);

      rowIndex = findlowRow();
      if (rowIndex > -1)
         if (!pressedkey)
            pressedkey = keys[rowIndex][3];
         return pressedkey;
digitalwrite(C4, HIGH);

   pressedkey = '\0';

   return pressedkey;

int main(void)


      char x = get_key();
      if (x != '\0')
       {  printf("pressed: %c\n",x ); break;


   return 0;



Edited by TRS-80
put into code fence and spoiler tags
Link to comment
Share on other sites

Armbian is a community driven open source project. Do you like to contribute your code?

This topic is now closed to further replies.

  • Create New...