# Simple keypad tutorial

**URL:** <https://community.particle.io/t/simple-keypad-tutorial/8758>\
**Category:** Hardware\
**Created:** [December 12, 2014, 9:13am UTC](https://community.particle.io/t/simple-keypad-tutorial/8758 "2014-12-12T09:13:46Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![emjx](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@emjx](https://community.particle.io/u/emjx)\
**Post date:** [December 12, 2014, 9:13am UTC](https://community.particle.io/t/simple-keypad-tutorial/8758/1 "2014-12-12T09:13:46Z")

</div>

I am in need of some guidance with connecting a 12 button keypad to the Spark Core. To get started, I have been looking at a membrane matrix keypad from AdaFruit which has some example Arduino code ([http://proto-pic.co.uk/membrane-matrix-keypad-3x4/](http://proto-pic.co.uk/membrane-matrix-keypad-3x4/)) - shown below:

```
#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] = {8, 7, 6, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {4, 3, 2}; //connect to the column pinouts of the keypad

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

```

This all looks well and good and from what I understand, I would change the rowPins and colPins values to be the actual [Spark.IO](http://Spark.IO) pins D0, D1, D3, D4 and D5, D6, D7.

With regards to the code shown above, this would obviously need to be pasted somewhere and from the Arduino website, it would suggest that this goes above “void setup()” line ([http://playground.arduino.cc/Code/Keypad](http://playground.arduino.cc/Code/Keypad)).

keypad.h would get created as a new file via the [Spark.IO](http://Spark.IO) Web interface (copying and pasting the code from the actual keypad.h file from the link in the last paragraph). Are any other files required? For example, the ZIP download from the Arduino website includes a utility folder, a file called Keypad.cpp and keywords.txt.

In the setup method, the following goes:

```
Serial.begin(9600);

```

and in loop() goes:

```
char key = keypad.getKey();

if (key != NO_KEY){
 Serial.println(key);
}

```

This all seems too easy and before running off to buy keypads and more cores, could anyone confirm whether this will work?

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 12, 2014, 4:58pm UTC](https://community.particle.io/t/simple-keypad-tutorial/8758/2 "2014-12-12T16:58:40Z")

</div>

I haven’t had a closer look, but the logic behind such a keypad is very simple, so I’m sure it’ll work.  
I’m also convinced that you’d need the `keypad.cpp’ since there will be all the code required to do the scanning and interpreting of the key strokes.  
But as said, it won’t be anything too hard to understand when you actually read the code.

`keyword.txt` is not required, it just helps the Arduino IDE to do code highlighting and some other editor stuff.

As for the `utility` folder, have a browse through the files therin, that will give you some clue if you’ll need it for your project or not.

---

<div class="post-metadata">

**Author:** ![emjx](https://avatars.discourse-cdn.com/v4/letter/e/aca169/32.png) [@emjx](https://community.particle.io/u/emjx)\
**Post date:** [April 13, 2015, 9:27am UTC](https://community.particle.io/t/simple-keypad-tutorial/8758/3 "2015-04-13T09:27:17Z")

</div>

A quick update for anyone who comes across this thread from Google - I finally got back to this little project but have abandoned it without being able to get it working. The code I originally posted above doesn’t work and unfortunately, as much as I want to get this working, I just don’t have the time. Sorry to anyone who finds this thread in Google hoping for a simple keypad tutorial - there isn’t one 😞

---

<div class="post-metadata">

**Author:** ![Ideapod](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/ideapod/32/1743_2.png) [@Ideapod](https://community.particle.io/u/Ideapod)\
**Post date:** [May 9, 2015, 2:41am UTC](https://community.particle.io/t/simple-keypad-tutorial/8758/4 "2015-05-09T02:41:14Z")

</div>

Referencing the original arduino tutorial, the setup method has a few more pieces of set up:

[http://playground.arduino.cc/Main/KeypadTutorial](http://playground.arduino.cc/Main/KeypadTutorial)

```
void setup()
{
  pinMode(ledpin,OUTPUT);
  digitalWrite(ledpin, HIGH);
  Serial.begin(9600);
}

```

and the loop says:

```
char key = kpd.getKey();
  if(key) // Check for a valid key.
  {
    switch (key)
    {
      case '*':
        digitalWrite(ledpin, LOW);
        break;
```
