Recycled keypad

VK3ZYZ

Moderator
Staff member
Last Sat. meeting left me with some extra treasures :)
It seems the only part that will be useful, at least at a start, will be the front panel. It measures 22x28CM so is pretty big.

Keypad.jpg
Photo on 3-2-25 at 1.21 pm.jpg


1738549335864.png

So, pins 2,3 and 4 can be connected together, giving a 6 x 4 matrix.
 

VK3ZYZ

Moderator
Staff member
A code example showing the numbers returned for each key.

1738557073530.png

The keyboard matrix is as follows...
Pin1 is Row0.
Pins 2,3 and 4 are Row1.
Pin5 is Row2
Pin 6 is Row3
Pin 7 is Row4
Pin 8 is Row5

Pin 9 is Col0
Pin 10 is Col1
Pin 11 is Col2
Pin 12 is Col3



Code:
#define Version "KeyPad20250203"
int key=0;
int Oldkey = 0;
#define Row0  2
#define Row1  3
#define Row2  4
#define Row3  5
#define Row4  6
#define Row5  7
#define Col0  8
#define Col1  9
#define Col2  10
#define Col3  11
void setup() {
  Serial.begin (9600);
pinMode (Row0,INPUT_PULLUP);
pinMode (Row1,INPUT_PULLUP);
pinMode (Row2,INPUT_PULLUP);
pinMode (Row3,INPUT_PULLUP);
pinMode (Row4,INPUT_PULLUP);
pinMode (Row5,INPUT_PULLUP);
pinMode (Col0,OUTPUT);
pinMode (Col1,OUTPUT);
pinMode (Col2,OUTPUT);
pinMode (Col3,OUTPUT);
digitalWrite(Col0,HIGH);
digitalWrite(Col1,HIGH);
digitalWrite(Col2,HIGH);
digitalWrite(Col3,HIGH);
Serial.println (Version);
}
void loop() {
  get_button();
  while(Oldkey != key)  // loop until twice read the same
    {Oldkey = key;
     get_button();
    }
  if (key!=0)
  { Serial.println(key);
    while(key!=0)
    {get_button();
    }
  }
}
void get_button()
{   key=0;
     digitalWrite(Col0,LOW) ;
     delay(1);
   if (digitalRead(Row0) == LOW) key=1;
   if (digitalRead(Row1) == LOW) key=2;
   if (digitalRead(Row2) == LOW) key=3;
   if (digitalRead(Row3) == LOW) key=4;
   if (digitalRead(Row4) == LOW) key=5;
   if (digitalRead(Row5) == LOW) key=6;
   digitalWrite(Col0,HIGH);
   digitalWrite(Col1,LOW);
        delay(1);
   if (digitalRead(Row0) == LOW) key=7;
   if (digitalRead(Row1) == LOW) key=8;
   if (digitalRead(Row2) == LOW) key=9;
   if (digitalRead(Row3) == LOW) key=10;
   if (digitalRead(Row4) == LOW) key=11;
   if (digitalRead(Row5) == LOW) key=12;
   digitalWrite(Col1,HIGH);
   digitalWrite(Col2,LOW);
        delay(1);
   if (digitalRead(Row0) == LOW) key=13;
   if (digitalRead(Row1) == LOW) key=14;
   if (digitalRead(Row2) == LOW) key=15;
   if (digitalRead(Row3) == LOW) key=16;
   if (digitalRead(Row4) == LOW) key=17;
   if (digitalRead(Row5) == LOW) key=18;
   digitalWrite(Col2,HIGH);
   digitalWrite(Col3,LOW);
        delay(1);
   if (digitalRead(Row0) == LOW) key=19;
   if (digitalRead(Row1) == LOW) key=20;
   if (digitalRead(Row2) == LOW) key=21;
   if (digitalRead(Row3) == LOW) key=22;
   if (digitalRead(Row4) == LOW) key=23;
   if (digitalRead(Row5) == LOW) key=24;
   digitalWrite(Col3,HIGH);
}
The key codes could be "fixed" in the code but I just had the numbers in line.
So, here is some code that does that. The non numerical keys return the original numbers but with 100 added to them.

Code:
#define Version "KeyPadFixed_20250203"
int key=-1;
int Oldkey = -1;
#define Row0  2
#define Row1  3
#define Row2  4
#define Row3  5
#define Row4  6
#define Row5  7
#define Col0  8
#define Col1  9
#define Col2  10
#define Col3  11
void setup() {
  Serial.begin (9600);
pinMode (Row0,INPUT_PULLUP);
pinMode (Row1,INPUT_PULLUP);
pinMode (Row2,INPUT_PULLUP);
pinMode (Row3,INPUT_PULLUP);
pinMode (Row4,INPUT_PULLUP);
pinMode (Row5,INPUT_PULLUP);
pinMode (Col0,OUTPUT);
pinMode (Col1,OUTPUT);
pinMode (Col2,OUTPUT);
pinMode (Col3,OUTPUT);
digitalWrite(Col0,HIGH);
digitalWrite(Col1,HIGH);
digitalWrite(Col2,HIGH);
digitalWrite(Col3,HIGH);
Serial.println (Version);
}
void loop() {
  get_button();
  while(Oldkey != key)  // loop until twice read the same
    {Oldkey = key;
     get_button();
    }
  if (key!=-1)
  { Serial.println(key);
    while(key!=-1)
    {get_button();
    }
  }
}
void get_button()
{   key=-1;
     digitalWrite(Col0,LOW) ;
     delay(1);
   if (digitalRead(Row0) == LOW) key=101;
   if (digitalRead(Row1) == LOW) key=102;
   if (digitalRead(Row2) == LOW) key=103;
   if (digitalRead(Row3) == LOW) key=3;
   if (digitalRead(Row4) == LOW) key=2;
   if (digitalRead(Row5) == LOW) key=1;
   digitalWrite(Col0,HIGH);
   digitalWrite(Col1,LOW);
        delay(1);
   if (digitalRead(Row0) == LOW) key=107;
   if (digitalRead(Row1) == LOW) key=108;
   if (digitalRead(Row2) == LOW) key=109;
   if (digitalRead(Row3) == LOW) key=6;
   if (digitalRead(Row4) == LOW) key=5;
   if (digitalRead(Row5) == LOW) key=4;
   digitalWrite(Col1,HIGH);
   digitalWrite(Col2,LOW);
        delay(1);
   if (digitalRead(Row0) == LOW) key=113;
   if (digitalRead(Row1) == LOW) key=114;
   if (digitalRead(Row2) == LOW) key=115;
   if (digitalRead(Row3) == LOW) key=9;
   if (digitalRead(Row4) == LOW) key=8;
   if (digitalRead(Row5) == LOW) key=7;
   digitalWrite(Col2,HIGH);
   digitalWrite(Col3,LOW);
        delay(1);
   if (digitalRead(Row0) == LOW) key=119;
   if (digitalRead(Row1) == LOW) key=120;
   if (digitalRead(Row2) == LOW) key=121;
   if (digitalRead(Row3) == LOW) key=122;
   if (digitalRead(Row4) == LOW) key=0;
   if (digitalRead(Row5) == LOW) key=124;
   digitalWrite(Col3,HIGH);
}
Note that not all the available key numbers will work as there are keys missing in the matrix. Wiring pins 2,3 and 4 together minimize this but there are still 3 blanks on pin 5.
 
Last edited:

BillC

Active member
A code example showing the numbers returned for each key.

View attachment 3023
The keyboard matrix is as follows...
Pin1 is Row0.
Pins 2,3 and 4 are Row1.
Pin5 is Row2
Pin 6 is Row3
Pin 7 is Row4
Pin 8 is Row5

Pin 9 is Col0
Pin 10 is Col1
Pin 11 is Col2
Pin 12 is Col3



Code:
#define Version "KeyPad20250203"
int key=0;
int Oldkey = 0;
#define Row0  2
#define Row1  3
#define Row2  4
#define Row3  5
#define Row4  6
#define Row5  7
#define Col0  8
#define Col1  9
#define Col2  10
#define Col3  11
void setup() {
  Serial.begin (9600);
pinMode (Row0,INPUT_PULLUP);
pinMode (Row1,INPUT_PULLUP);
pinMode (Row2,INPUT_PULLUP);
pinMode (Row3,INPUT_PULLUP);
pinMode (Row4,INPUT_PULLUP);
pinMode (Row5,INPUT_PULLUP);
pinMode (Col0,OUTPUT);
pinMode (Col1,OUTPUT);
pinMode (Col2,OUTPUT);
pinMode (Col3,OUTPUT);
digitalWrite(Col0,HIGH);
digitalWrite(Col1,HIGH);
digitalWrite(Col2,HIGH);
digitalWrite(Col3,HIGH);
Serial.println (Version);
}
void loop() {
  get_button();
  while(Oldkey != key)  // loop until twice read the same
    {Oldkey = key;
     get_button();
    }
  if (key!=0)
  { Serial.println(key);
    while(key!=0)
    {get_button();
    }
  }
}
void get_button()
{   key=0;
     digitalWrite(Col0,LOW) ;
     delay(1);
   if (digitalRead(Row0) == LOW) key=1;
   if (digitalRead(Row1) == LOW) key=2;
   if (digitalRead(Row2) == LOW) key=3;
   if (digitalRead(Row3) == LOW) key=4;
   if (digitalRead(Row4) == LOW) key=5;
   if (digitalRead(Row5) == LOW) key=6;
   digitalWrite(Col0,HIGH);
   digitalWrite(Col1,LOW);
        delay(1);
   if (digitalRead(Row0) == LOW) key=7;
   if (digitalRead(Row1) == LOW) key=8;
   if (digitalRead(Row2) == LOW) key=9;
   if (digitalRead(Row3) == LOW) key=10;
   if (digitalRead(Row4) == LOW) key=11;
   if (digitalRead(Row5) == LOW) key=12;
   digitalWrite(Col1,HIGH);
   digitalWrite(Col2,LOW);
        delay(1);
   if (digitalRead(Row0) == LOW) key=13;
   if (digitalRead(Row1) == LOW) key=14;
   if (digitalRead(Row2) == LOW) key=15;
   if (digitalRead(Row3) == LOW) key=16;
   if (digitalRead(Row4) == LOW) key=17;
   if (digitalRead(Row5) == LOW) key=18;
   digitalWrite(Col2,HIGH);
   digitalWrite(Col3,LOW);
        delay(1);
   if (digitalRead(Row0) == LOW) key=19;
   if (digitalRead(Row1) == LOW) key=20;
   if (digitalRead(Row2) == LOW) key=21;
   if (digitalRead(Row3) == LOW) key=22;
   if (digitalRead(Row4) == LOW) key=23;
   if (digitalRead(Row5) == LOW) key=24;
   digitalWrite(Col3,HIGH);
}
The key codes could be "fixed" in the code but I just had the numbers in line.
So, here is some code that does that. The non numerical keys return the original numbers but with 100 added to them.

Code:
#define Version "KeyPadFixed_20250203"
int key=-1;
int Oldkey = -1;
#define Row0  2
#define Row1  3
#define Row2  4
#define Row3  5
#define Row4  6
#define Row5  7
#define Col0  8
#define Col1  9
#define Col2  10
#define Col3  11
void setup() {
  Serial.begin (9600);
pinMode (Row0,INPUT_PULLUP);
pinMode (Row1,INPUT_PULLUP);
pinMode (Row2,INPUT_PULLUP);
pinMode (Row3,INPUT_PULLUP);
pinMode (Row4,INPUT_PULLUP);
pinMode (Row5,INPUT_PULLUP);
pinMode (Col0,OUTPUT);
pinMode (Col1,OUTPUT);
pinMode (Col2,OUTPUT);
pinMode (Col3,OUTPUT);
digitalWrite(Col0,HIGH);
digitalWrite(Col1,HIGH);
digitalWrite(Col2,HIGH);
digitalWrite(Col3,HIGH);
Serial.println (Version);
}
void loop() {
  get_button();
  while(Oldkey != key)  // loop until twice read the same
    {Oldkey = key;
     get_button();
    }
  if (key!=-1)
  { Serial.println(key);
    while(key!=-1)
    {get_button();
    }
  }
}
void get_button()
{   key=-1;
     digitalWrite(Col0,LOW) ;
     delay(1);
   if (digitalRead(Row0) == LOW) key=101;
   if (digitalRead(Row1) == LOW) key=102;
   if (digitalRead(Row2) == LOW) key=103;
   if (digitalRead(Row3) == LOW) key=3;
   if (digitalRead(Row4) == LOW) key=2;
   if (digitalRead(Row5) == LOW) key=1;
   digitalWrite(Col0,HIGH);
   digitalWrite(Col1,LOW);
        delay(1);
   if (digitalRead(Row0) == LOW) key=107;
   if (digitalRead(Row1) == LOW) key=108;
   if (digitalRead(Row2) == LOW) key=109;
   if (digitalRead(Row3) == LOW) key=6;
   if (digitalRead(Row4) == LOW) key=5;
   if (digitalRead(Row5) == LOW) key=4;
   digitalWrite(Col1,HIGH);
   digitalWrite(Col2,LOW);
        delay(1);
   if (digitalRead(Row0) == LOW) key=113;
   if (digitalRead(Row1) == LOW) key=114;
   if (digitalRead(Row2) == LOW) key=115;
   if (digitalRead(Row3) == LOW) key=9;
   if (digitalRead(Row4) == LOW) key=8;
   if (digitalRead(Row5) == LOW) key=7;
   digitalWrite(Col2,HIGH);
   digitalWrite(Col3,LOW);
        delay(1);
   if (digitalRead(Row0) == LOW) key=119;
   if (digitalRead(Row1) == LOW) key=120;
   if (digitalRead(Row2) == LOW) key=121;
   if (digitalRead(Row3) == LOW) key=122;
   if (digitalRead(Row4) == LOW) key=0;
   if (digitalRead(Row5) == LOW) key=124;
   digitalWrite(Col3,HIGH);
}
Note that not all the available key numbers will work as there are keys missing in the matrix. Wiring pins 2,3 and 4 together minimize this but there are still 3 blanks on pin 5.
Denys, what an absolute hoot!, hope you can make the screen do something too. Bill.
 

VK3ZYZ

Moderator
Staff member
The displays will need active driving so maybe someone could look at writing some driver code for a Pi Pico or something like that.
I do not think an Arduino Nano would have the memory of grunt to do it.
 

Attachments

VK3ZYZ

Moderator
Staff member
I have the I2C version running after fixing some errors on the test lashup!
Also, the keys now run from 0 to 24.

Photo on 6-2-25 at 1.53 pm.jpg


Code:
#define Version "KeyPadI2C_20250206"

#include "PCF8574.h"
PCF8574 PCF_01(0x3E);                 // set PCH8574 address here.

int key=-1;
int Oldkey = -1;
int KeyTemp = 0;

void setup() {
PCF_01.begin();
Serial.begin (9600);
Serial.println (Version);
}

void loop() {
  get_button();
  while(Oldkey != key)  // loop until twice read the same
    {Oldkey = key;
     get_button();
    }
  if (key!=-1)
  { Serial.println(key);
    while(key!=-1)
    {get_button();
    }
  }
}
void get_button()
{   key=-1;
      PCF_01.write8(B00111111); 
      KeyTemp=PCF_01.read8();
   if (bitRead(KeyTemp,0) == LOW) key=12;
   if (bitRead(KeyTemp,1) == LOW) key=11;
   if (bitRead(KeyTemp,2) == LOW) key=10;
   if (bitRead(KeyTemp,3) == LOW) key=3;
   if (bitRead(KeyTemp,4) == LOW) key=2;
   if (bitRead(KeyTemp,5) == LOW) key=1;
      

      PCF_01.write8(B01111111); 
     KeyTemp=PCF_01.read8();
   if (bitRead(KeyTemp,0) == LOW) key=15;
   if (bitRead(KeyTemp,1) == LOW) key=14;
   if (bitRead(KeyTemp,2) == LOW) key=13;
   if (bitRead(KeyTemp,3) == LOW) key=6;
   if (bitRead(KeyTemp,4) == LOW) key=5;
   if (bitRead(KeyTemp,5) == LOW) key=4;

      PCF_01.write8(B10111111); 
     KeyTemp=PCF_01.read8();
   if (bitRead(KeyTemp,0) == LOW) key=18;
   if (bitRead(KeyTemp,1) == LOW) key=17;
   if (bitRead(KeyTemp,2) == LOW) key=16;
   if (bitRead(KeyTemp,3) == LOW) key=9;
   if (bitRead(KeyTemp,4) == LOW) key=8;
   if (bitRead(KeyTemp,5) == LOW) key=7;

      PCF_01.write8(B11111111); 
     KeyTemp=PCF_01.read8();
   if (bitRead(KeyTemp,0) == LOW) key=24;
   if (bitRead(KeyTemp,1) == LOW) key=23;
   if (bitRead(KeyTemp,2) == LOW) key=22;
   if (bitRead(KeyTemp,3) == LOW) key=21;
   if (bitRead(KeyTemp,4) == LOW) key=0;
   if (bitRead(KeyTemp,5) == LOW) key=20;     
}
 

VK3ZYZ

Moderator
Staff member
Here is a PCB for converting matrix keypads to I2C.
This will handle up to 5x5 matrices.
1738892783764.png


I may get around to sending it off for production soon.
The full 12 pins suit this current keypad.

If one just wanted a 3x3 or up to 4x4 keypad, a circuit just using the PCF8574 would be ok, but I needed a 4x6 matrix. Hence the 74ACT139 to get the extra 2 lines. A 74ACT138 could be used to have an 8x5 matrix running off the 8 pins of the PCF8574 if a bigger keypad is needed, with the code (and PCB) changed to suit.

EDIT: Some PCBs are on order.
 
Last edited:
Top