Using the Keypad library with the adafruit 1x4 membrane keypad

the keypad library is great and all but it seems build exclusively for matrix keypads, rather than single dimensional keypads

specifically, all the examples have this instantiation

#include <Keypad.h>

const byte ROWS = 4; //four rows
const byte COLS = 3; //three columns
char keys[ROWS][COLS] = {
    {'1','2','3'},
    {'4','5','6'},
    {'7','8','9'},
    {'*','0','#'}
};

byte rowPins[ROWS] = {5, 4, 3, 2}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {8, 7, 6}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

i tried dropping the const rows to 1 and cols to 4 and changing the char to be {'1','2','3','4'}, thinking it's just a matter of making the matrix 1D but that doesn't seem to have any effect, pressing keys doesn't output anything to the serial monitor

is this library just not suited to this and i've be better off doing some input pullup in a loop?

Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Can you please post a copy of your sketch, using code tags?

Thanks Tom... :slight_smile:

TomGeorge:
Hi,

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?
Can you please post a copy of your sketch, using code tags?

Thanks Tom... :slight_smile:

it's the adafruit 1x4, i linked it

my crappy hacky sketch was just changing the instantiation section to this

#include <Keypad.h>

const byte ROWS = 1; 
const byte COLS = 4; 
char keys[ROWS][COLS] = {
    {'1','2','3',4'}
};

byte rowPins[ROWS] = {}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9,10,11,12}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

the 4 signal lines of the keypad are in pins 9,10,11,12, with the 5th line being the ground

the full sketch is this, just the customkeypad sketch that comes with the keypad.h library

/* @file CustomKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Demonstrates changing the keypad size and key values.
|| #
*/
#include <Keypad.h>

const byte ROWS = 1; 
const byte COLS = 4; 
char keys[ROWS][COLS] = {
    {'1','2','3',4'}
};

byte rowPins[ROWS] = {}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9,10,11,12}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );; 

void setup(){
  Serial.begin(9600);
}
  
void loop(){
  char customKey = customKeypad.getKey();
  
  if (customKey){
    Serial.println(customKey);
  }
}

Hi,
You need to tell it the row connection, that will be the common to Pin 5, not common to gnd.

byte rowPins[ROWS] = {5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {9,10,11,12}; //connect to the column pinouts of the keypad

Try that... Tom... :slight_smile:

TomGeorge:
Hi,
You need to tell it the row connection, that will be the common to Pin 5, not common to gnd.

byte rowPins[ROWS] = {5}; //connect to the row pinouts of the keypad

byte colPins[COLS] = {9,10,11,12}; //connect to the column pinouts of the keypad




Try that... Tom... :)

there we go, that did it. i just assumed the common would go to ground, wouldn't have thought to put it as the row pin.
The button wiring is strange though. pressing the one key on the membrande keypad outputs the second value (1), two = the first value (0), three is the fourth value (3), and four is the third value (2).

Hi,
That is not unusual for membrane switchs as they are limited in how the tracks can run.

You just need to edit the order in the

char keys[ROWS][COLS] = {
    {'1','2','3',4'}

statement.

Tom... :slight_smile:

Sorry for brinnging this item back...

Is it possible to use the Analog pins (A0...A3) instead of 9...12 ??

If so...
Wich way do they need to be declared??:

byte colPins[COLS] = {A0,A1,A2,A3};
OR
byte colPins[COLS] = {14,15,16,17};

NOTE:
Row pin is 13 in my case, so no problem on it.

Thanks,
Rafa.

YEs. The analog inputs can be used for digital input. You can declare them either way, no difference.