Power Requirements
A quality 5V power source is essential for the LCD & Keypad Shield to achieve maximum backlight and display contrast. Using the LCD Shield along with the Arduino powered by USB might lead to a voltage decrease through the cable. If you are experiencing issues with the contrast of the display or the brightness of the backlight, attempt connecting a power source of approximately 7 to 9Vdc to the Arduino’s 2.1mm DC jack. In an undervoltage scenario, a common sign is when one line of the LCD displays light-colored rectangles instead of characters, while the other line remains blank. The Arduino can still operate properly at around 4V, but the LCD & Keypad Shield will not work.
Library Requirements
The LiquidCrystal library, included in the official Arduino distribution, manages all the tasks associated with interfacing with the LCD Shield. To determine if it is installed, open the IDE and navigate to Files -> Examples -> LiquidCrystal. If it is present, you are all set.
Minimal Display Example
To start up the LCD and display a message, open a new sketch in the Arduino IDE and paste in the following code:
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 );
void setup(){ lcd.begin(16, 2); lcd.print(“hello, world!”);}
void loop(){ // your main loop code here…}
Reading The Buttons
The LCD Shield has 5 buttons intended for navigation or control input. The buttons are organized in a convenient layout and named UP, DOWN, LEFT, RIGHT, and SELECT, however it is completely up to your design to determine the action taken when a specific button is pushed.
A chain of resistors connects all buttons to a single analog input A0, providing a unique reference voltage to A0 based on the pressed button. This part of the shield diagram displays the input buttons and corresponding resistors.
If no button is pressed, the voltage on A0 will be raised to 5V by the 2K resistor known as R6. None of the other resistors play a role in that scenario, and the analog reading on A0 will max out at 1023. So, if you use the analogRead() function on A0 and the result is 1023 (or higher than approximately 1000), you can conclude that no buttons are currently being pressed.
Now imagine the outcome when the “DOWN” button is pushed. Currently, A0 is receiving a voltage that is split between the 2K resistor attempting to pull it to 5V, and the series of 330R and 620R resistors (totaling 950R) attempting to pull it to 0V. In this scenario, A0 receives a voltage of approximately 1.61V, causing an analogRead() on A0 to result in a value of around 329. If A0 reads around 329, then it means the “DOWN” button is being pressed.
The same concept is valid for the remaining buttons, with their respective voltages and corresponding analogRead() values displayed in the above diagram.
This is an efficient method to have a complete set of input buttons with just one I/O pin on your Arduino, allowing other pins to be available for your project.
Complex Example
The following detailed example uses various methods to illustrate how to display messages on the LCD, read input from buttons, and update the display message based on the button inputs.
Sample code for the Freetronics LCD & Keypad Shield:
by Marc Alexander, 7 September 2011
This example code is in the public domain.
This program demonstrates button detection, LCD text/number printing,
and LCD backlight control on the Freetronics LCD & Keypad Shield, connected to an Arduino board.
After powerup, the screen looks like this:
|Freetronics 16×2|
|Btn: 0 | <- This time value counts up the number of seconds since reset (overflows at 99)
When a button is pressed, a label appears for it:
|Freetronics 16×2|
|Btn:RIGHT 0 |
Labels are LEFT, UP, DOWN, RIGHT and SELECT-FLASH.
SELECT-FLASH makes the LCD backlight flash off and on when held down.
Pins used by LCD & Keypad Shield:
A0: Buttons, analog input from voltage ladder
D4: LCD bit 4
D5: LCD bit 5
D6: LCD bit 6
D7: LCD bit 7
D8: LCD RS
D9: LCD E
D3: LCD Backlight (high = on, also has pullup high so default is on)
ADC voltages for the 5 buttons on analog input pin A0:
RIGHT: 0.00V : 0 @ 8bit ; 0 @ 10 bit
UP: 0.71V : 36 @ 8bit ; 145 @ 10 bit
DOWN: 1.61V : 82 @ 8bit ; 329 @ 10 bit
LEFT: 2.47V : 126 @ 8bit ; 505 @ 10 bit
SELECT: 3.62V : 185 @ 8bit ; 741 @ 10 bit*/
For more detail: LCD & Keypad Shield Quickstart Guide