Hardware Reference
In-Depth Information
with the display using the most important functions. You can find descriptions
of the library functions and examples illustrating their use on the Arduino
website: http://arduino.cc/en/Reference/LiquidCrystal (also linked from
www.exploringarduino.com/content/ch10 ).
AddingTexttotheDisplay
In this first example, you add some text and an incrementing number to the
display. This exercise demonstrates how to initialize the display, how to write
text, and how to move the cursor. First, include the LiquidCrystal library:
#include <LiquidCrystal.h>
Then, initialize an LCD object, as follows:
LiquidCrystal lcd (2,3,4,5,6,7);
The arguments for the LCD initialization represent the Arduino pins con-
nected to RS, EN, D4, D5, D6, and D7, in that order. In the setup, you call the
l i bra r y 's begin() function to set up the LCD display with the character size.
(The one I'm using is a 16n2 display, but you might be using another size, such
as a 20n4.) The arguments for this command represent the number of columns
and the number of rows, respectively:
lcd.begin(16, 2);
After doing that, you can call the library's print() and setCursor() com-
mands to print text to a given location on the display. For example, if you want
to print my name on the second line, you issue these commands:
lcd.setCursor(0,1);
lcd.print("Jeremy Blum");
The positions on the screen are indexed starting with (0,0) in the top-left
position. The first argument of setCursor() specifies which column number,
and the second specifies which row number. By default, the starting location is
(0,0). So, if you call print() without first changing the cursor location, the text
starts in the top-left corner.
WARNING Thelibrarydoesnotcheckforstringsthataretoolong.So,ifyou
trytoprintastringstartingatposition0thatislongerthanthenumberofcharac-
tersintherowyouareaddressing,youmightnoticestrangebehavior.Makesure
tocheckthatwhateveryouareprintingwillfitonthedisplay!
Search WWH ::




Custom Search