IAN LANG ELECTRONICS

Over on the right hand side there you can see a standard number pad made by Sparkfun. Can you guess where I purchased this from? Go on, have a guess.

Wrong. It actually came from a company I've recently discovered called Proto-Pic, based in bonny Scotland. The package arrived within three days of ordering and very well wrapped, and correct. I'm going to make them my supplier of second resort, because they impressed me with their website and extensive range.

It's a matrix type and as you can see there are nine holes at the bottom end. The end holes on each side are not connected to anything, or if they are I can't see what it is, and sure enough they don't need to be used. The other seven are three for columns, and four for rows. If you imagine the connections underneath, they go something like the below:

Pins from left to right go 0 1 2 3 4 5 6 7 8

3                  1                5

2

7

6

4

So, if we pressed button 1, it would set up a conductive path between pin 3 and pin 2. Similarly if we pressed button 4, it would set up a conductive path between pin 3 and pin seven. If we pressed button 7, we'd get a path between pins three and six, and pressing the star button gives us a path between pin 3 and pin 4. When no button is pressed, there's no conductive path anywhere. If we keep the columns high at 5V by applying it to pins 3, 1 & 5, then pressing a button in a row will send an output of high to whichever pin that row connects to ( 2,7,6 or 4).

There lies the problem. Consider what happens in the first row (button 1, 2 & 3). All the buttons are attached to a different column but each is high and has a feed of five volts. It doesn't matter which button in the top row you press, pin two sees five volts hitting it. We need a way to discriminate which button is being pressed.

In days of yore we'd have to set up a transistor switching system to do this. It would have taken ages and cost bucketfuls of money. People, we now have an Arduino that we can code to do the discriminating for us. The way it works is that we feed the columns ( 3, 1 & 5) with an output from three digital pins. Then we use another four to read which row is activated when a button is pressed. If it's the top row (i.e pin 2 of the keypad has 5V potential and consequently so does whichever pin of the Arduino to which pin 2 of the keypad is attached) then it must either be button 1, 2 or 3 that's been pressed. To detect it, we switch off and on again the columns one by one, by digitalWriting the pins of the Arduino that they're attached to. In the switch off, the potential at pin 2 of the keyboard will fall from 5V to 0V only when the column supplying that potential is switched off. In the other two cases it will stay high. Therefore we have a ready-made digital signalling technique to hand and when we detect the absence of the signal, that's the button we want to acknowledge as the one pressed.

If you are not thouroughly confused by the above well done. If you are, here's how it works in practice. Set your Arduino up like this:

In point of fact the value of those resistors does not matter all that much. What they're doing is reliably pulling pins 8, 9, 10 & 11 of your Arduino to ground. If they weren't there you'd get some false readings especially if there was a lot of EMI (electro-magnetic interference, that is, not a record label) around where you've got your Arduino. Something like a computer would do it.

These two drawings, by the way, were developed in Fritzing, which is an impressive piece of software written by the Technical Institute of Potsdam.

Potsdam- it's just down from Berlin and used to be in East Germany. I went there last time just after the Wall fell and I can tell you they weren't this technically advanced then. They were still struggling with the concept of cars that didn't break down every four or five kilometres and couldn't be immobilised by a passenger on the back seat accidentally disconnecting a wire by moving his foot. How times have changed.

Anyway, Fritzing is well worth looking at. I'm not sure I like the schematics it produces but the wiring diagrams as above are wonderful.

Fritzing

So then, having mused on recent history and set up our Arduino it's time to slap it with some code. Here's a big bag of it:

//Ian Lang Electronics///

//For detecting which key on the 12 button 4 row 3 column is pressed

char* inputbutton=0; // this will be the output variable

void setup(){

for(int t=2;t<5;t++){

pinMode(t,OUTPUT);

digitalWrite(t,1);}// the feed pins 2, 3 & 4 provide 5V to the columns

Serial.begin(9600);//start the serial feed

}

void loop (){

//**************TOP ROW**************

digitalWrite(2,0); //col 1 off

if (digitalRead(8)==0){ // has 8 gone low?

inputbutton= "one";} // if above is true, it's button one

digitalWrite(2,1); //col 1 back on

digitalWrite(3,0);//col 2

inputbutton= "two";} // if above is true, its button 2

digitalWrite(3,1);//col 2

digitalWrite(4,0);// col 3

inputbutton= "three";}// if above is true it's button 3

digitalWrite(4,1);}// col 3

//**************SECOND ROW**************

digitalWrite(2,0); //col 1

inputbutton= "four";}

digitalWrite(2,1); //col 1

digitalWrite(3,0);//col 2

inputbutton= "five";}

digitalWrite(3,1);//col 2

digitalWrite(4,0);// col 3

inputbutton= "six";}

digitalWrite(4,1);}// col 3

//**************THIRD ROW**************

digitalWrite(2,0); //col 1

inputbutton= "seven";}

digitalWrite(2,1); //col 1

digitalWrite(3,0);//col 2

inputbutton= "eight";}

digitalWrite(3,1);//col 2

digitalWrite(4,0);// col 3

inputbutton= "nine";}

digitalWrite(4,1);}// col 3

//**************BOTTOM ROW**************

digitalWrite(2,0); //col 1

inputbutton= "*";}

digitalWrite(2,1); //col 1

digitalWrite(3,0);//col 2

inputbutton= "zero";}

digitalWrite(3,1);//col 2

digitalWrite(4,0);// col 3

inputbutton= "#";}

digitalWrite(4,1);}// col 3

Serial.println(inputbutton);

//Reset the variable inputbutton

inputbutton=" ";

}

Once the code has uploaded, open the serial monitor. Precisely nothing will happen. If you're observant, you might notice the thumb of the serial monitor sliding downwards due to the fact the Arduino is sending a null character, but no text or numbers appear. Until you press a button on the keypad that is. Go on, press number 1. You want to and you know you do. The word  "one" should appear several times going down the monitor until you take your finger off the button. Now press button four. The word "four" should appear. Go through them in order and make sure what you are pressing corresponds to what the monitor says.If it does, Huzzah! If not, check your wiring.

Over the page we go for a bit of analysis of this code.

More>

In the diagram on the right, the columns are fed from the blue wires, the output from the rows goes down the orange ones. The input/output holes at the bottom of the keypad come unpopulated and so you'll need to do a bit of soldering. To get it to fit a breadboard, I used some headers from Oomlout, and there's another advertisement for them. I promise I have no shares in the company. Despite what it looks like.