[go: up one dir, main page]

0% found this document useful (0 votes)
70 views12 pages

LCD in Detials

This document discusses liquid crystal displays (LCDs) and how to connect and control them using an Arduino. It explains that the most common LCD panel uses the Hitachi HD44780 chip and can display 2 or 4 lines of 16 or 20 characters each. The Arduino LCD library allows controlling these LCDs in 4-bit or 8-bit mode by connecting the data lines and control lines to Arduino pins. It provides functions for initializing the LCD, writing text, positioning the cursor, and controlling the display settings.

Uploaded by

Jun Hast
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
70 views12 pages

LCD in Detials

This document discusses liquid crystal displays (LCDs) and how to connect and control them using an Arduino. It explains that the most common LCD panel uses the Hitachi HD44780 chip and can display 2 or 4 lines of 16 or 20 characters each. The Arduino LCD library allows controlling these LCDs in 4-bit or 8-bit mode by connecting the data lines and control lines to Arduino pins. It provides functions for initializing the LCD, writing text, positioning the cursor, and controlling the display settings.

Uploaded by

Jun Hast
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Liquid crystal display (LCD)

Liquid crystal displays (LCDs) offer a convenient and inexpensive way to provide
a user interface for a project. This lecture explains how to connect and use
common text and graphical LCD panels with Arduino. By far the most popular
LCD is the text panel based on the Hitachi HD44780 chip. This displays two or
four lines of text, with 16 or 20 characters per line (32- and 40-character versions
are available, but usually at much higher prices). A library for driving text LCD
displays is provided with Arduino, and you can print text on your LCD as easily
as on the Serial Monitor because LCD and serial share the same underlying print
functions. LCDs can do more than display simple text: words can be scrolled or
highlighted and you can display a selection of special symbols and non-English
characters. You can create your own symbols and block graphics with a text
LCD, but if you want fine graphical detail, you need a graphical display. Graphical
LCD (GLCD) displays are available at a small price premium over text displays,
and many popular GLCD panels can display up to eight lines of 20 text characters
in addition to graphics. LCD displays have more wires connecting to Arduino than
most other things. Incorrect connections are the major cause of problems with
LCDs, so take your time wiring things up and triple-check that things are
connected correctly. An inexpensive multimeter capable of measuring voltage and
resistance is a big help for verifying that your wiring is correct. It can save you a
lot of head scratching if nothing is being displayed. You don’t need anything
fancy, as even the cheapest multimeter will help you verify that the correct pins
are connected and that the voltages are correct.
The Arduino library allows an Arduino board to control LCDs based on the
Hitachi HD44780 (or a compatible) chipset, which is found on most text-
based LCDs. The library works with in either 4- or 8-bit mode (i.e. using 4 or 8
data lines in addition to the rs, enable, and, optionally, the rw control lines.
 LiquidCrystal()
Description
Creates a variable of type Liquid Crystal. The display can be controlled using 4
or 8 data lines. If the former, omit the pin numbers for d0 to d3 and leave those
lines unconnected. The RW pin can be tied to ground instead of connected to a
pin on the Arduino; if so, omit it from this function's parameters.
Syntax
LiquidCrystal(rs, enable, d4, d5, d6, d7)
LiquidCrystal(rs, rw, enable, d4, d5, d6, d7)
LiquidCrystal(rs, enable, d0, d1, d2, d3, d4, d5, d6, d7)
LiquidCrystal(rs, rw, enable, d0, d1, d2, d3, d4, d5, d6, d7)
Parameters
rs: the number of the Arduino pin that is connected to the RS pin on the LCD
rw: the number of the Arduino pin that is connected to the RW pin on the LCD
(optional)
enable: the number of the Arduino pin that is connected to the enable pin on the
LCD
d0, d1, d2, d3, d4, d5, d6, d7: the numbers of the Arduino pins that are
connected to the corresponding data pins on the LCD.
d0, d1, d2, and d3 are optional; if omitted, the LCD will be controlled using only
the four data lines (d4, d5, d6, d7).

Example
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
void setup()
{
lcd.begin(16, 2);
lcd.print("Hello Students");
}
void loop() {}

 begin()
Description
Initializes the interface to the LCD screen, and specifies the dimensions (width
and height) of the display.
begin() needs to be called before any other LCD library commands.
Syntax
lcd.begin(cols, rows)
Parameters
lcd: a variable of type LiquidCrystal
cols: the number of columns that the display has ‫عدد األعمدة التي تحتوي عليها الشاشة‬
rows: the number of rows that the display has
 clear()
Description
Clears the LCD screen and positions the cursor in the upper-left corner.
Syntax
lcd.clear()
Parameters
lcd: a variable of type LiquidCrystal

 home()
Description
Positions the cursor in the upper-left of the LCD. That is, use that location in
outputting subsequent text to the display. To also clear the display, use
the clear() function instead.
Syntax
lcd.home()
Parameters
lcd: a variable of type LiquidCrystal

 setCursor()
Description
Position the LCD cursor; that is, set the location at which subsequent text written to
the LCD will be displayed.
Syntax
lcd.setCursor(col, row)
Parameters
lcd: a variable of type LiquidCrystal
col: the column at which to position the cursor (with 0 being the first column)
row: the row at which to position the cursor (with 0 being the first row)

 write()
Description
Write a character to the LCD.
Syntax
lcd.write(data)
Parameters
lcd: a variable of type LiquidCrystal
data: the character to write to the display
Returns
byte
write() will return the number of bytes written, though reading that number is
optional
Example
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
void setup()
{
Serial.begin(9600);
}
void loop()
{
if (Serial.available()) {
lcd.write(Serial.read());
}
}

 print()
Description
Prints text to the LCD.
Syntax
lcd.print(data)
lcd.print(data, BASE)
Parameters
lcd: a variable of type LiquidCrystal
data: the data to print (char, byte, int, long, or string)
BASE (optional): the base in which to print numbers: BIN for binary (base 2),
DEC for decimal (base 10), OCT for octal (base 8), HEX for hexadecimal (base
16).
Returns
byte
print() will return the number of bytes written, though reading that number is
optional

Example
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
void setup()
{
lcd.print("hello, world!");
}
void loop() {}
 cursor() and noCursor()
Description
Display the LCD cursor: an underscore (line) at the position to which the next
character will be written.
Syntax
lcd.cursor()
Parameters
lcd: a variable of type LiquidCrystal

 noCursor()
Description
Hides the LCD cursor.
Syntax
lcd.noCursor()
Parameters
lcd: a variable of type LiquidCrystal

Example

 cursor() and noCursor()

#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// Turn off the cursor:
lcd.noCursor();
delay(500);
// Turn on the cursor:
lcd.cursor();
delay(500);
}
 blink()
Description
Display the blinking LCD cursor. If used in combination with the cursor() function,
the result will depend on the particular display.
Syntax
lcd.blink()
Parameters
lcd: a variable of type LiquidCrystal

 noBlink()
Description
Turns off the blinking LCD cursor.
Syntax
lcd.noBlink()
Parameters
lcd: a variable of type LiquidCrystal

Example

blink() and noBlink()


#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}

void loop() {
// Turn off the blinking cursor:
lcd.noBlink();
delay(3000);
// Turn on the blinking cursor:
lcd.blink();
delay(3000);
}
 display()
Description
Turns on the LCD display, after it's been turned off with noDisplay(). This will
restore the text (and cursor) that was on the display.
Syntax
lcd.display()
Parameters
lcd: a variable of type LiquidCrystal

 noDisplay()
Description
Turns off the LCD display, without losing the text currently shown on it.
Syntax
lcd.noDisplay()
Parameters
lcd: a variable of type LiquidCrystal

Example

display() and noDisplay()

#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}

void loop() {
// Turn off the display:
lcd.noDisplay();
delay(500);
// Turn on the display:
lcd.display();
delay(500);
}
 scrollDisplayLeft()
Description
Scrolls the contents of the display (text and cursor) one space to the left.
Syntax
lcd.scrollDisplayLeft()
Parameters
lcd: a variable of type LiquidCrystal

 scrollDisplayRight()
Description
Scrolls the contents of the display (text and cursor) one space to the right.
Syntax
lcd.scrollDisplayRight()
Parameters
lcd: a variable of type LiquidCrystal

#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
delay(1000);
}

void loop() {
// scroll 13 positions (string length) to the left
// to move it offscreen left:
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(150);
}
// scroll 29 positions (string length + display length) to the right
// to move it offscreen right:
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
// scroll one position right:
lcd.scrollDisplayRight();
// wait a bit:
delay(150);
}

// scroll 16 positions (display length + string length) to the left


// to move it back to center:
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
// wait a bit:
delay(150);
}

// delay at the end of the full loop:


delay(1000);

 autoscroll()
Description
Turns on automatic scrolling of the LCD. This causes each character output to the
display to push previous characters over by one space. If the current text direction is
left-to-right (the default), the display scrolls to the left; if the current direction is
right-to-left, the display scrolls to the right. This has the effect of outputting each
new character to the same location on the LCD.
Syntax
lcd.autoscroll()
Parameters
lcd: a variable of type LiquidCrystal

 noAutoscroll()
Description
Turns off automatic scrolling of the LCD.
Syntax
lcd.noAutoscroll()
Parameters
lcd: a variable of type LiquidCrystal
 leftToRight()
Description
Set the direction for text written to the LCD to left-to-right, the default. This means
that subsequent characters written to the display will go from left to right, but does
not affect previously-output text.
Syntax
lcd.leftToRight()
Parameters
lcd: a variable of type LiquidCrystal

 rightToLeft()
Description
Set the direction for text written to the LCD to right-to-left (the default is
left-to-right). This means that subsequent characters written to the display
will go from right to left, but does not affect previously-output text.
Syntax
lcd.rightToLeft()
Parameters
lcd: a variable of type LiquidCrystal

 createChar()
Description
Create a custom character (gylph) for use on the LCD. Up to eight characters of 5x8
pixels are supported (numbered 0 to 7). The appearance of each custom character is
specified by an array of eight bytes, one for each row. The five least significant bits
of each byte determine the pixels in that row. To display a custom character on the
screen, write() its number.
NB : When referencing custom character "0", if it is not in a variable, you need to
cast it as a byte, otherwise the compiler throws an error. See the example below.
Syntax
lcd.createChar(num, data)
Parameters
lcd: a variable of type LiquidCrystal
num: which character to create (0 to 7)
data: the character's pixel data

Example
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2)
byte smiley[8] = {
B00000,
B10001,
B00000,
B00000,
B10001,
B01110,
B00000,
};
void setup() {
lcd.createChar(0, smiley);
lcd.begin(16, 2);
lcd.write(byte(0));
}
void loop() {}

Example:

#include <LiquidCrystal.h>

int tempPin = 0;
int lightPin = 1;

// rs e d4 d5 d6 d7
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);

void setup()
{
lcd.begin(16, 2);
}

void loop()
{
// Display Temperature in C and F
int tempReading = analogRead(tempPin);
float tempVolts = tempReading * 5.0 / 1024.0;
float tempC = (tempVolts - 0.5) * 100.0;
float tempF = tempC * 9.0 / 5.0 + 32.0;
// ----------------
lcd.print("TF=");
//lcd.setCursor(4, 0);
lcd.print(tempF);
lcd.print("TC=");
lcd.print(tempC);

// Display Light on second row


int lightReading = analogRead(lightPin);
lcd.setCursor(0, 1);
// ----------------
lcd.print("Light=");
//lcd.setCursor(6, 1);
lcd.print(lightReading);
delay(500);
}

You might also like