Arduino Keypad 4x4 - Need basic script

I am new to the Arduino, computer programming, and electrical engineering. I wanted to plug a 4 column x 4 row keypad into my Arduino digital pins 0 to 7 and get output into the Arduino software serial monitor. I have tried a couple of the scripts from the Arduino playground (arduino.cc/playground/Code/Keypad) but the serial monitor starts going bizerk and typing random numbers and letters. I don't care really what each key outputs, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 would be ideal, left to right. I have a standard 4x4 8 pin keyboard.

Pin 1 + Pin 8 = 1 Pin 2 + Pin 8 = 2 Pin 3 + Pin 8 = 3 Pin 4 + Pin 8 = 4 Pin 1 + Pin 7 = 5 Pin 2 + Pin 7 = 6 Pin 3 + Pin 7 = 7 Pin 4 + Pin 7 = 8 Pin 1 + Pin 6 = 9 Pin 2 + Pin 6 = 10 Pin 3 + Pin 6 = 11 Pin 4 + Pin 6 = 12 Pin 1 + Pin 5 = 13 Pin 2 + Pin 5 = 14 Pin 3 + Pin 5 = 15 Pin 4 + Pin 5 = 16

Example, when you push down key 1 (top left of keypad), it connects Pin 1 and Pin 8 which should let Arduino software type "1" in the serial monitor.

Hope someone can post the code I can upload to my Arduino.

First things first. To be able to use the serial monitor in the Arduion IDE then the serial data utilizes pins 0 and 1 on the Arduino, so you will have to choose pins other then those two.

Lefty

Ok I guess i'll do pins 6-13

Still don't know the programming though :(

The best plan is to make an attempt to get it working then post a description of what works and what doesn’t with the source code. I suspect someone will be willing to lend a helping hand.

  • Brian

Well I used this code found here http://www.arduino.cc/playground/Code/Keypad#Example

/*
||
|| @file HelloKeypad.pde
|| @version 1.0
|| @author Alexander Brevig
|| @contact alexanderbrevig@gmail.com
||
|| @description
|| | Display the intuitive way of using a keypad when using this HAL
|| #
||
|| @license
|| | Copyright (c) 2009 Alexander Brevig. All rights reserved.
|| | This code is subject to AlphaLicence.txt
|| | alphabeta.alexanderbrevig.com/AlphaLicense.txt
|| #
||
*/
 
#include <Keypad.h> //http://alexanderbrevig.com/hosting//arduino/libraries/NewKeypad.zip
 
byte rows = 4; //four rows
byte cols = 4; //four columns
byte rowPins[] = {3, 2, 1, 0}; //connect to the row pinouts of the keypad
byte colPins[] = {7, 6, 5, 4}; //connect to the column pinouts of the keypad
 
Keypad keypad = Keypad(rowPins,colPins,rows,cols);
 
void setup(){
  Serial.begin(9600);
}
 
void loop(){
  char key = keypad.getKey();
 
  if (key != NO_KEY){
    Serial.println(key);
  }
}

After it successfully uploaded to my Arduino, i typed a few buttons and while on serial monitor and it started showing gibberish like
865
eyd
das
fasdf
4tfd
e34
45
a4
etc

I did include keypad.h properly but I think it would be easier if I just had one single code to upload to my unit without all the includes. I figured this would be simple for anyone with programming knowledge. I am still learning. This code is C++ right?

Oh.

Sorry, I do not think that code is functional at the moment. It is actually kind of not released yet.

Stupid of me making the Playground page, but I was on a wave of productivity, and just had to write it.

I'll try to fix it as soon as possible.

Sorry again.

Here is the link to the sketch = http://www.arduino.cc/playground/uploads/Main/matrixKeypad.zip Maybe I did not compile everything together properly as it was my first day using the program but I do think it will be a lot easier for me to learn from if it was all one code without the includes and compiling etc.

Oh ok, that could be the problem to :) Is it hard to compile a simple keypad script to work with the Arduino based on my initial post? I also tried the original one http://www.arduino.cc/playground/Main/KeypadTutorial

I'm working on the code right now. Tha Library that is.

You could write a series of if..else statemens to determine the 1...16 for your requirements.

If you have time to wait a bit, I'll try to make something work :)

[edit]I will not be able to post until tomorrow, will get back to this thread ASAP. Sorry again. :)[/edit]

Thanks so much, I am actually going to head out to the library now to read a beginners book on C++ so I have lots of time, no rush at all. That's the main programming language of the Arduino right?

I am actually going to head out to the library now to read a beginners book on C++ so I have lots of time, no rush at all. That’s the main programming language of the Arduino right?

You are correct :slight_smile:

This should print 1->16 to the Serial console:

//http://www.arduino.cc/playground/Code/Keypad
#include <Keypad.h> // Download Keypad.zip

//define the cymbols on the buttons of the keypads
char hexaKeys[4][5] = {
“ABCD”,
“EFGH”,
“IJKL”,
“MNOP”
};
byte rows = 4; //four rows
byte cols = 4; //four columns
byte rowPins = {2,3,4,5}; //connect to the row pinouts of the keypad
byte colPins = {6,7,8,9}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad(hexaKeys,rowPins,colPins,rows,cols);

void setup(){
Serial.begin(9600);
}

void loop(){
char key = keypad.getKey();

if (key != NO_KEY) {
key -= ‘A’-1;
Serial.println(key,DEC);
}
}

I just tried uploading that to my board and it doesn't seem to work. At first it will display "16" in serial output without anything plugged into the android. Then I plugged a short wire into digital pin 2 and 1 into digital pin 8 and tapped them together to represent a keypad stroke and nothing showed up. As I was connecting the 2 wires to my breadboard connected to my keypad I got a random serial output of:

16

8

16

8

16

8

16

8

16

8

16

8

16

8

16

8

16

8

16

12

16

12

16

12

16

12

16

12

16

12

16

12

16

12

16

12

16

12

16

12

16

12

16

12

16

12

16

and then it just stopped, even when I pressed any button on the keypad. Not sure if I am doing something wrong. Also tried connecting wires between other pins but nothing shows up.

You’ll need to change:

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

To suit you needs.

Have you done that?

[edit]You’ll also need the newest library. Keypad.zip[/edit]

Would it be simple to say something like:

If pin2 touches pin8, serialprint "28" If pin3 touches pin9, serialprint "39"

//just random print outputs I can change around

If you can provide me with these 2 commands properly written in code and the code before and after I can fill in the rest of the keys and configure it for my pad.

Yes I just left it as it is for testing purposes and connected pin 2 to pin 6 etc to see if it would trigger any number but nothing. I know i have keypad.h, i'll make sure its the newer one.

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

Using pin 0 and 1 is not a good idea if you're going to use the Serial.

[edit]Keypad library is updated today :) [/edit]

Ok I downloaded the new keypad.zip and put the contents in my arduino folder/hardware/libraries, uploaded it, checked serial monitor, saw a 16 appear by itself and then as i touched wires connected to pin 2 and 8, not connected, just touched with my hand, the serial monitor started displaying random numbers from 1 to 16. I restarted the program and the arduino and checked the serial monitor again and now it just displays the 16 at the start and nothing when I connect the pins 2 and 8 or any other column with row pin.

Should I be grounding anything? Am using pins 2 and up, learned not to use the 0 and 1 :)

Nothing should be grounded.

This code prints 3 when pin 2 and 8 are connected:

//http://www.arduino.cc/playground/Code/Keypad
#include <Keypad.h>

//define the cymbols on the buttons of the keypads
char hexaKeys[4][5] = {
“ABCD”,
“EFGH”,
“IJKL”,
“MNOP”
};
byte rows = 4; //four rows
byte cols = 4; //four columns
byte rowPins = {2,3,4,5}; //connect to the row pinouts of the keypad
byte colPins = {6,7,8,9}; //connect to the column pinouts of the keypad

Keypad keypad = Keypad(hexaKeys,rowPins,colPins,rows,cols);

void setup(){
Serial.begin(9600);
}

void loop(){
char key = keypad.getKey();

if (key != NO_KEY) {
key -= ‘A’-1;
Serial.println(key,DEC);
}
}

Delete the hardware/libraries/Keypad folder, download the new library, and extract to that location.

It works here, so it is strange that it does not work there.

It doesn't seem to be working for me. Have you tried it on your arduino? Just get the 16 at the beginning of the serial monitor then nothing as i tap a wire connected to pin2 and pin 8.