Posts Tagged ‘keypad’
Keypad & LCD Display
While I was still trying to figure out what to do these 10 keypads, I received an LCD Display I ordered on eBay. It’s friday, I have no better idea than plug them both on an Arduino and code something.
I started from the circuit of the Keypad article and moved the wires connected to pins 7 & 8 to analog pins 0 & 1. There is no good reason to that shift except that it makes it easier to have the LCD wires all connected to the same side of the Arduino.
I will not go in details with the wiring since Limor Fried, founder of Adafruit Industries published an excellent demo on how to connect the display to an Arduino. This will require an additional 6 pins on your Arduino. I used the same pins as in the Lady Ada demo, 7, 8, 9, 10, 11 and 12.
Now let’s jump into the code. This is a very simple demo and you will not find anything mind blowing. The hard part has been coded for you in the LiquidCrystal library, included with the Arduino IDE.
Thanks for reading …
#include <Keypad.h>
#include <LiquidCrystal.h>
#include <Wire.h>
#define REDLITE 3
#define GREENLITE 5
#define BLUELITE 6
#define TITLE "KeyPad & LCD __"
#define READY "Ready ... "
#define EMPTY " "
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
// you can change the overall brightness by range 0 -> 255
int brightness = 255;
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','#'}
};
//connect to the row pinouts of the keypad
byte rowPins[ROWS] = {2, 3, 4, 5};
//connect to the column pinouts of the keypad
byte colPins[COLS] = {6, 14, 15};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup()
{
// set up the LCD's number of rows and columns:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(TITLE);
lcd.setCursor(0,1);
lcd.print(READY);
brightness = 100;
Serial.begin(9600);
}
int index = 1;
char digits[2] = {' ',' '};
void loop(){
char key = keypad.getKey();
if (key != NO_KEY)
{
index = !index;
if(key == '*')
{
index = 1;
digits[0] = ' ';
digits[1] = ' ';
lcd.setCursor(0,0);
lcd.print(TITLE);
lcd.setCursor(0,1);
lcd.print(READY);
}
else if(key == '#')
{
// Animation
for(int i=0; i<15;i++)
{
lcd.setCursor(0,1);
lcd.print(EMPTY);
lcd.setCursor(i,1);
lcd.print(digits);
delay(100);
}
lcd.setCursor(0,1);
lcd.print(EMPTY);
lcd.setCursor(14,0);
lcd.print(digits);
index = 1;
digits[0] = ' ';
digits[1] = ' ';
lcd.setCursor(0,1);
lcd.print(READY);
}
else
{
digits[index] = key;
lcd.setCursor(0,1);
lcd.print(digits);
lcd.print(EMPTY);
}
Serial.println(key);
}
}
Cheap Keypad and Arduino
I just received a package of 10 flexible keypads I bought on eBay for 15.88$, shipping included. Having a keypad in your project opens so many possibilities, at 1.59$ each I think it’s a deal!
I found a keypad library for Arduino on the Arduino Playground. I made some minor modifications to the sample code they provide on that page. I changed the order of the rowPins and colPins arrays to match the order of the pins on my keypad. I also had to switch the # and the * to match my keypad.
There is however a bad side to this type of keypad … the number of pins required. This is a matrix type keypad that requires 7 IOs to run. For many projects, finding 7 IOs is simply not possible. There are two solutions to this problem. The most simple and most expensive one is to switch to the Arduino Mega. The cheap but more complex solution would be to use an IO port extender like the MCP23008.
#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] = {2, 3, 4, 5}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {6, 7, 8}; //connect to the column pinouts of the keypad
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
void setup(){
Serial.begin(9600);
}
void loop(){
char key = keypad.getKey();
if (key != NO_KEY){
Serial.println(key);
}
}
I think I will revisit my KidsClock project so I will not have to re-upload the program to change the wake up time or to switch to day light saving time. What would you do with a keypad and an Arduino? Leave your comment in the comment section of this post …


