Liquid Crystal Displays (LCD) with Arduino

Find out how to wire an LCD to an Arduino, and how to use the LiquidCrystal library through a set of useful examples.

This article was revised on 2021/11/18 by Karl Söderby.

The LiquidCrystal library allows you to control LCD displays that are compatible with the Hitachi HD44780 driver. There are many of them out there, and you can usually tell them by the 16-pin interface.

Output of the sketch on a 16x2 LCD
Output of the sketch on a 16x2 LCD

The LCDs have a parallel interface, meaning that the microcontroller has to manipulate several interface pins at once to control the display. The interface consists of the following pins:

  • A register select (RS) pin that controls where in the LCD's memory you're writing data to. You can select either the data register, which holds what goes on the screen, or an instruction register, which is where the LCD's controller looks for instructions on what to do next.
  • A Read/Write (R/W) pin that selects reading mode or writing mode
  • An Enable pin that enables writing to the registers
  • 8 data pins (D0 -D7). The states of these pins (high or low) are the bits that you're writing to a register when you write, or the values you're reading when you read.

There's also a display contrast pin (Vo), power supply pins (+5V and GND) and LED Backlight (Bklt+ and BKlt-) pins that you can use to power the LCD, control the display contrast, and turn on and off the LED backlight, respectively.

The process of controlling the display involves putting the data that form the image of what you want to display into the data registers, then putting instructions in the instruction register. The LiquidCrystal Library simplifies this for you so you don't need to know the low-level instructions.

The Hitachi-compatible LCDs can be controlled in two modes: 4-bit or 8-bit. The 4-bit mode requires seven I/O pins from the Arduino, while the 8-bit mode requires 11 pins. For displaying text on the screen, you can do most everything in 4-bit mode, so example shows how to control a 16x2 LCD in 4-bit mode.

Hardware Required

  • Arduino Board
  • LCD Screen (compatible with Hitachi HD44780 driver)
  • pin headers to solder to the LCD display pins
  • 10k ohm potentiometer
  • 220 ohm resistor
  • hook-up wires
  • breadboard

Circuit

Note that this circuit was originally designed for the Arduino UNO. As the Arduino is communicating with the display using SPI, pin 11 & 12 will change depending on what board you are using. For example, on a MKR WiFi 1010, the SPI bus is attached to pin 8 & 11.

Before wiring the LCD screen to your Arduino board we suggest to solder a pin header strip to the 14 (or 16) pin count connector of the LCD screen, as you can see in the image further up.

To wire your LCD screen to your board, connect the following pins:

  • LCD RS pin to digital pin 12
  • LCD Enable pin to digital pin 11
  • LCD D4 pin to digital pin 5
  • LCD D5 pin to digital pin 4
  • LCD D6 pin to digital pin 3
  • LCD D7 pin to digital pin 2
  • LCD R/W pin to GND
  • LCD VSS pin to GND
  • LCD VCC pin to 5V
  • LCD LED+ to 5V through a 220 ohm resistor
  • LCD LED- to GND

Additionally, wire a 10k potentiometer to +5V and GND, with it's wiper (output) to LCD screens VO pin (pin3).

The circuit (made using Fritzing).
The circuit (made using Fritzing).

Schematic

The schematic (made using Fritzing).
The schematic (made using Fritzing).

Hello World Example

This example sketch prints

Hello World!
to the LCD and shows the time in seconds since the Arduino was reset.

1/*
2 LiquidCrystal Library - Hello World
3
4 Demonstrates the use a 16x2 LCD display. The LiquidCrystal
5 library works with all LCD displays that are compatible with the
6 Hitachi HD44780 driver. There are many of them out there, and you
7 can usually tell them by the 16-pin interface.
8
9 This sketch prints "Hello World!" to the LCD
10 and shows the time.
11
12 The circuit:
13 * LCD RS pin to digital pin 12
14 * LCD Enable pin to digital pin 11
15 * LCD D4 pin to digital pin 5
16 * LCD D5 pin to digital pin 4
17 * LCD D6 pin to digital pin 3
18 * LCD D7 pin to digital pin 2
19 * LCD R/W pin to ground
20 * LCD VSS pin to ground
21 * LCD VCC pin to 5V
22 * 10K resistor:
23 * ends to +5V and ground
24 * wiper to LCD VO pin (pin 3)
25
26 Library originally added 18 Apr 2008
27 by David A. Mellis
28 library modified 5 Jul 2009
29 by Limor Fried (http://www.ladyada.net)
30 example added 9 Jul 2009
31 by Tom Igoe
32 modified 22 Nov 2010
33 by Tom Igoe
34 modified 7 Nov 2016
35 by Arturo Guadalupi
36
37 This example code is in the public domain.
38
39 http://www.arduino.cc/en/Tutorial/LiquidCrystalHelloWorld
40
41*/
42
43// include the library code:
44#include <LiquidCrystal.h>
45
46// initialize the library by associating any needed LCD interface pin
47// with the arduino pin number it is connected to
48const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
49LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
50
51void setup() {
52 // set up the LCD's number of columns and rows:
53 lcd.begin(16, 2);
54 // Print a message to the LCD.
55 lcd.print("hello, world!");
56}
57
58void loop() {
59 // set the cursor to column 0, line 1
60 // (note: line 1 is the second row, since counting begins with 0):
61 lcd.setCursor(0, 1);
62 // print the number of seconds since reset:
63 lcd.print(millis() / 1000);
64}

Autoscroll Example

This example sketch shows how to use the

autoscroll()
and
noAutoscroll()
methods to move all the text on the display left or right.

  • autoscroll()
    moves all the text one space to the left each time a letter is added
  • noAutoscroll()
    turns scrolling off

This sketch prints the characters

0
to
9
with autoscroll off, then moves the cursor to the bottom right, turns autoscroll on, and prints them again.

1/*
2
3 LiquidCrystal Library - Autoscroll
4
5 Demonstrates the use a 16x2 LCD display. The LiquidCrystal
6
7 library works with all LCD displays that are compatible with the
8
9 Hitachi HD44780 driver. There are many of them out there, and you
10
11 can usually tell them by the 16-pin interface.
12
13 This sketch demonstrates the use of the autoscroll()
14
15 and noAutoscroll() functions to make new text scroll or not.
16
17 The circuit:
18
19 * LCD RS pin to digital pin 12
20
21 * LCD Enable pin to digital pin 11
22
23 * LCD D4 pin to digital pin 5
24
25 * LCD D5 pin to digital pin 4
26
27 * LCD D6 pin to digital pin 3
28
29 * LCD D7 pin to digital pin 2
30
31 * LCD R/W pin to ground
32
33 * 10K resistor:
34
35 * ends to +5V and ground
36
37 * wiper to LCD VO pin (pin 3)
38
39 Library originally added 18 Apr 2008
40
41 by David A. Mellis
42
43 library modified 5 Jul 2009
44
45 by Limor Fried (http://www.ladyada.net)
46
47 example added 9 Jul 2009
48
49 by Tom Igoe
50
51 modified 22 Nov 2010
52
53 by Tom Igoe
54
55 modified 7 Nov 2016
56
57 by Arturo Guadalupi
58
59 This example code is in the public domain.
60
61 http://www.arduino.cc/en/Tutorial/LiquidCrystalAutoscroll
62
63*/
64
65// include the library code:
66#include <LiquidCrystal.h>
67
68// initialize the library by associating any needed LCD interface pin
69// with the arduino pin number it is connected to
70
71const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
72
73LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
74
75void setup() {
76
77 // set up the LCD's number of columns and rows:
78
79 lcd.begin(16, 2);
80}
81
82void loop() {
83
84 // set the cursor to (0,0):
85
86 lcd.setCursor(0, 0);
87
88 // print from 0 to 9:
89
90 for (int thisChar = 0; thisChar < 10; thisChar++) {
91
92 lcd.print(thisChar);
93
94 delay(500);
95
96 }
97
98 // set the cursor to (16,1):
99
100 lcd.setCursor(16, 1);
101
102 // set the display to automatically scroll:
103
104 lcd.autoscroll();
105
106 // print from 0 to 9:
107
108 for (int thisChar = 0; thisChar < 10; thisChar++) {
109
110 lcd.print(thisChar);
111
112 delay(500);
113
114 }
115
116 // turn off automatic scrolling
117
118 lcd.noAutoscroll();
119
120 // clear screen for the next loop:
121
122 lcd.clear();
123}
124```s
125
126### Blink Example
127
128This example sketch shows how to use the `blink()` and `noBlink()` methods to blink a block-style cursor.
129
130```arduino
131/*
132
133 LiquidCrystal Library - Blink
134
135 Demonstrates the use a 16x2 LCD display. The LiquidCrystal
136
137 library works with all LCD displays that are compatible with the
138
139 Hitachi HD44780 driver. There are many of them out there, and you
140
141 can usually tell them by the 16-pin interface.
142
143 This sketch prints "Hello World!" to the LCD and makes the
144
145 cursor block blink.
146
147 The circuit:
148
149 * LCD RS pin to digital pin 12
150
151 * LCD Enable pin to digital pin 11
152
153 * LCD D4 pin to digital pin 5
154
155 * LCD D5 pin to digital pin 4
156
157 * LCD D6 pin to digital pin 3
158
159 * LCD D7 pin to digital pin 2
160
161 * LCD R/W pin to ground
162
163 * 10K resistor:
164
165 * ends to +5V and ground
166
167 * wiper to LCD VO pin (pin 3)
168
169 Library originally added 18 Apr 2008
170
171 by David A. Mellis
172
173 library modified 5 Jul 2009
174
175 by Limor Fried (http://www.ladyada.net)
176
177 example added 9 Jul 2009
178
179 by Tom Igoe
180
181 modified 22 Nov 2010
182
183 by Tom Igoe
184
185 modified 7 Nov 2016
186
187 by Arturo Guadalupi
188
189 This example code is in the public domain.
190
191 http://www.arduino.cc/en/Tutorial/LiquidCrystalBlink
192
193*/
194
195// include the library code:
196#include <LiquidCrystal.h>
197
198// initialize the library by associating any needed LCD interface pin
199// with the arduino pin number it is connected to
200
201const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
202
203LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
204
205void setup() {
206
207 // set up the LCD's number of columns and rows:
208
209 lcd.begin(16, 2);
210
211 // Print a message to the LCD.
212
213 lcd.print("hello, world!");
214}
215
216void loop() {
217
218 // Turn off the blinking cursor:
219
220 lcd.noBlink();
221
222 delay(3000);
223
224 // Turn on the blinking cursor:
225
226 lcd.blink();
227
228 delay(3000);
229}

Cursor

This example sketch shows how to use the

cursor()
and
noCursor()
methods to control an underscore-style cursor.

1/*
2
3 LiquidCrystal Library - Cursor
4
5 Demonstrates the use a 16x2 LCD display. The LiquidCrystal
6
7 library works with all LCD displays that are compatible with the
8
9 Hitachi HD44780 driver. There are many of them out there, and you
10
11 can usually tell them by the 16-pin interface.
12
13 This sketch prints "Hello World!" to the LCD and
14
15 uses the cursor() and noCursor() methods to turn
16
17 on and off the cursor.
18
19 The circuit:
20
21 * LCD RS pin to digital pin 12
22
23 * LCD Enable pin to digital pin 11
24
25 * LCD D4 pin to digital pin 5
26
27 * LCD D5 pin to digital pin 4
28
29 * LCD D6 pin to digital pin 3
30
31 * LCD D7 pin to digital pin 2
32
33 * LCD R/W pin to ground
34
35 * 10K resistor:
36
37 * ends to +5V and ground
38
39 * wiper to LCD VO pin (pin 3)
40
41 Library originally added 18 Apr 2008
42
43 by David A. Mellis
44
45 library modified 5 Jul 2009
46
47 by Limor Fried (http://www.ladyada.net)
48
49 example added 9 Jul 2009
50
51 by Tom Igoe
52
53 modified 22 Nov 2010
54
55 by Tom Igoe
56
57 modified 7 Nov 2016
58
59 by Arturo Guadalupi
60
61 This example code is in the public domain.
62
63 http://www.arduino.cc/en/Tutorial/LiquidCrystalCursor
64
65*/
66
67// include the library code:
68#include <LiquidCrystal.h>
69
70// initialize the library by associating any needed LCD interface pin
71// with the arduino pin number it is connected to
72
73const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
74
75LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
76
77void setup() {
78
79 // set up the LCD's number of columns and rows:
80
81 lcd.begin(16, 2);
82
83 // Print a message to the LCD.
84
85 lcd.print("hello, world!");
86}
87
88void loop() {
89
90 // Turn off the cursor:
91
92 lcd.noCursor();
93
94 delay(500);
95
96 // Turn on the cursor:
97
98 lcd.cursor();
99
100 delay(500);
101}

Display Example

This example sketch shows how to use the

display()
and
noDisplay()
methods to turn on and off the display. The text to be displayed will still be preserved when you use noDisplay() so it's a quick way to blank the display without losing everything on it.

1/*
2 LiquidCrystal Library - display() and noDisplay()
3
4 Demonstrates the use a 16x2 LCD display. The LiquidCrystal
5 library works with all LCD displays that are compatible with the
6 Hitachi HD44780 driver. There are many of them out there, and you
7 can usually tell them by the 16-pin interface.
8
9 This sketch prints "Hello World!" to the LCD and uses the
10 display() and noDisplay() functions to turn on and off
11 the display.
12
13 The circuit:
14 * LCD RS pin to digital pin 12
15 * LCD Enable pin to digital pin 11
16 * LCD D4 pin to digital pin 5
17 * LCD D5 pin to digital pin 4
18 * LCD D6 pin to digital pin 3
19 * LCD D7 pin to digital pin 2
20 * LCD R/W pin to ground
21 * 10K resistor:
22 * ends to +5V and ground
23 * wiper to LCD VO pin (pin 3)
24
25 Library originally added 18 Apr 2008
26 by David A. Mellis
27 library modified 5 Jul 2009
28 by Limor Fried (http://www.ladyada.net)
29 example added 9 Jul 2009
30 by Tom Igoe
31 modified 22 Nov 2010
32 by Tom Igoe
33 modified 7 Nov 2016
34 by Arturo Guadalupi
35
36 This example code is in the public domain.
37
38 http://www.arduino.cc/en/Tutorial/LiquidCrystalDisplay
39
40*/
41
42// include the library code:
43#include <LiquidCrystal.h>
44
45// initialize the library by associating any needed LCD interface pin
46// with the arduino pin number it is connected to
47const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
48LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
49
50void setup() {
51 // set up the LCD's number of columns and rows:
52 lcd.begin(16, 2);
53 // Print a message to the LCD.
54 lcd.print("hello, world!");
55}
56
57void loop() {
58 // Turn off the display:
59 lcd.noDisplay();
60 delay(500);
61 // Turn on the display:
62 lcd.display();
63 delay(500);
64}

Scroll Example

This example sketch shows how to use the

scrollDisplayLeft()
and
scrollDisplayRight()
methods to reverse the direction the text is flowing. It prints "Hello World!", scrolls it offscreen to the left, then offscreen to the right, then back to home.

1/*
2 LiquidCrystal Library - scrollDisplayLeft() and scrollDisplayRight()
3
4 Demonstrates the use a 16x2 LCD display. The LiquidCrystal
5 library works with all LCD displays that are compatible with the
6 Hitachi HD44780 driver. There are many of them out there, and you
7 can usually tell them by the 16-pin interface.
8
9 This sketch prints "Hello World!" to the LCD and uses the
10 scrollDisplayLeft() and scrollDisplayRight() methods to scroll
11 the text.
12
13 The circuit:
14 * LCD RS pin to digital pin 12
15 * LCD Enable pin to digital pin 11
16 * LCD D4 pin to digital pin 5
17 * LCD D5 pin to digital pin 4
18 * LCD D6 pin to digital pin 3
19 * LCD D7 pin to digital pin 2
20 * LCD R/W pin to ground
21 * 10K resistor:
22 * ends to +5V and ground
23 * wiper to LCD VO pin (pin 3)
24
25 Library originally added 18 Apr 2008
26 by David A. Mellis
27 library modified 5 Jul 2009
28 by Limor Fried (http://www.ladyada.net)
29 example added 9 Jul 2009
30 by Tom Igoe
31 modified 22 Nov 2010
32 by Tom Igoe
33 modified 7 Nov 2016
34 by Arturo Guadalupi
35
36 This example code is in the public domain.
37
38 http://www.arduino.cc/en/Tutorial/LiquidCrystalScroll
39
40*/
41
42// include the library code:
43#include <LiquidCrystal.h>
44
45// initialize the library by associating any needed LCD interface pin
46// with the arduino pin number it is connected to
47const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
48LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
49
50void setup() {
51 // set up the LCD's number of columns and rows:
52 lcd.begin(16, 2);
53 // Print a message to the LCD.
54 lcd.print("hello, world!");
55 delay(1000);
56}
57
58void loop() {
59 // scroll 13 positions (string length) to the left
60 // to move it offscreen left:
61 for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
62 // scroll one position left:
63 lcd.scrollDisplayLeft();
64 // wait a bit:
65 delay(150);
66 }
67
68 // scroll 29 positions (string length + display length) to the right
69 // to move it offscreen right:
70 for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
71 // scroll one position right:
72 lcd.scrollDisplayRight();
73 // wait a bit:
74 delay(150);
75 }
76
77 // scroll 16 positions (display length + string length) to the left
78 // to move it back to center:
79 for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
80 // scroll one position left:
81 lcd.scrollDisplayLeft();
82 // wait a bit:
83 delay(150);
84 }
85
86 // delay at the end of the full loop:
87 delay(1000);
88
89}

Serial to Display Example

This example sketch accepts serial input from a host computer and displays it on the LCD. To use it, upload the sketch, then open the Serial Monitor and type some characters and click Send. The text will appear on your LCD.

1/*
2 LiquidCrystal Library - Serial Input
3
4 Demonstrates the use a 16x2 LCD display. The LiquidCrystal
5 library works with all LCD displays that are compatible with the
6 Hitachi HD44780 driver. There are many of them out there, and you
7 can usually tell them by the 16-pin interface.
8
9 This sketch displays text sent over the serial port
10 (e.g. from the Serial Monitor) on an attached LCD.
11
12 The circuit:
13 * LCD RS pin to digital pin 12
14 * LCD Enable pin to digital pin 11
15 * LCD D4 pin to digital pin 5
16 * LCD D5 pin to digital pin 4
17 * LCD D6 pin to digital pin 3
18 * LCD D7 pin to digital pin 2
19 * LCD R/W pin to ground
20 * 10K resistor:
21 * ends to +5V and ground
22 * wiper to LCD VO pin (pin 3)
23
24 Library originally added 18 Apr 2008
25 by David A. Mellis
26 library modified 5 Jul 2009
27 by Limor Fried (http://www.ladyada.net)
28 example added 9 Jul 2009
29 by Tom Igoe
30 modified 22 Nov 2010
31 by Tom Igoe
32 modified 7 Nov 2016
33 by Arturo Guadalupi
34
35 This example code is in the public domain.
36
37 http://www.arduino.cc/en/Tutorial/LiquidCrystalSerialDisplay
38
39*/
40
41// include the library code:
42#include <LiquidCrystal.h>
43
44// initialize the library by associating any needed LCD interface pin
45// with the arduino pin number it is connected to
46const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
47LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
48
49void setup() {
50 // set up the LCD's number of columns and rows:
51 lcd.begin(16, 2);
52 // initialize the serial communications:
53 Serial.begin(9600);
54}
55
56void loop() {
57 // when characters arrive over the serial port...
58 if (Serial.available()) {
59 // wait a bit for the entire message to arrive
60 delay(100);
61 // clear the screen
62 lcd.clear();
63 // read all the available characters
64 while (Serial.available() > 0) {
65 // display each character to the LCD
66 lcd.write(Serial.read());
67 }
68 }
69}

Set Cursor Example

This example sketch shows how to use the

setCursor()
method to reposition the cursor. To move the cursor, just call
setCursor()
with a row and column position. For example, for a 2x16 display:

1lcd.setCursor(0, 0); // top left
2lcd.setCursor(15, 0); // top right
3lcd.setCursor(0, 1); // bottom left
4lcd.setCursor(15, 1); // bottom right

Here is the full example:

1/*
2
3 LiquidCrystal Library - setCursor
4
5 Demonstrates the use a 16x2 LCD display. The LiquidCrystal
6
7 library works with all LCD displays that are compatible with the
8
9 Hitachi HD44780 driver. There are many of them out there, and you
10
11 can usually tell them by the 16-pin interface.
12
13 This sketch prints to all the positions of the LCD using the
14
15 setCursor() method:
16
17 The circuit:
18
19 * LCD RS pin to digital pin 12
20
21 * LCD Enable pin to digital pin 11
22
23 * LCD D4 pin to digital pin 5
24
25 * LCD D5 pin to digital pin 4
26
27 * LCD D6 pin to digital pin 3
28
29 * LCD D7 pin to digital pin 2
30
31 * LCD R/W pin to ground
32
33 * 10K resistor:
34
35 * ends to +5V and ground
36
37 * wiper to LCD VO pin (pin 3)
38
39 Library originally added 18 Apr 2008
40
41 by David A. Mellis
42
43 library modified 5 Jul 2009
44
45 by Limor Fried (http://www.ladyada.net)
46
47 example added 9 Jul 2009
48
49 by Tom Igoe
50
51 modified 22 Nov 2010
52
53 by Tom Igoe
54
55 modified 7 Nov 2016
56
57 by Arturo Guadalupi
58
59 This example code is in the public domain.
60
61 http://www.arduino.cc/en/Tutorial/LiquidCrystalSetCursor
62
63*/
64
65// include the library code:
66#include <LiquidCrystal.h>
67
68// initialize the library by associating any needed LCD interface pin
69// with the arduino pin number it is connected to
70
71const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
72
73LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
74
75// these constants won't change. But you can change the size of
76// your LCD using them:
77
78const int numRows = 2;
79
80const int numCols = 16;
81
82void setup() {
83
84 // set up the LCD's number of columns and rows:
85
86 lcd.begin(numCols, numRows);
87}
88
89void loop() {
90
91 // loop from ASCII 'a' to ASCII 'z':
92
93 for (int thisLetter = 'a'; thisLetter <= 'z'; thisLetter++) {
94
95 // loop over the columns:
96
97 for (int thisRow = 0; thisRow < numRows; thisRow++) {
98
99 // loop over the rows:
100
101 for (int thisCol = 0; thisCol < numCols; thisCol++) {
102
103 // set the cursor position:
104
105 lcd.setCursor(thisCol, thisRow);
106
107 // print the letter:
108
109 lcd.write(thisLetter);
110
111 delay(200);
112
113 }
114
115 }
116
117 }
118}

Text Direction Example

This example sketch shows how to use the

leftToRight()
and
rightToLeft()
methods. These methods control which way text flows from the cursor.

  • rightToLeft()
    causes text to flow to the left from the cursor, as if the display is right-justified.
  • leftToRight()
    causes text to flow to the right from the cursor, as if the display is left-justified.

This sketch prints

a
through
l
right to left, then
m
through
r
left to right, then
s
through
z
right to left again.

1/*
2
3 LiquidCrystal Library - TextDirection
4
5 Demonstrates the use a 16x2 LCD display. The LiquidCrystal
6
7 library works with all LCD displays that are compatible with the
8
9 Hitachi HD44780 driver. There are many of them out there, and you
10
11 can usually tell them by the 16-pin interface.
12
13 This sketch demonstrates how to use leftToRight() and rightToLeft()
14
15 to move the cursor.
16
17 The circuit:
18
19 * LCD RS pin to digital pin 12
20
21 * LCD Enable pin to digital pin 11
22
23 * LCD D4 pin to digital pin 5
24
25 * LCD D5 pin to digital pin 4
26
27 * LCD D6 pin to digital pin 3
28
29 * LCD D7 pin to digital pin 2
30
31 * LCD R/W pin to ground
32
33 * 10K resistor:
34
35 * ends to +5V and ground
36
37 * wiper to LCD VO pin (pin 3)
38
39 Library originally added 18 Apr 2008
40
41 by David A. Mellis
42
43 library modified 5 Jul 2009
44
45 by Limor Fried (http://www.ladyada.net)
46
47 example added 9 Jul 2009
48
49 by Tom Igoe
50
51 modified 22 Nov 2010
52
53 by Tom Igoe
54
55 modified 7 Nov 2016
56
57 by Arturo Guadalupi
58
59 This example code is in the public domain.
60
61 http://www.arduino.cc/en/Tutorial/LiquidCrystalTextDirection
62
63*/
64
65// include the library code:
66#include <LiquidCrystal.h>
67
68// initialize the library by associating any needed LCD interface pin
69// with the arduino pin number it is connected to
70
71const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
72
73LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
74
75int thisChar = 'a';
76
77void setup() {
78
79 // set up the LCD's number of columns and rows:
80
81 lcd.begin(16, 2);
82
83 // turn on the cursor:
84
85 lcd.cursor();
86}
87
88void loop() {
89
90 // reverse directions at 'm':
91
92 if (thisChar == 'm') {
93
94 // go right for the next letter
95
96 lcd.rightToLeft();
97
98 }
99
100 // reverse again at 's':
101
102 if (thisChar == 's') {
103
104 // go left for the next letter
105
106 lcd.leftToRight();
107
108 }
109
110 // reset at 'z':
111
112 if (thisChar > 'z') {
113
114 // go to (0,0):
115
116 lcd.home();
117
118 // start again at 0
119
120 thisChar = 'a';
121
122 }
123
124 // print the character
125
126 lcd.write(thisChar);
127
128 // wait a second:
129
130 delay(1000);
131
132 // increment the letter:
133
134 thisChar++;
135}

Contribute to Arduino

Join the community and suggest improvements to this article via GitHub. Make sure to read out contribution policy before making your pull request.

Missing something?

Check out our store and get what you need to follow this tutorial.