Quantcast
Viewing all articles
Browse latest Browse all 61

Tutorial: Arduino and Numeric Keypads

Use numeric keypads with Arduino in chapter forty-two of a series originally titled “Getting Started/Moving Forward with Arduino!” by John Boxall – a series of articles on the Arduino universe. The first chapter is here, the complete series is detailed here. Any files from tutorials will be found here.

This is part one of two chapters that will examine another useful form of input – the numeric keypad; and some applications that hopefully may be of use.  Here is the example we will be working with:

Image may be NSFW.
Clik here to view.

It seems quite similar to the keypad from a 1980s-era Dick Smith Electronics cordless phone. Turning the keypad over we find seven pins:

Image may be NSFW.
Clik here to view.

Personally I like this type of connection, as it makes prototyping very easy using a breadboard – you just push it in. Looking at the back the pins are numbered seven to one (left to right). My example was from Futurlec of all places. You can also find types that have solder pads. At this point you need to download the data sheet.pdf, as it shows the pinouts for the rows and columns. At first glance trying to establish a way of reading the keypad with the Arduino does seem troublesome – however the basic process is to ‘scan’ each row and then test if a button has been pressed.

If your keypad has more than seven pins or contacts – and the data sheet was not supplied, you will need to manually determine which contacts are for the rows and columns. This can be done using the continuity function of a multimeter (the buzzer). Start by placing one probe on pin 1, the other probe on pin 2, and press the keys one by one. Make a note of when a button completes the circuit, then move onto the next pin. Soon you will know which is which. For example, on the example keypad pins 1 and 5 are for button “1″, 2 and 5 for “4″, etc…

In the interest of keeping things simple and relatively painless we will use the numeric keypad Arduino library. Download the library from here, copy the “Keypad” folder into your ../arduino-002x/libraries folder, then restart the Arduino IDE.

Now for our first example. From a hardware perspective you will need

  • An Arduino Uno or 100% compatible board
  • A numeric keypad
  • An LCD of some sort. We will be using an I2C-interface model. If you are unsure about LCD usage, please see this tutorial
  • If you don’t have an LCD – that’s ok. After installing the keypad library, select File>Examples>Keypad>Examples>HelloKeypad in the IDE.
Connect the keypad to the Arduino in the following manner:
  • Keypad row 1 to Arduino digital 5
  • Keypad row 2 to Arduino digital 4
  • Keypad row 3 to Arduino digital 3
  • Keypad row 4 to Arduino digital 2
  • Keypad column 1 to Arduino digital 8
  • Keypad column 2 to Arduino digital 7
  • Keypad column 3 to Arduino digital 6
Now for the sketch:

For the non-believers, here it is in action:


As you can see the library really does all the work for us. In the section below the comment “keypad type definition” we have defined how many rows and columns make up the keypad. Furthermore which digital pins connect to the keypad’s row and column pins. If you have a different keypad such as a 16-button version these will need to be modified. Furthermore you can also map out what the buttons will represent in the array “keys”. Then all of these variables are passed to the library in the function Keypad keypad = Keypad() etc.

Reading the buttons pressed is accomplished in void loop()… it reads the keypad by placing the current value into the char variable “key”. The if… statement tests if a button has been pressed. You can reproduce this loop within your own sketch to read values and then move forward to other functions. Let’s do that now in our next example.

Keypad Switch

Using our existing example hardware we can turn something on or off by using the keypad – replicating what can be found in some alarm systems and so on. Our goal with this example is simple – the systems waits for a PIN to be entered. If the PIN is correct, do something. If the PIN is incorrect, do something else. What the actions are can be up to you, but for the example we will turn on or off a digital output. This example is to give you a concept and framework to build you own ideas with.

The hardware is the same as the previous example but without the LCD. Instead, we have a 560 ohm resistor followed by an LED to GND from digital pin ten. Now for the sketch:

And the ubiquitous demonstration video:

This sketch is somewhat more complex. It starts with the usual keypad setting up and so on. We have two arrays, attempt and PIN. PIN holds the number which will successfully activate the switch, and attempt is used to store the key presses entered by the user. Users must press ‘*’ then the PIN then ‘#’ to activate the switch.

The comparison to check for accuracy is in the function checkPIN(). It compares the contents of PIN against attempt. If they match, the function correctPIN() is called. If the entered PIN is incorrect, the function incorrectPIN() is called. We also call the function incorrectPIN() in void setup to keep things locked down in case of a power failure or a system reset.

You can now see that such a complex device can be harnessed very easily, and could have a variety of uses. In part two, we will look at the 16-digit 

Image may be NSFW.
Clik here to view.

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

The post Tutorial: Arduino and Numeric Keypads appeared first on tronixstuff.


Viewing all articles
Browse latest Browse all 61

Trending Articles