Arduino LCD Keypad Shield

56K2327

Intro: Arduino LCD Keypad Shield

STEP 1: Library

You will need to install the Keypad library.

STEP 2: Code

Here is my modified code from the example code included with the library. I added the function to turn on the LED on digital pin 13 when the select button is pushed and to turn it back off then the select button is released.

STEP 3: NOTE:

Do not changed digital pin 10. This pin is used to control the back light of the LCD. If its set to output and high it may damage you LCD shield.

18 Comments

thats cool, but you just adjust your button values using serial viewer. then you see your button values and fixed in code

/*
Created by
Ark Bramwell, July 2010
Modified by keen
Date: 05/05/2017
Function: this program will test the LCD panel and the buttons
*/
//Sample using LiquidCrystal library
#include <LiquidCrystal.h>
#include <Wire.h>
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
// define some values used by the panel and buttons
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 buttons
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1500) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
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 < 550) return btnLEFT;
if (adc_key_in < 800) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
void setup()
{
Serial.begin(9600);
lcd.begin(16, 2); // start the library
lcd.setCursor(0, 0);
lcd.print("Push the buttons"); // print a simple message
}
void loop()
{
lcd.setCursor(9, 1); // move cursor to second line "1" and 9 spaces over
lcd.print(millis() / 1000); // display seconds elapsed since power-up
lcd.setCursor(0, 1); // move to the begining of the second line
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an action
{
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;
}
}
Serial.print("Deger :" );
Serial.println(adc_key_in);
}
Thank you for the info. I need your help, I want to ask about how to make the keypad function like scrolling up and down..? I'll try to make it like this in the void loop. Then, below these coding, there will be a condition to make it to scroll. I'll try it but nothing happen. It just appear the "right", "left", "up" etc.. :'(
void loop() {
lcd.setCursor(0,1);
lcd_key = read_LCD_buttons(); // read the buttons
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnUP:
{
lcd.clear();
lcd.print(" up ");
break;
}
case btnDOWN:
{
lcd.clear();
lcd.print(" down ");
break;
}
}
I ordered and recieved this unit today and checked out some vids please note ...There are 2 or more versions of shields of this type..before using verify the number of push buttons on your unit and program your unit per its instructions. My unit has 6 pushbuttons others have 5 Please note the difference.

I downloaded your sketch but when I tried to compile it I got the following error messages:

Arduino: 1.8.6 (Windows 10), Board: "Arduino/Genuino Uno"

FINA14KITKJE85T:6:1: error: 'DFR_LCD_Keypad' does not name a type

DFR_LCD_Keypad keypad(A0, &lcd);

^

C:\Users\taran\Downloads\FINA14KITKJE85T\FINA14KITKJE85T.ino:7:11: warning: uninitialized const 'KEY_RIGHT' [-fpermissive]

const int KEY_RIGHT;

^

FINA14KITKJE85T:8:19: error: expected initializer before ':' token

const int KEY_LEFT:

^

C:\Users\taran\Downloads\FINA14KITKJE85T\FINA14KITKJE85T.ino:10:11: warning: uninitialized const 'KEY_DOWN' [-fpermissive]

const int KEY_DOWN;

^

C:\Users\taran\Downloads\FINA14KITKJE85T\FINA14KITKJE85T.ino:11:11: warning: uninitialized const 'KEY_NONE' [-fpermissive]

const int KEY_NONE;

^

C:\Users\taran\Downloads\FINA14KITKJE85T\FINA14KITKJE85T.ino: In function 'void setup()':

FINA14KITKJE85T:22:11: error: 'led' was not declared in this scope

pinMode(led, OUTPUT);

^

C:\Users\taran\Downloads\FINA14KITKJE85T\FINA14KITKJE85T.ino: In function 'void loop()':

FINA14KITKJE85T:29:14: error: 'keypad' was not declared in this scope

last_key = keypad.get_last_key();

^

FINA14KITKJE85T:39:12: error: the value of 'KEY_RIGHT' is not usable in a constant expression

case KEY_RIGHT:

^

C:\Users\taran\Downloads\FINA14KITKJE85T\FINA14KITKJE85T.ino:7:11: note: 'KEY_RIGHT' was not initialized with a constant expression

const int KEY_RIGHT;

^

FINA14KITKJE85T:39:12: error: the value of 'KEY_RIGHT' is not usable in a constant expression

case KEY_RIGHT:

^

C:\Users\taran\Downloads\FINA14KITKJE85T\FINA14KITKJE85T.ino:7:11: note: 'KEY_RIGHT' was not initialized with a constant expression

const int KEY_RIGHT;

^

FINA14KITKJE85T:43:12: error: 'KEY_UP' was not declared in this scope

case KEY_UP:

^

FINA14KITKJE85T:47:12: error: the value of 'KEY_DOWN' is not usable in a constant expression

case KEY_DOWN:

^

C:\Users\taran\Downloads\FINA14KITKJE85T\FINA14KITKJE85T.ino:10:11: note: 'KEY_DOWN' was not initialized with a constant expression

const int KEY_DOWN;

^

FINA14KITKJE85T:47:12: error: the value of 'KEY_DOWN' is not usable in a constant expression

case KEY_DOWN:

^

C:\Users\taran\Downloads\FINA14KITKJE85T\FINA14KITKJE85T.ino:10:11: note: 'KEY_DOWN' was not initialized with a constant expression

const int KEY_DOWN;

^

FINA14KITKJE85T:51:12: error: 'KEY_LEFT' was not declared in this scope

case KEY_LEFT:

^

FINA14KITKJE85T:55:12: error: 'KEY_SELECT' was not declared in this scope

case KEY_SELECT:

^

FINA14KITKJE85T:61:12: error: the value of 'KEY_NONE' is not usable in a constant expression

case KEY_NONE:

^

C:\Users\taran\Downloads\FINA14KITKJE85T\FINA14KITKJE85T.ino:11:11: note: 'KEY_NONE' was not initialized with a constant expression

const int KEY_NONE;

^

FINA14KITKJE85T:61:12: error: the value of 'KEY_NONE' is not usable in a constant expression

case KEY_NONE:

^

C:\Users\taran\Downloads\FINA14KITKJE85T\FINA14KITKJE85T.ino:11:11: note: 'KEY_NONE' was not initialized with a constant expression

const int KEY_NONE;

^

exit status 1

'DFR_LCD_Keypad' does not name a type

Invalid library found in C:\Users\taran\Documents\Arduino\libraries\keypad_values: C:\Users\taran\Documents\Arduino\libraries\keypad_values

Invalid library found in C:\Users\taran\Documents\Arduino\libraries\Yahzee: C:\Users\taran\Documents\Arduino\libraries\Yahzee

This report would have more information with

"Show verbose output during compilation"

option enabled in File -> Preferences.

Thanks, your library and example sketch worked perfectly for me!

the range of resistance for the up button was set too low. it is closer to 150 ohms. I fixed it in the DFR_LCD_Keypad.h file.

.#define DFR_LCD_KEYPAD_KEY_UP_ADC_LOW 120

#define DFR_LCD_KEYPAD_KEY_UP_ADC_HIGH 160

works now

I downloaded your sketch but when I tried to compile it I got the following error messages:

demo_simple:14: error: 'DFR_LCD_Keypad' does not name a type

demo_simple.ino: In function 'void loop()':

demo_simple:30: error: 'keypad' was not declared in this scope

demo_simple:42: error: 'KEY_RIGHT' was not declared in this scope

demo_simple:46: error: 'KEY_UP' was not declared in this scope

demo_simple:50: error: 'KEY_DOWN' was not declared in this scope

demo_simple:54: error: 'KEY_LEFT' was not declared in this scope

demo_simple:58: error: 'KEY_SELECT' was not declared in this scope

demo_simple:62: error: 'KEY_NONE' was not declared in this scope

I am a newbee with Arduino but am trying to learn C++, but would appreciate any suggestions you might have to fix it. Thanks, Rob

I got the same error when compiling on 1.8.1. I discovered that the Library file was in the wrong file directory on my PC. Once I moved it to the right spot it compiled fine for the Uno. I'm not sure if Arduino changed something with the newer IDE or I messed something up transferring everything to my new computer... I will go ahead and download the newist IDE and see if it will compile for me.

Thanks. I'll make sure my files are in the correct place as well. Just very frustrating to copy someone else's code - which works - and find I can't compile it or there are error messages. Again, thanks for your help and interest.

Thats the only thing I can think of. I uninstalled the Arduino IDE and deleted all my files, installed the newest IDE and added all my files back to the correct directory. Ran the code and it compiled with out any errors. I know it can be very frustrating, I have found many codes were their is some sort of problem. Most of the time it seems to be with the library's either something is wrong or it can't be found online anymore. Hope you get this working!

Great advice! I uninstalled then reinstalled IDE 1.8.5. Had moved all my libraries and renamed the folder. Then downloaded new libraries and made sure I put them in the right (Libraries) folder with proper names. Everything works now and I have a functional LCD shield-RTC-Uno clock! Thanks so much for your help. Regards, R

Awesome! Glad you got it working.

Did you download and instll the library?

I did, or at least I think I did, as they show up in the sketch. I tried in both IDE 1.8.5 (compiling error) and 1.0.5. Here are the screen shots. And thanks for your help!

Hi

Thank you for such great information, I now have a functional LCD. The only issue is that the UP button does not work, could it be that I have a broken LCD shield? I am using a shield made by SainSmart. Thank you again

Victor

It could be the LCD shield or to code. I'm
not sure if yours works the same as mine. You can check the shield by
using a multimeter and check the continuity of the button and then
follow the traces to the pins and recheck. You should only have
continuity if the button is pressed. If that checks out it could either
be a problem with the Arduino GPIO connectors (they ware out over time)
or could be a problem with the library Pinout. If thats the case you can
modify it or #define in the sketch.