LiquidCrystal library for working with character LCD on Arduino

This library allows Arduino boards to drive liquid crystal displays (LCDs) based on the HD44780 controller (or equivalent), which is used in most character LCD displays. The library operates in either four or eight bit mode (i.e. 4 or 8 data lines are used in addition to the RS, EN, and RW control lines, if necessary).

LiquidCrystal library for working with character LCD on Arduino
LiquidCrystal library for working with character LCD on Arduino

Scheme

The examples below use the Arduino Uno board. Connect the following pins to connect the LCD display to your board:

  • output RS LCD display to digital output 12;
  • output of EN LCD display to digital output 11;
  • D4 LCD display pin to digital pin 5;
  • D5 LCD display pin to digital pin 4;
  • D6 LCD display pin to digital pin 3;
  • D7 LCD display pin to digital pin 2;

Also connect the LCD display's RW pin to ground. The extreme terminals of the 10 kΩ potentiometer must be connected to the + 5V and GND buses, and the middle terminal of the potentiometer must be connected to the VO pin (pin 3) of the LCD display. A 220 ohm resistor is used to power the display backlight (usually pins 15 and 16 of the LCD display).

Functions

LiquidCrystal ()
Creates a variable of type LiquidCrystal. The display can be controlled with 4 or 8 data lines. In the first case, the pin numbers from D0 to D3 are skipped, and these pins remain unconnected. RW pin, instead of connecting to Arduino, can be connected to ground; if so, it is skipped in the function 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: pin number of the Arduino board that is connected to the RS LCD pin of the display;
  • rw: the pin number of the Arduino board that is connected to the RW pin of the LCD display (optional)
  • en: pin number of the Arduino board, which is connected to the enable pin of the EN LCD display;
  • d0, d1, d2, d3, d4, d5, d6, d7: pin numbers of the Arduino board that are connected to the corresponding pins of the LCD display. d0, d1, d2 and d3 are optional; if omitted, the LCD will be controlled with only four data lines (d4, d5, d6, d7).

Example 

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
void setup()
{
  lcd.begin(16,1);
  lcd.print("hello, world!");
}
void loop() {}

begin ()
Initializes the communication interface with the LCD display, and indicates the dimensions (width and height) of the display. The begin () function must be called before any other functions in the LCD library.

Syntax

lcd.begin(cols, rows)

Parameters

lcd: variable of type LiquidCrystal;
cols: the number of columns (characters per line) of the display;
rows: the number of rows the display has.
clear ()
Clears the LCD and places the cursor in the upper left corner.

Syntax

lcd.clear()

Parameters

lcd: variable of type LiquidCrystal.
home ()
Places the cursor in the upper left corner of the LCD display. Used to place the cursor where the subsequent text will be displayed. To also clear the display, use clear ().

Syntax

lcd.home()

Parameters

lcd: variable of type LiquidCrystal.
setCursor ()
Places the cursor at the specified position of the LCD display. Used to place the cursor where the subsequent text will be displayed.

lcd.setCursor(col, row)

Parameters

lcd: variable of type LiquidCrystal;
col: the column where you want to place the cursor (the first column is 0);
row: the row to place the cursor on (the first row is 0).

write ()
Writes a character to the LCD display.

Syntax

lcd.write(data)

Parameters

lcd: variable of type LiquidCrystal;
data: character to write to LCD.
Return value

byte
write () returns the number of bytes written, although reading this number is optional.

#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 ()
Prints text on the LCD.

Syntax

lcd.print(data) 
lcd.print(data, BASE)

Parameters

lcd: variable of type LiquidCrystal;
data: data to print (char, byte, int, long or string);
BASE (optional): base for printing numbers: BIN for binary (base 2), DEC for decimal (base 10), OCT for octal (base 8), HEX for hexadecimal (base 16).
Return value

byte
print () returns the number of bytes written, although reading this number is optional.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);
void setup()
{
  lcd.print("hello, world!");
}
void loop() {}

cursor ()
Shows the cursor on the LCD display: an underline (line) where the next character will be written.

Syntax

lcd.cursor()

Parameters

lcd: variable of type LiquidCrystal.
Example

See an example for the noCursor () function.

noCursor ()
Hides the cursor on the LCD.

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
  lcd.begin(16, 2);
  lcd.print("hello, world!");
}

void loop() {
  // Выключить курсор:
  lcd.noCursor();
  delay(500);
  // Включить курсор:
  lcd.cursor();
  delay(500);
}

blink ()
Shows a flashing cursor on the LCD. When used in combination with cursor (), the result will depend on the particular display.

Syntax

lcd.blink()

Parameters

lcd: variable of type LiquidCrystal.
Example

See an example for the noBlink () function.

noBlink ()
Turns off the blinking cursor on the LCD display.

Syntax

lcd.noBlink()

Parameters

lcd: variable of type LiquidCrystal.
Example

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
  lcd.begin(16, 2);
  lcd.print("hello, world!");
}

void loop() {
  // Выключить мигающий курсор:
  lcd.noBlink();
  delay(3000);
  // Включить мигающий курсор:
  lcd.blink();
  delay(3000);
}

display()

Включает LCD дисплей после того, как он был выключен функцией noDisplay(). Она восстанавливает текст (и курсор), который был на дисплее.

Syntax

lcd.display()

Parameters

lcd: variable of type LiquidCrystal.
Example

See an example for the noDisplay () function.

noDisplay ()
Turns off the LCD display without losing the text that is currently displayed on it.

Syntax

lcd.noDisplay()

Parameters

lcd: variable of type LiquidCrystal.
Example

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
void setup() {
  lcd.begin(16, 2);
  lcd.print("hello, world!");
}

void loop() {
  // Turn off the display:
  lcd.noDisplay();
  delay(500);
  // Switch on display:
  lcd.display();
  delay(500);
}

scrollDisplayLeft ()
Scrolls the display contents (text and cursor) one position to the left.
Syntax

lcd.scrollDisplayLeft()

Parameters

lcd: variable of type LiquidCrystal.
Example

See an example for the scrollDisplayRight () function.

scrollDisplayRight ()
Scrolls the display contents (text and cursor) one position to the right.
Syntax

lcd.scrollDisplayRight()

Parameters

lcd: variable of type LiquidCrystal.
Example

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  lcd.begin(16, 2);
  lcd.print("hello, world!");
  delay(1000);
}

void loop() {
  // scroll 13 positions (line length) to the left,
  // to move content outside of the display to the left:
  for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
    // scroll one position to the left:
    lcd.scrollDisplayLeft();
    // wait a bit:
    delay(150);
  }

  // scroll 29 positions (line length + display length) to the right,
  // to move content outside the display to the right:
  for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
    // scroll one position to the right:
    lcd.scrollDisplayRight();
    // wait a bit:
    delay(150);
  }

  // scroll 16 positions (display length + line length) left,
  // to move content back to center:
  for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
    // scroll one position to the left:
    lcd.scrollDisplayLeft();
    // wait a bit:
    delay(150);
  }

  // delay at the end of a full cycle:
  delay(1000);

}

autoscroll ()
Enables automatic scrolling on the LCD display. This causes each character displayed on the display to move the previous characters one position. If the text direction is left-to-right (default), the display scrolls to the left; if the text direction is right-to-left, the display scrolls to the right. This results in the output of each new character in the same place on the LCD display.
Syntax

lcd.autoscroll()

Parameters

lcd: variable of type LiquidCrystal.
Example

See an example for the noAutoscroll () function.

noAutoscroll ()
Turns off automatic scrolling on the LCD display.
Syntax

lcd.noAutoscroll()

Parameters

lcd: variable of type LiquidCrystal.
Example

The sketch prints characters 0 through 9 with auto-scrolling off, then moves the cursor down and to the right, enables auto-scrolling, and prints those characters again.

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

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

void loop() {
  // place cursor at point (0,0):
  lcd.setCursor(0, 0);
  // print characters 0 through 9:
  for (int thisChar = 0; thisChar < 10; thisChar++) {
    lcd.print(thisChar);
    delay(500);
  }

  // place cursor at point (16,1):
  lcd.setCursor(16, 1);
  // enable automatic scrolling on the display:
  lcd.autoscroll();
  // print characters 0 through 9:
  for (int thisChar = 0; thisChar < 10; thisChar++) {
    lcd.print(thisChar);
    delay(500);
  }
  // turn off auto scrolling
  lcd.noAutoscroll();

  // clear screen for next loop:
  lcd.clear();
}

leftToRight ()
Sets the direction of text written on the LCD to left to right, which is the default. This means that subsequent characters written to the display go from left to right, but this does not affect the previously written text in any way.
Syntax

lcd.leftToRight()

Parameters

lcd: variable of type LiquidCrystal.
rightToLeft ()
Sets the direction of text recorded on the LCD to right to left (the default is left to right). This means that subsequent characters written to the display go from right to left, but this does not affect the previously written text in any way.
Syntax

lcd.rightToLeft()

Parameters

lcd: variable of type LiquidCrystal.

 


Find out about the update of this script first in our telegram channel: https://t.me/proweblabxyz