Go Down

Topic: How to use this keypad? (Read 1 time) previous topic - next topic

feveran

Hi there, i bought this http://dx.com/p/5-io-keyboard-matrix-buttons-module-blue-150608
can someone tell me how to interface this keypad with arduino mega?

Boffin1

is there a datasheet with it?

It says only 5 I/O lines so should be easy
With my mobile phone I can call people and talk to them -  how smart can you get ?

mstanley

This is a very interesting piece of kit.  A PDF of the schematic is here http://www.wvshare.com/downloads/accBoard/5-IO-Keypad.7z.

Because this board contains keys and a joystick you'll need to be just a bit more careful with your programming logic.  But in general what you need to do is...

1.  Wire up the board. Leave board pin 1 unconnected (it doesn't do anything). Connect board pin 2 to arduino GND pin.  Connect board pins IO1 thru IO5 to arduino pins 2 thru 6 them in any order.

2.  In setup() you'll need to enable the internal pullup resistors with pinMode(pinNum, INPUT_PULLUP).

3.  Create a readAllPins() function that does a digitalRead() on each of the arduino pins 2 thru 6 one at a time.

4.  Create a scanKeys() function that does a digitalWrite(pinNum, LOW) on each of the arduino pins 2 thru 6 one at a time and in order.  Then for each arduino pin that you digitalWrite(pinNum, LOW) call your readAllPins() function and store the values into a variable.

5.  Inside loop():  Call your read function first and if any pin is low that means the joystick is being used. You can tell which direction by the arduino pin number that reads low.  If all pins are high then call your scanKeys() function to see if any buttons are being pressed.

You can't use the keys and the joystick at the same time and there's no debouncing. All that can be fixed later once you are more familiar with what I wrote above.

Let me know if you need more detail.  I wouldn't mind playing with the code for this.


feveran

Thanks  a lot  :)  . I will try and inform you.

feveran

Hi again, i think i made very big mistake  :P
but very confused now.
What is your advice to make this code more to work?

int val = 0;
int val1 = 0;
int val2 = 0;
int val3 = 0;
int val4 = 0;

void setup() {
  Serial.begin(9600);
  pinMode(30, INPUT_PULLUP);
   pinMode(31, INPUT_PULLUP);
    pinMode(32, INPUT_PULLUP);
     pinMode(33, INPUT_PULLUP);
      pinMode(34, INPUT_PULLUP);

}
void readAllPins()
{

  val=digitalRead(30);
    val1=digitalRead(31);
      val2=digitalRead(32);
        val3=digitalRead(33);
          val4=digitalRead(34);
}
void  scanKeys()
{
   digitalWrite(30, LOW);
   readAllPins();
    digitalWrite(31, LOW);
       readAllPins();
     digitalWrite(32, LOW);
        readAllPins();
      digitalWrite(33, LOW);
         readAllPins();
       digitalWrite(34, LOW);
          readAllPins();
}
void loop() {
   readAllPins();
   if(val||val1||val2||val3||val4==LOW){
     
   }
    if(val&&val1&&val2&&val3&&val4==LOW){
     scanKeys();
     Serial.println(String(val)+String(val1)+String(val2)+String(val3)+String(val4));
   }
}

mstanley

Hi feveran,

 I wonder if you could tell me what it is you want to accomplish?  Do you want to learn how to make this hardware work or are you just interested in using it for your project?  I can help either way but I am asking because I don't want to take over your project.  I can write the code to make the buttons and joystick work but if that is what you are interested in doing then I can teach you how to write the code and understand the hardware.

 What do you think?

-Mark

feveran

In fact i will use this in my project. I will use this keypad for getting command from user. The keypad is my project's user input device but i never use this before. If you give me more detail, i can understand and use this i think :)

Joe Lima

#7
Jul 17, 2013, 08:07 am Last Edit: Jul 17, 2013, 08:14 am by JoeLima Reason: 1
Hi everyone (specialy mstanley),

Sorry to reply here, but I didn't want to start a new topic since mine is the same issue reported here. I mean, I'm trying to use the very same Keyboard Module and also with an Arduino Mega. I am a programmer, but I didn't follow the logic you proposed above. i tried to code your suggestion, but I could not read the push buttons, only the joystick buttons. I need to know why is necessary to call digitalWrite(pinNum, LOW) to read the push buttons since the joystick buttons are working...  Actually, my code ended up very similar to feveran's code above. Could you please give me some directions using this hardware. I'm a Engineering Student and need to implement this in a project. If possible, could you send me an example for the readAllPins() and scanKeys() functions you mentioned? This will be very useful... I will appreciate any help. Thank you!  ;)



Joe Lima

#8
Jul 25, 2013, 08:52 pm Last Edit: Jul 26, 2013, 11:59 pm by Joe Lima Reason: 1
[SOLVED]  Here is an Arduino Library that I made to use with this Keypad: http://sourceforge.net/projects/w5iokeypadlibrary/, But, first I will describe the solution, as in a "magic trick", when you discover the "secret" is really easy to understand what to do. Here we go:

The first secret here is to think in the GND pin as another pin in the same way as IO1, IO2, IO3, IO4, IO5. DO NOT be tempted to put the GND pin in the real GND because you will loose control of it (this was my first mistake on my early tries). This said, you will  need to connect them, each one, to a digital pin in Arduino, separately. Now the "magic" begins:

Besides creating a function to scan the buttons and another to recognize then, I did it in just one function that returns an integer indicating which button has been pressed. The following algorithm describes how to read the buttons in the correct way (and the function modus operandi):

1) Early in the code, initialize each pin as INPUT_PULLUP, this is important to IO5Pin specially which will never change this state. Be careful;

2) Start reading the Joystick buttons: Set the GND pin as an OUTPUT and send a LOW to it. This way the GNDPin will act as a pure GND. Then, set all other pins, each one, as INPUT_PULLUP (IO1 to O4, IO5 isn't needed since it will remain as an INPUT_PULLUP forever). When this is set all pins should report HIGH status except the one which the button has been pressed, so, start scanning the the pins from IO1 to IO5 (IN ORDER). The first one which has a LOW condition is the one with the button being pressed. In this case exit the function with an exclusive integer number to identify this Joystick button press;

3) Start reading the push buttons from the K1-K4 group: Set the GND pin as an INPUT_PULLUP. Is not needed to send a HIGH to it because it is done automatically. This is important because we do not want another pin acting as a GND now, we need to control WHICH pin is acting as GND and WHEN (this is the second secret!). Now, set the IO1Pin as an OUTPUT and send a LOW to it (now the IO1Pin is the GND, got it?). Then, set all remaining pins, each one,  as INPUT_PULLUP  (IO2 to IO4, IO5 isn't needed remember?). Again, when this is set all pins should report HIGH status (including the Joystick ones, but since we are not interested on then now, they will be ignored because GNDPin is NOT acting like a GND anymore, see?).  Start scanning the the pins from IO2 to IO5 (IN ORDER). The first one which has the LOW condition is the one with the button being pressed. In this case exit the function with an exclusive integer number to identify this push button press;

4) Start reading the push buttons from the K5-K7 group: Set the IO1Pin pin as an INPUT_PULLUP (It cannot be a GND anymore, right?) Set the IO2Pin as an OUTPUT and send a LOW to it (IO2Pin is the new GND). Then, set all remaining pins, each one, as INPUT_PULLUP  (IO3 and IO4, forget IO5). Again, when this is set all pins should report HIGH status (including the Joystick ones and the K1-K4 group ones). Start scanning the the pins from IO3 to IO5 (IN ORDER). The first one which has the LOW condition is the one with the button being pressed. In this case exit the function with an exclusive integer number to identify this push button press;

5) Start reading the push buttons from the K8-K9 group: Set the IO2Pin pin as an INPUT_PULLUP. Set the IO3Pin as an OUTPUT and send a LOW to it (IO3Pin is the new GND). Then, the remaining pin as INPUT_PULLUP  (only IO4!). Again, when this is set all pins should report HIGH status (including the Joystick ones,  the K1-K4 group ones and the K5-K7 group ones).  Start scanning the the pins from IO4 to IO5 (IN ORDER). The first one which has the LOW condition is the one with the button being pressed. In this case exit the function with an exclusive integer number to identify this push button press;

6) Start reading the push buttons from the lonely K10 group: Set the IO3Pin pin as an INPUT_PULLUP. Set the IO4Pin as an OUTPUT and send a LOW to it (IO4Pin is the new GND). There in no remaining pins to set as INPUT_PULLUP. Again, when this is set all pins should report HIGH status (including the Joystick ones,  the K1-K4 group ones,  the K5-K7 group ones and the K8-K9 group ones).  Scan the pin IO5, if it has the LOW condition then the K10  button has been pressed. In this case exit the function with an exclusive integer number to identify this push button press;

7) Set the IO4Pin as an INPUT_PULLUP (just in case) and then return an unique code that means that no key has been pressed.

THAT'S IT! By the way, this keypad should be called "6IO Keypad". Thank you Mark Stanley for the suggestions!

Here is the code:

// 5IO Keypad Driver
// Djohnson "Joe" Lima

#include <LiquidCrystal.h>

/*******************************************************

This program will test 5IOKeypad keys.
Note: This test program uses an LCD Shield, if you do not
     have it, just comment the appropriate lines.
     Serial output has been provided for convenience.

********************************************************/

// Initialize 5IOKeys Pins
int GNDPin = 0;
int IO1Pin = 0;
int IO2Pin = 0;
int IO3Pin = 0;
int IO4Pin = 0;
int IO5Pin = 0;

// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

void Init_5IOKeys(int Pin0, int Pin1, int Pin2, int Pin3, int Pin4, int Pin5) {

 // Assign Arduino pins to 5IOKey Pins
 GNDPin = Pin0;
 IO1Pin = Pin1;
 IO2Pin = Pin2;
 IO3Pin = Pin3;
 IO4Pin = Pin4;
 IO5Pin = Pin5;

 // Initialize the pushbuttons pins as input:
 pinMode(GNDPin, OUTPUT);  digitalWrite(GNDPin, LOW);
 pinMode(IO1Pin, INPUT_PULLUP);
 pinMode(IO2Pin, INPUT_PULLUP);    
 pinMode(IO3Pin, INPUT_PULLUP);    
 pinMode(IO4Pin, INPUT_PULLUP);    
 pinMode(IO5Pin, INPUT_PULLUP);    

};

int ReadButtons_5IOKeys() {


 // Start reading the Joystick keys
 pinMode(GNDPin, OUTPUT); digitalWrite(GNDPin, LOW);                                   // Set this pin to act as the GND
 pinMode(IO1Pin, INPUT_PULLUP);                                                        //  
 pinMode(IO2Pin, INPUT_PULLUP);                                                        //
 pinMode(IO3Pin, INPUT_PULLUP);                                                        //  Set these pins to 'listen' the pushing
 pinMode(IO4Pin, INPUT_PULLUP);                                                        //
 // Return the appropriate code
 if (digitalRead(IO1Pin)==LOW) { while (digitalRead(IO1Pin)==LOW); return 11; } else  
 if (digitalRead(IO2Pin)==LOW) { while (digitalRead(IO2Pin)==LOW); return 12; } else  
 if (digitalRead(IO3Pin)==LOW) { while (digitalRead(IO3Pin)==LOW); return 13; } else  
 if (digitalRead(IO4Pin)==LOW) { while (digitalRead(IO4Pin)==LOW); return 14; } else  
 if (digitalRead(IO5Pin)==LOW) { while (digitalRead(IO5Pin)==LOW); return 15; };      

 // Start reading the K1-K4 push keys
 pinMode(GNDPin, INPUT_PULLUP);
 pinMode(IO1Pin, OUTPUT); digitalWrite(IO1Pin, LOW);                                   // Now, IO1Pin is the new GND
 pinMode(IO2Pin, INPUT_PULLUP);    
 pinMode(IO3Pin, INPUT_PULLUP);    
 pinMode(IO4Pin, INPUT_PULLUP);    
 if (digitalRead(IO2Pin)==LOW) { while (digitalRead(IO2Pin)==LOW); return 1; };
 if (digitalRead(IO3Pin)==LOW) { while (digitalRead(IO3Pin)==LOW); return 2; };
 if (digitalRead(IO4Pin)==LOW) { while (digitalRead(IO4Pin)==LOW); return 3; };
 if (digitalRead(IO5Pin)==LOW) { while (digitalRead(IO5Pin)==LOW); return 4; };

 // Start reading the K5-K7 push keys
 pinMode(IO1Pin, INPUT_PULLUP);    
 pinMode(IO2Pin, OUTPUT); digitalWrite(IO2Pin, LOW);                                  // Now, IO2Pin is the new GND
 pinMode(IO3Pin, INPUT_PULLUP);    
 pinMode(IO4Pin, INPUT_PULLUP);    
 if (digitalRead(IO3Pin)==LOW) { while (digitalRead(IO3Pin)==LOW); return 5; };
 if (digitalRead(IO4Pin)==LOW) { while (digitalRead(IO4Pin)==LOW); return 6; };
 if (digitalRead(IO5Pin)==LOW) { while (digitalRead(IO5Pin)==LOW); return 7; };

 // Start reading the K8-K9 push keys
 pinMode(IO2Pin, INPUT_PULLUP);    
 pinMode(IO3Pin, OUTPUT); digitalWrite(IO3Pin, LOW);                                  // Now, IO3Pin is the new GND
 pinMode(IO4Pin, INPUT_PULLUP);    
 if (digitalRead(IO4Pin)==LOW) { while (digitalRead(IO4Pin)==LOW); return 8; };
 if (digitalRead(IO5Pin)==LOW) { while (digitalRead(IO5Pin)==LOW); return 9; };
 
 // Start reading the 1K0 push key
 pinMode(IO3Pin, INPUT_PULLUP);    
 pinMode(IO4Pin, OUTPUT); digitalWrite(IO4Pin, LOW);                                 // Now, IO4Pin is the new GND
 if (digitalRead(IO5Pin)==LOW) { while (digitalRead(IO5Pin)==LOW); return 10; };

 pinMode(IO4Pin, INPUT_PULLUP);    

 return 0; // No key pressed
 
};

void setup()
{

 Init_5IOKeys( 49, 47, 45, 43, 41, 39 ); // These are the digital ports wich the IO pins on the Keypad are linked

 Serial.begin(9600);
 
 lcd.begin(16, 2);                   // start the library
 lcd.clear();

}

void loop()
{

 int IOKey = 0;

 delay(100);
 
 // Look if some button have bem pushed
 do {IOKey=ReadButtons_5IOKeys();} while (IOKey==0);

 lcd.setCursor(0 ,1);lcd.print("    ");
 lcd.setCursor(0 ,1);lcd.print(IOKey);
 Serial.println(IOKey);

}

Go Up