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.
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).
Schematic
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 World3
4 Demonstrates the use a 16x2 LCD display. The LiquidCrystal5 library works with all LCD displays that are compatible with the6 Hitachi HD44780 driver. There are many of them out there, and you7 can usually tell them by the 16-pin interface.8
9 This sketch prints "Hello World!" to the LCD10 and shows the time.11
12 The circuit:13 * LCD RS pin to digital pin 1214 * LCD Enable pin to digital pin 1115 * LCD D4 pin to digital pin 516 * LCD D5 pin to digital pin 417 * LCD D6 pin to digital pin 318 * LCD D7 pin to digital pin 219 * LCD R/W pin to ground20 * LCD VSS pin to ground21 * LCD VCC pin to 5V22 * 10K resistor:23 * ends to +5V and ground24 * wiper to LCD VO pin (pin 3)25
26 Library originally added 18 Apr 200827 by David A. Mellis28 library modified 5 Jul 200929 by Limor Fried (http://www.ladyada.net)30 example added 9 Jul 200931 by Tom Igoe32 modified 22 Nov 201033 by Tom Igoe34 modified 7 Nov 201635 by Arturo Guadalupi36
37 This example code is in the public domain.38
39 https://docs.arduino.cc/learn/electronics/lcd-displays40
41*/42
43// include the library code:44#include <LiquidCrystal.h>45
46// initialize the library by associating any needed LCD interface pin47// with the arduino pin number it is connected to48const 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 160 // (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.
moves all the text one space to the left each time a letter is addedautoscroll()
turns scrolling offnoAutoscroll()
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 - Autoscroll4
5 Demonstrates the use a 16x2 LCD display. The LiquidCrystal6
7 library works with all LCD displays that are compatible with the8
9 Hitachi HD44780 driver. There are many of them out there, and you10
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 1220
21 * LCD Enable pin to digital pin 1122
23 * LCD D4 pin to digital pin 524
25 * LCD D5 pin to digital pin 426
27 * LCD D6 pin to digital pin 328
29 * LCD D7 pin to digital pin 230
31 * LCD R/W pin to ground32
33 * 10K resistor:34
35 * ends to +5V and ground36
37 * wiper to LCD VO pin (pin 3)38
39 Library originally added 18 Apr 200840
41 by David A. Mellis42
43 library modified 5 Jul 200944
45 by Limor Fried (http://www.ladyada.net)46
47 example added 9 Jul 200948
49 by Tom Igoe50
51 modified 22 Nov 201052
53 by Tom Igoe54
55 modified 7 Nov 201656
57 by Arturo Guadalupi58
59 This example code is in the public domain.60
61 http://www.arduino.cc/en/Tutorial/LiquidCrystalAutoscroll62
63*/64
65// include the library code:66#include <LiquidCrystal.h>67
68// initialize the library by associating any needed LCD interface pin69// with the arduino pin number it is connected to70
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 scrolling117
118 lcd.noAutoscroll();119
120 // clear screen for the next loop:121
122 lcd.clear();123}
Blink Example
This example sketch shows how to use the
blink()
and noBlink()
methods to blink a block-style cursor.1/*2
3 LiquidCrystal Library - Blink4
5 Demonstrates the use a 16x2 LCD display. The LiquidCrystal6
7 library works with all LCD displays that are compatible with the8
9 Hitachi HD44780 driver. There are many of them out there, and you10
11 can usually tell them by the 16-pin interface.12
13 This sketch prints "Hello World!" to the LCD and makes the14
15 cursor block blink.16
17 The circuit:18
19 * LCD RS pin to digital pin 1220
21 * LCD Enable pin to digital pin 1122
23 * LCD D4 pin to digital pin 524
25 * LCD D5 pin to digital pin 426
27 * LCD D6 pin to digital pin 328
29 * LCD D7 pin to digital pin 230
31 * LCD R/W pin to ground32
33 * 10K resistor:34
35 * ends to +5V and ground36
37 * wiper to LCD VO pin (pin 3)38
39 Library originally added 18 Apr 200840
41 by David A. Mellis42
43 library modified 5 Jul 200944
45 by Limor Fried (http://www.ladyada.net)46
47 example added 9 Jul 200948
49 by Tom Igoe50
51 modified 22 Nov 201052
53 by Tom Igoe54
55 modified 7 Nov 201656
57 by Arturo Guadalupi58
59 This example code is in the public domain.60
61 http://www.arduino.cc/en/Tutorial/LiquidCrystalBlink62
63*/64
65// include the library code:66#include <LiquidCrystal.h>67
68// initialize the library by associating any needed LCD interface pin69// with the arduino pin number it is connected to70
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 // Print a message to the LCD.82
83 lcd.print("hello, world!");84}85
86void loop() {87
88 // Turn off the blinking cursor:89
90 lcd.noBlink();91
92 delay(3000);93
94 // Turn on the blinking cursor:95
96 lcd.blink();97
98 delay(3000);99}
Cursor
This example sketch shows how to use the
cursor()
and noCursor()
methods to control an underscore-style cursor.1/*2
3 LiquidCrystal Library - Cursor4
5 Demonstrates the use a 16x2 LCD display. The LiquidCrystal6
7 library works with all LCD displays that are compatible with the8
9 Hitachi HD44780 driver. There are many of them out there, and you10
11 can usually tell them by the 16-pin interface.12
13 This sketch prints "Hello World!" to the LCD and14
15 uses the cursor() and noCursor() methods to turn16
17 on and off the cursor.18
19 The circuit:20
21 * LCD RS pin to digital pin 1222
23 * LCD Enable pin to digital pin 1124
25 * LCD D4 pin to digital pin 526
27 * LCD D5 pin to digital pin 428
29 * LCD D6 pin to digital pin 330
31 * LCD D7 pin to digital pin 232
33 * LCD R/W pin to ground34
35 * 10K resistor:36
37 * ends to +5V and ground38
39 * wiper to LCD VO pin (pin 3)40
41 Library originally added 18 Apr 200842
43 by David A. Mellis44
45 library modified 5 Jul 200946
47 by Limor Fried (http://www.ladyada.net)48
49 example added 9 Jul 200950
51 by Tom Igoe52
53 modified 22 Nov 201054
55 by Tom Igoe56
57 modified 7 Nov 201658
59 by Arturo Guadalupi60
61 This example code is in the public domain.62
63 http://www.arduino.cc/en/Tutorial/LiquidCrystalCursor64
65*/66
67// include the library code:68#include <LiquidCrystal.h>69
70// initialize the library by associating any needed LCD interface pin71// with the arduino pin number it is connected to72
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 LiquidCrystal5 library works with all LCD displays that are compatible with the6 Hitachi HD44780 driver. There are many of them out there, and you7 can usually tell them by the 16-pin interface.8
9 This sketch prints "Hello World!" to the LCD and uses the10 display() and noDisplay() functions to turn on and off11 the display.12
13 The circuit:14 * LCD RS pin to digital pin 1215 * LCD Enable pin to digital pin 1116 * LCD D4 pin to digital pin 517 * LCD D5 pin to digital pin 418 * LCD D6 pin to digital pin 319 * LCD D7 pin to digital pin 220 * LCD R/W pin to ground21 * 10K resistor:22 * ends to +5V and ground23 * wiper to LCD VO pin (pin 3)24
25 Library originally added 18 Apr 200826 by David A. Mellis27 library modified 5 Jul 200928 by Limor Fried (http://www.ladyada.net)29 example added 9 Jul 200930 by Tom Igoe31 modified 22 Nov 201032 by Tom Igoe33 modified 7 Nov 201634 by Arturo Guadalupi35
36 This example code is in the public domain.37
38 http://www.arduino.cc/en/Tutorial/LiquidCrystalDisplay39
40*/41
42// include the library code:43#include <LiquidCrystal.h>44
45// initialize the library by associating any needed LCD interface pin46// with the arduino pin number it is connected to47const 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 LiquidCrystal5 library works with all LCD displays that are compatible with the6 Hitachi HD44780 driver. There are many of them out there, and you7 can usually tell them by the 16-pin interface.8
9 This sketch prints "Hello World!" to the LCD and uses the10 scrollDisplayLeft() and scrollDisplayRight() methods to scroll11 the text.12
13 The circuit:14 * LCD RS pin to digital pin 1215 * LCD Enable pin to digital pin 1116 * LCD D4 pin to digital pin 517 * LCD D5 pin to digital pin 418 * LCD D6 pin to digital pin 319 * LCD D7 pin to digital pin 220 * LCD R/W pin to ground21 * 10K resistor:22 * ends to +5V and ground23 * wiper to LCD VO pin (pin 3)24
25 Library originally added 18 Apr 200826 by David A. Mellis27 library modified 5 Jul 200928 by Limor Fried (http://www.ladyada.net)29 example added 9 Jul 200930 by Tom Igoe31 modified 22 Nov 201032 by Tom Igoe33 modified 7 Nov 201634 by Arturo Guadalupi35
36 This example code is in the public domain.37
38 http://www.arduino.cc/en/Tutorial/LiquidCrystalScroll39
40*/41
42// include the library code:43#include <LiquidCrystal.h>44
45// initialize the library by associating any needed LCD interface pin46// with the arduino pin number it is connected to47const 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 left60 // 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 right69 // 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 left78 // 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 Input3
4 Demonstrates the use a 16x2 LCD display. The LiquidCrystal5 library works with all LCD displays that are compatible with the6 Hitachi HD44780 driver. There are many of them out there, and you7 can usually tell them by the 16-pin interface.8
9 This sketch displays text sent over the serial port10 (e.g. from the Serial Monitor) on an attached LCD.11
12 The circuit:13 * LCD RS pin to digital pin 1214 * LCD Enable pin to digital pin 1115 * LCD D4 pin to digital pin 516 * LCD D5 pin to digital pin 417 * LCD D6 pin to digital pin 318 * LCD D7 pin to digital pin 219 * LCD R/W pin to ground20 * 10K resistor:21 * ends to +5V and ground22 * wiper to LCD VO pin (pin 3)23
24 Library originally added 18 Apr 200825 by David A. Mellis26 library modified 5 Jul 200927 by Limor Fried (http://www.ladyada.net)28 example added 9 Jul 200929 by Tom Igoe30 modified 22 Nov 201031 by Tom Igoe32 modified 7 Nov 201633 by Arturo Guadalupi34
35 This example code is in the public domain.36
37 http://www.arduino.cc/en/Tutorial/LiquidCrystalSerialDisplay38
39*/40
41// include the library code:42#include <LiquidCrystal.h>43
44// initialize the library by associating any needed LCD interface pin45// with the arduino pin number it is connected to46const 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 arrive60 delay(100);61 // clear the screen62 lcd.clear();63 // read all the available characters64 while (Serial.available() > 0) {65 // display each character to the LCD66 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 left2lcd.setCursor(15, 0); // top right3lcd.setCursor(0, 1); // bottom left4lcd.setCursor(15, 1); // bottom right
Here is the full example:
1/*2
3 LiquidCrystal Library - setCursor4
5 Demonstrates the use a 16x2 LCD display. The LiquidCrystal6
7 library works with all LCD displays that are compatible with the8
9 Hitachi HD44780 driver. There are many of them out there, and you10
11 can usually tell them by the 16-pin interface.12
13 This sketch prints to all the positions of the LCD using the14
15 setCursor() method:16
17 The circuit:18
19 * LCD RS pin to digital pin 1220
21 * LCD Enable pin to digital pin 1122
23 * LCD D4 pin to digital pin 524
25 * LCD D5 pin to digital pin 426
27 * LCD D6 pin to digital pin 328
29 * LCD D7 pin to digital pin 230
31 * LCD R/W pin to ground32
33 * 10K resistor:34
35 * ends to +5V and ground36
37 * wiper to LCD VO pin (pin 3)38
39 Library originally added 18 Apr 200840
41 by David A. Mellis42
43 library modified 5 Jul 200944
45 by Limor Fried (http://www.ladyada.net)46
47 example added 9 Jul 200948
49 by Tom Igoe50
51 modified 22 Nov 201052
53 by Tom Igoe54
55 modified 7 Nov 201656
57 by Arturo Guadalupi58
59 This example code is in the public domain.60
61 http://www.arduino.cc/en/Tutorial/LiquidCrystalSetCursor62
63*/64
65// include the library code:66#include <LiquidCrystal.h>67
68// initialize the library by associating any needed LCD interface pin69// with the arduino pin number it is connected to70
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 of76// 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.
causes text to flow to the left from the cursor, as if the display is right-justified.rightToLeft()
causes text to flow to the right from the cursor, as if the display is left-justified.leftToRight()
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 - TextDirection4
5 Demonstrates the use a 16x2 LCD display. The LiquidCrystal6
7 library works with all LCD displays that are compatible with the8
9 Hitachi HD44780 driver. There are many of them out there, and you10
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 1220
21 * LCD Enable pin to digital pin 1122
23 * LCD D4 pin to digital pin 524
25 * LCD D5 pin to digital pin 426
27 * LCD D6 pin to digital pin 328
29 * LCD D7 pin to digital pin 230
31 * LCD R/W pin to ground32
33 * 10K resistor:34
35 * ends to +5V and ground36
37 * wiper to LCD VO pin (pin 3)38
39 Library originally added 18 Apr 200840
41 by David A. Mellis42
43 library modified 5 Jul 200944
45 by Limor Fried (http://www.ladyada.net)46
47 example added 9 Jul 200948
49 by Tom Igoe50
51 modified 22 Nov 201052
53 by Tom Igoe54
55 modified 7 Nov 201656
57 by Arturo Guadalupi58
59 This example code is in the public domain.60
61 http://www.arduino.cc/en/Tutorial/LiquidCrystalTextDirection62
63*/64
65// include the library code:66#include <LiquidCrystal.h>67
68// initialize the library by associating any needed LCD interface pin69// with the arduino pin number it is connected to70
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 letter95
96 lcd.rightToLeft();97
98 }99
100 // reverse again at 's':101
102 if (thisChar == 's') {103
104 // go left for the next letter105
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 0119
120 thisChar = 'a';121
122 }123
124 // print the character125
126 lcd.write(thisChar);127
128 // wait a second:129
130 delay(1000);131
132 // increment the letter:133
134 thisChar++;135}
Custom Character
This example demonstrates how to add custom characters on an LCD display.
Note that this example requires an additional potentiometer:
- Outer pins connected to 5V and GND.
- Inner pin (wiper) connected to A0.
This potentiometer controls the
delayTime
variable.1/*2 LiquidCrystal Library - Custom Characters3
4 Demonstrates how to add custom characters on an LCD display.5 The LiquidCrystal library works with all LCD displays that are6 compatible with the Hitachi HD44780 driver. There are many of7 them out there, and you can usually tell them by the 16-pin interface.8
9 This sketch prints "I <heart> Arduino!" and a little dancing man10 to the LCD.11
12 The circuit:13 * LCD RS pin to digital pin 1214 * LCD Enable pin to digital pin 1115 * LCD D4 pin to digital pin 516 * LCD D5 pin to digital pin 417 * LCD D6 pin to digital pin 318 * LCD D7 pin to digital pin 219 * LCD R/W pin to ground20 * 10K potentiometer:21 * ends to +5V and ground22 * wiper to LCD VO pin (pin 3)23 * 10K poterntiometer on pin A024
25 created 21 Mar 201126 by Tom Igoe27 modified 11 Nov 201328 by Scott Fitzgerald29 modified 7 Nov 201630 by Arturo Guadalupi31
32 Based on Adafruit's example at33 https://github.com/adafruit/SPI_VFD/blob/master/examples/createChar/createChar.pde34
35 This example code is in the public domain.36 https://docs.arduino.cc/learn/electronics/lcd-displays#custom-character37
38 Also useful:39 http://icontexto.com/charactercreator/40
41*/42
43// include the library code:44#include <LiquidCrystal.h>45
46// initialize the library by associating any needed LCD interface pin47// with the arduino pin number it is connected to48const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;49LiquidCrystal lcd(rs, en, d4, d5, d6, d7);50
51// make some custom characters:52byte heart[8] = {53 0b00000,54 0b01010,55 0b11111,56 0b11111,57 0b11111,58 0b01110,59 0b00100,60 0b0000061};62
63byte smiley[8] = {64 0b00000,65 0b00000,66 0b01010,67 0b00000,68 0b00000,69 0b10001,70 0b01110,71 0b0000072};73
74byte frownie[8] = {75 0b00000,76 0b00000,77 0b01010,78 0b00000,79 0b00000,80 0b00000,81 0b01110,82 0b1000183};84
85byte armsDown[8] = {86 0b00100,87 0b01010,88 0b00100,89 0b00100,90 0b01110,91 0b10101,92 0b00100,93 0b0101094};95
96byte armsUp[8] = {97 0b00100,98 0b01010,99 0b00100,100 0b10101,101 0b01110,102 0b00100,103 0b00100,104 0b01010105};106
107void setup() {108 // initialize LCD and set up the number of columns and rows:109 lcd.begin(16, 2);110
111 // create a new character112 lcd.createChar(0, heart);113 // create a new character114 lcd.createChar(1, smiley);115 // create a new character116 lcd.createChar(2, frownie);117 // create a new character118 lcd.createChar(3, armsDown);119 // create a new character120 lcd.createChar(4, armsUp);121
122 // set the cursor to the top left123 lcd.setCursor(0, 0);124
125 // Print a message to the lcd.126 lcd.print("I ");127 lcd.write(byte(0)); // when calling lcd.write() '0' must be cast as a byte128 lcd.print(" Arduino! ");129 lcd.write((byte)1);130
131}132
133void loop() {134 // read the potentiometer on A0:135 int sensorReading = analogRead(A0);136 // map the result to 200 - 1000:137 int delayTime = map(sensorReading, 0, 1023, 200, 1000);138 // set the cursor to the bottom row, 5th position:139 lcd.setCursor(4, 1);140 // draw the little man, arms down:141 lcd.write(3);142 delay(delayTime);143 lcd.setCursor(4, 1);144 // draw him arms up:145 lcd.write(4);146 delay(delayTime);147}
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.