LCD 1602 Keypad Shield for arduino The LCD Keypad shield is developed for Arduino compatible boards, to provide a user-friendly interface that allows users to go through the menu, make selections etc. It consists of a 1602 white character blue backlight LCD. The keypad consists of 5 keys — select, up, right, down and left. To save the digital IO pins, the keypad interface uses only one ADC channel. The key value is read through a 5 stage voltage divider. Note: Version 1.1 main updates are the button values, which have being updated on the example code. For older version check the comments and edit, or use the Enhanced V1.0 library . Pin Function Analog 0 Button (select, up, right, down and left) Digital 4 DB4 Digital 5 DB5 Digital 6 DB6 Digital 7 DB7 Digital 8 RS (Data or Signal Display Selection) Digital 9 Enable Digital 10 Backlit Control
Sample Code
#include <LiquidCrystal.h>
// Select the pin used on LCD
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define the button
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
//read the button value
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read analog A0 value
// when read the 5 key values in the vicinity of the following:0,144,329,504,741
// By setting different threshold, you can read the one button
if (adc_key_in > 1000) return btnNONE;
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 250) return btnUP;
if (adc_key_in < 450) return btnDOWN;
if (adc_key_in < 650) return btnLEFT;
if (adc_key_in < 850) return btnSELECT;
// V1.0 Use the following version threshold:
/*
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
*/
return btnNONE;
}
void setup()
{
lcd.begin(16, 2); // star
lcd.setCursor(0,0);
lcd.print("Push the buttons"); // display“Push the buttons”
}
void loop()
{
lcd.setCursor(9,1); // The cursor is set at second. and have 9 spaces
lcd.print(millis()/1000); // Output waiting time
lcd.setCursor(0,1); // The cursor moves to the beginning of the second line.
lcd_key = read_LCD_buttons(); // read key
switch (lcd_key) // display key
{
case btnRIGHT:
{
lcd.print("RIGHT ");
break;
}
case btnLEFT:
{
lcd.print("LEFT ");
break;
}
case btnUP:
{
lcd.print("UP ");
break;
}
case btnDOWN:
{
lcd.print("DOWN ");
break;
}
case btnSELECT:
{
lcd.print("SELECT");
break;
}
case btnNONE:
{
lcd.print("NONE ");
break;
}
}
}