Which Libraries are needed for LCD KeyPadShield?

Hi!

I'm new to arduino and I just bought a KIT which includes the "SainSmart UNO" and "SainSmart 1602 LCD Keypad Shield". I've successfully played around with the blink demo code while the display was not connected. When I try to use the LCD KeyPad Shield I cannot get any example code to work. I've downloaded the arduino IDE 1.0.2 but none of the included example codes seem to work with my LCD KeypadShield (remains blank):
http://www.picvalley.net/v.php?p=u/2636/13287145033312739071355129756pYtDMOTH3OvlwZMRiYfw.JPG

Could anyone please tell me what to do to run an example code like Hello World? I've found various downloads but they don't come with an explanation where to store them (in the libraries directory?). Without (proper) preparation and none of them compiled.

Thanks in advance!
T.

The library is a folder usually containing two files, dadedah.cpp and dadedah.h, along with a folder full of examples. It lives in the \libraries folder which, in my case, is under \arduino-1.0.1.

You can use these examples but, when you modify or save under another name, they don't go where they came from but are stored under \arduino\My Sketches. I don't think this folder exists until you take some action. It is then created automatically.

When I installed the latest Arduino IDE on a laptop, the directory structure was a little different but operates the same way.

The standard examples are included in the file menu, you shouldn't have to download anything and I'm sure hello world and autoscroll are amongst them.

The display problem may just be the pin configuration. If your shield lights up, it is probably OK. You should be able to twiddle the potentiometer at the top left and see at least one row of black boxes. I don't think you need any code for this. I had two or three attempts before I got the code right. I eventually used the Mellis & Igoe code included in the IDE but changed the initialisation line to

LiquidCrystal lcd(8,9,4,5,6,7);

This was the only change made to the standard code in the IDE. This different pin setup is simply due to changes in the design of the shield, for which there are no hard and fast design rules. Your shield looks exactly the same as mine. If you change that line, it might work. If so, you can then use it for every sketch applicable to that hardware combination. If it doesn't work it doesn't necessarily mean it's a dud, it's just different from mine.

The shield consists of two parts, the LCD module and the Arduino-compatible board. If you turn it over you can probably see the tracks and identify which LCD pin is connected with which Arduino pin. If you can't get a result with the above line, the pin connections will be useful to know! The included sketch has the anticipated connections listed.

The circuit says but on my shield it is to

  • LCD RS pin to digital pin 12 8
  • LCD Enable pin to digital pin 11 9
  • LCD D4 pin to digital pin 5 4
  • LCD D5 pin to digital pin 4 5
  • LCD D6 pin to digital pin 3 6
  • LCD D7 pin to digital pin 2 7

Hence the line LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
now becomes LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

Note that it is looking for the pins in the list order, not numerical order. Don't change it.

FWIW I had grief with the buttons too, and initially gave up on them completely. The problem was much the same and I got a result with the line

LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);

No extra libraries are needed for the buttons.

Hi NickPyner!

Thanks for your answer. But I still cant get this working.

Downloading and unpacking arduino-1.0.2-windows.zip results in a different files and library structure than you described. However, since the basic examples work fine, I think the basic setup is OK.

I assume I should be able to choose an example from File/Examples (I have a German version, but that's probably the translation).

  • I try File/Examples/LiquidCrystal/HelloWorld.
  • I successfully compile it with CTRL-R.
  • I successfully upload it with CTRL-U

But the display remains showing the very same like the original photo I linked in my first posting. After a minute or so the backlight went off. I have the impression that there's something wrong with this hardware but I'm trying to make sure it's not ME doing something wrong.

What makes me quite sure that the hardware is broken is that after the backlight went off, it went on again after I touched the board. So I think there probably a bad solder joint somewhere. Or whatever. But before complaining to the seller I would like to be sure it's not me doing something wrong.

torstenv:
Downloading and unpacking arduino-1.0.2-windows.zip results in a different files and library structure than you described. However, since the basic examples work fine, I think the basic setup is OK.

Yes, I have that version on the laptop. It is not fundamentally different.

I assume I should be able to choose an example from File/Examples (I have a German version, but that's probably the translation).

  • I try File/Examples/LiquidCrystal/HelloWorld.
  • I successfully compile it with CTRL-R.
  • I successfully upload it with CTRL-U

But the display remains showing the very same like the original photo I linked in my first posting. After a minute or so the backlight went off. I have the impression that there's something wrong with this hardware but I'm trying to make sure it's not ME doing something wrong.

What makes me quite sure that the hardware is broken is that after the backlight went off, it went on again after I touched the board. So I think there probably a bad solder joint somewhere. Or whatever. But before complaining to the seller I would like to be sure it's not me doing something wrong.

Hummm. Funny you should mention that.... The reason why I have not gone to bed yet is that I have had some embarrassing grief with my LCD stuff. It is now pretty clear that the problem was that I have just changed the motherboard in my desktop and the power from the USB cable is now inadequate. I have just connected an external PSU and all is well. You may get a result just by changing the USB port if you don't have a separate 9v supply, but what you are describing does tend to point to a bad connection. You might be able to see it if you look closely as I think it is more likely to be a connection than a board fault.

The display problem may just be the pin configuration. If your shield lights up, it is probably OK. You should be able to twiddle the potentiometer at the top left and see at least one row of black boxes. I don't think you need any code for this. I had two or three attempts before I got the code right. I eventually used the Mellis & Igoe code included in the IDE but changed the initialisation line to

LiquidCrystal lcd(8,9,4,5,6,7);

This was the only change made to the standard code in the IDE. This different pin setup is simply due to changes in the design of the shield,

Agreed. But what fact did you base on when you provided that line of code? You said different designs could have different pin setups so then your suggestion is based on which particular design documented where?

OP, Check the pins between lcd top left corner and the pins that insert to arduino to make sure they are all connected according to the pin diagram found here:

http://www.sainsmart.com/sainsmart-1602-lcd-keypad-shield-for-arduino-duemilanove-uno-mega2560-mega1280.html

Look for document for arduino 1.0

You should modify your example code to have:

LiquidCrystal lcd(8,9,4,5,6,7);

I see NickPyner already told you the same but somehow he forgot to mention this particular pin arrangement is derived directly from the seller-provided document found at that link I provided above. Might be an overlook.

Also, disregard this part from his reply:

FWIW I had grief with the buttons too, and initially gave up on them completely. The problem was much the same and I got a result with the line

LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);

No extra libraries are needed for the buttons.

I don't see any reason that helped on your keypad. Not sure what shield you have either. BTW, I don't remember your hardware across threads.

@NickPyner, I suggest you base your comment on more solid reasoning. If you said you read that above link and thought the OP should use LiquidCrystal lcd(8,9,4,5,6,7); then everyone is happy. Your "this code now works with my hardware" will not help anyone else even including yourself. Programming software and hardware is a serious business and facts are linked by logic. You should tell the OP why you think adding 13 makes YOUR shield buttons work to help let him decide whether to take your suggestion. I know I said "should". But I based that on my 3+ years on the forum helping those that need help and a lot more years in a real classroom environment training students and future teachers. You are very welcome to help out here but there are non-written rules of being a helper. Help but only provide sound advice that is relevant to the OP's own hardware and software. Something like that. Fire back at me if you want but I have a full day of work ahead of myself and won't check back on this thread till I punch out for the day.

The problem was much the same and I got a result with the line

LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);

Do you understand how this change (from the end of reply # 1) affects the operation of the library and how it does or does not affect your particular shield?

I know the answer to the first part but not the second.

Post a link to the datasheet or schematic diagram for your shield and I can help you answer the question.

Don

In my haste to try and help the helper I forgot the original question.

"Which Libraries are needed for LCD KeyPadShield?"

It looks like you should use the LiquidCrystal library to deal with the display and use the library provided in the 'Download Link' at the SainSmart website for the keypad.

Disregard any references to a 4bit Arduino LCD library, that library is ancient history.

I know that this information about the keypad conflicts with that offered in reply #1 ("No extra libraries are needed for the buttons") so use your common sense to decide who to believe.

Don

liudr:
You should modify your example code to have:

LiquidCrystal lcd(8,9,4,5,6,7);

I see NickPyner already told you the same but somehow he forgot to mention this particular pin arrangement is derived directly from the seller-provided document found at that link I provided above. Might be an overlook.

@NickPyner, I suggest you base your comment on more solid reasoning.

There is no somehow about it, and it wasn't an overlook. I don't have a sainsmart shield, just one that looks the same, as I made very clear, and it came with no documentation.

As I also surely made very clear, I determined the pin assignment by reading the back of the circuit board, hence the comprehensive list in my reply #1, and the two configuration lines compared. I would have thought that was reasoning solid enough on which to base my comment. You may find that reasoning not solid enough, but I'm not sure I want hear about that.

I might also point that the comment on the buttons was as casual as was surely implied. With that configuration, the button sketch worked for me, it might for the OP, and it costs nothing if it doesn't. I didn't write it, I'm not interested in buttons. But I will correct what I said to "No extra libraries were required for the buttons.........

/*
  The circuit:
 * LCD RS pin to digital pin 8
 * LCD Enable pin to digital pin 9
 * LCD D4 pin to digital pin 4
 * LCD D5 pin to digital pin 5
 * LCD D6 pin to digital pin 6
 * LCD D7 pin to digital pin 7
 * LCD BL pin to digital pin 10
 * KEY pin to analog pin 0        (14)  THIS IS THE BUTTON PIN
 */

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);

char msgs[5][16] = {"Right Key OK ",
                    "Up Key OK    ",               
                    "Down Key OK  ",
                    "Left Key OK  ",
                    "Select Key OK" };

int adc_key_val[5] ={50, 200, 400, 600, 800 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;

void setup()
{
    Serial.begin(9600);
  lcd.clear(); 
  lcd.begin(16, 2);
  lcd.setCursor(0,0); 
  lcd.print("ADC key testing"); 
}

void loop()
{ //1

 
  if (key != oldkey)   // if keypress is detected "!=" means not equal!!
   {//2
    delay(50);  // wait for debounce time
    adc_key_in = analogRead(0);    // read the value from the sensor 
    key = get_key(adc_key_in);    // convert into key press
    if (key != oldkey)    
    {//3   
      lcd.setCursor(0, 1);
      oldkey = key;
      if (key >=0){//4
           lcd.print(msgs[key]);
      Serial.println(key); 
      
      if (key == 2)
 {
  Serial.println("            down  ");
 }
      
      
lcd.setCursor(15,1);  
lcd.print(key);

      }//4
    }//3

   }//2
  /*
 if (key = 800)
 {
   delay(2000);
   lcd.clear();
  }
*/
 delay(100);
 
}//1

// Convert ADC value to key number
int get_key(unsigned int input)
{
    int k;
   
    for (k = 0; k < NUM_KEYS; k++)
    {
      if (input < adc_key_val[k])
 {
            return k;
        }
   }
   
    if (k >= NUM_KEYS)k = -1;  // No valid key pressed
    return k;
}

I might also point that the comment on the buttons was as (sic) casual as was surely implied.

With my background I tend to concentrate my responses based on what is stated, not what is implied.

Now that you have posted some relevant code your statement that "No extra libraries are needed for the buttons" is correct. You haven't mentioned where it came from but the code that you posted uses the LiquidCrystal library to deal with the display but it also contains the code required to read the keypad. So your statement is correct for your implementation.

Is the OP using the same code as you? I don't know and you probably don't either.

I know that you are trying to be helpful but you should think about how your posts are being interpreted by others. In some instances you have passed along information that worked for you, which is fine, but in my opinion you should not say or imply that that is the correct or the best solution.

Have you given any thought to the question about pin 13?

Don

torstenv,
In terms of your core original question, as to which library can be used to drive the LCD.
The LiquidCrystal library that comes with the IDE since about IDE 0016 or so will work.
The issue you will have is that all the examples that come with the LiquidCrystal library use
a different pin wiring to the LCD than the shield you have. That is why you can't simply
use any of the examples. They do work, they just won't work "as is" with your LCD-keypad shield
since the internal wiring on the PCB of the shield hooks up different Arduino pins to the LCD than
what the examples are configuring the LiquidCrystal library to use.
You can either go directly modify the LCD constructor in the LiquidCrystal library examples directory
to use the pins for your shield pins or make copies of them and modify the copies.

The "libraries" directory referred to is underneath where ever you installed the Arduino s/w.
i.e. in this case the examples are located:
{installdir}/libraries/LiquidCrystal/examples

The IDE wants to see the directory name and sketch name in the examples directory match.
So if you make copies under an existing directory, you need to make sure the directory and sketch match.
You also must exit and re-start the IDE every time you create a new directory with a sketch.

You can also put them in your private sketch area which also can have a "libraries" area.
Simply copy examples from the LiquidCrystal library area and put them in your private
sketch are and leave the originals alone.
This is a better approach as your private area will continue to show up for future
IDE installs vs just being in the release you happen to modify/update.
To locate your private sketch area location, bring up the IDE and click on
[File]->[Preferences]
It is there in the Sketchbook location box.
What I do to make it simpler and more obvious and to plan for the future,
is to go to that location and create 3 sub-directories:
hardware
libraries
sketches

hardware - is used for alternate cores like attiny, 644/1284 etc... ( you might not ever use this)

libraries - can be used for 3rd party libraries that you may like and collect over time say:
IRremote, temperature, OneWire, RTC, etc...

sketches - well that is where you create directories for your sketches.

The nice thing about using this structure is that you only have to set it up once
and then it will show up on all future IDEs.
To use the sketches, from your private area in the IDE simply click on
[File]->[Sketchbook]->
Then you can click on "libraries" or "sketch" then drill down to pick the desired
sketch.

Hope that helps.
BTW, take note of the backlight control issue in the thread mentioned
at the bottom of this reply.
Some of newer LCD shields have fixed this issue but many have not so
it is worth a read to understand the potential issue.

--- bill


NickPyner:
I don't have a sainsmart shield, just one that looks the same, as I made very clear, and it came with no documentation.

As I also surely made very clear, I determined the pin assignment by reading the back of the circuit board, hence the comprehensive list in my reply #1, and the two configuration lines compared. I would have thought that was reasoning solid enough on which to base my comment. You may find that reasoning not solid enough, but I'm not sure I want hear about that.

Nick,
So you are saying that your LCD-keypad shield is not the same as the sainsmart LCD-keypad
shield in that the R/W line is wired to Arduino pin 13 (the Arduino LED) on your shield?

It's possible, (I've seen some crazy things out there in Arduino land)
but so far I've not seen an LCD-keypad shield that has done this. All the ones I've seen, including the SainSmart,
YwROBOT, LinkSprite, DFRrobot, Emartee, and many other LCD-keypad shields, hardwire the LCD R/W pin to gnd
so they do not waste and Arduino pin. This is a good design decision
since the Arduino supplied LiquidCrystal library does not use BUSY polling and
sets R/W pin (if specified) to ground anyway.

Many of the LCD-keypad shield designs seem to have copied one another all the way down to a hardware
design issue with respect to controlling the LCD backlight - on the boards that allow backlight control.
See this thread for issues related to that issue:
http://arduino.cc/forum/index.php/topic,96747.0.html

With respect to backlight control. I would encourage anyone using the shields backlight control
capability to be very careful using it (i.e. don't use it) until you know if the shield has
a proper backlight circuit.
Many of the lcd shields out there have a bad circuit and are overloading the pin
being used for backlight control when controlling it using simple digitalWrite() functions
or analogWrite() for dimming.

NickPyner:
There is no somehow about it, and it wasn't an overlook. I don't have a sainsmart shield, just one that looks the same, as I made very clear, and it came with no documentation.

This is common between you and me. I don't have a sainsmart shield either, not even one that remotely looks like it. I have LCD shields I designed and sell. They look and work a lot better;). What separates us is the fact that I found the document of the sainsmart shield and read its diagram. This is as good as having a working sainsmart shield in front of me. I then made the suggestion about the code, which happened to be the same as yours. It's more work helping others than helping yourself. It also gives you peace of mind that your comment is going to work for the OP. They won't curse you if anything bad should happen due to you giving them the wrong info. Something bad could happen. It's not free to try. When electronics aren't connected correctly, they get burned up. Everyone feels bad.

bperrybap:

NickPyner:
I don't have a sainsmart shield, just one that looks the same, as I made very clear, and it came with no documentation.

As I also surely made very clear, I determined the pin assignment by reading the back of the circuit board, hence the comprehensive list in my reply #1, and the two configuration lines compared.

Nick,
So you are saying that your LCD-keypad shield is not the same as the sainsmart LCD-keypad
shield in that the R/W line is wired to Arduino pin 13 (the Arduino LED) on your shield?

No, I only said it looks the same. I certainly did not say it is not the same in that the R/W line is wired to pin 13. Indeed, quiet the opposite, the the only reason why I wrote anything is that my pin arrangement could be just like the Sainsmart, hence the config line I posted. I'm afraid I had forgotten that the button code is not in the IDE Examples, and my only real purpose was to assure OP that he didn't need a library for buttons. Apparently, this is not correct.....

It's possible, (I've seen some crazy things out there in Arduino land)

I take your point, I was suss about the arrangement, and still don't understand it. The R/W track is top-of-board, and that doesn't help. The sketch works and that was all I needed. After that, it is a bit of a dog-chasing-car scene. I'm not interested in buttons, I just wanted to be sure they could work. I did ask about this but didn't get anywhere. I never even found out why my previous attempts did not work.

but so far I've not seen an LCD-keypad shield that has done this. All the ones I've seen, including the SainSmart,
YwROBOT, LinkSprite, DFRrobot, Emartee, and many other LCD-keypad shields, hardwire the LCD R/W pin to gnd
so they do not waste and Arduino pin. This is a good design decision
since the Arduino supplied LiquidCrystal library does not use BUSY polling and
sets R/W pin (if specified) to ground anyway.

I suppose I could try it with pin 13 out of the config! Apart from display pins 4,5,6,7 and RS on pin 8, the only other pin I know is used, is pin 14.

Many of the LCD-keypad shield designs seem to have copied one another all the way down to a hardware
design issue with respect to controlling the LCD backlight - on the boards that allow backlight control.

Yes, I can't see how these things can vary much and there are probably only a handful of PCB designs common to dozens of manufacturers. Hence my comments in the first place, particularly about Freetronics. I have steered clear of backlight control. I don't know if my shield ever had it, and it very likely doesn't now, as I have clipped some pins.

Are you familiar with the Nokia 5110, and do you think the same caution about backlight applies to it? And, while you are at it, do you have any comment on backlight and contrast in near-freezing conditions? I'm getting a bit concerned about this but, down here in Godzone, it's not so easy to test.

@Nick

Please try to understand that we are not trying to put you or anyone else down or start a flame war when we correct erroneous statements.

You have to understand that unless a thread somehow gets deleted it will virtually be around forever. This means that in the future someone with a similar shield who is having trouble with the keypad part may stumble upon this thread. He could read your unqualified statement that "No extra libraries are needed for the buttons" and not realize that unless the code required to deal with the pushbuttons is included in the sketch then a library is precisely what is needed.

Similarly your recommendation to use a constructor with seven terms in the argument (

LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);

) is just plain wrong for that shield, for many other shields, and for the circuits used with most LCD tutorials that do not use a shield. In certain circumstances it could cause a sketch to work improperly and in others it could cause component damage. In yet some other cases it will have no effect but I still can't see how it possibly could have helped in yours.

Don