Arduino Compatible Coding 12 - Scrolling Long Text On The Character LCD Using Arduino
Arduino Compatible Coding 12 - Scrolling Long Text On The Character LCD Using Arduino
In this tutorial, we will learn about scrolling long string of text on a character LCD, which can be
tricky. A character LCD module has a small amount of RAM that can store text and supports the
continuity of text scrolling across an LCD display. However, this requires a careful design of the
embedded program.
In this tutorial, we will flash a long string of text on a character LCD from Arduino and review the
different steps and concerns that should be considered.
Displaying long strings of text on a character LCD is far from straight forward. The reason for
this is that the display data RAM of character LCD modules is limited. For example, the display
data RAM (DDRAM) of a 16×2 character LCD can store only 80 characters. In a two-line mode,
the DDRAM can only store 40 characters per line. This means any attempt to store a text string
greater than 40 characters will override the other line.
If you want to display and scroll text greater than 40 characters on a line of 16×2 LCD, the only
way to do so is to break the original string into parts and write those parts of the text string to the
DDRAM one by one, scrolling each part of the text string.
Another concern is maintaining continuity of the text that’s scrolling through the LCD. If only 40
characters per line can be written to an LCD module at one time, then only 16 characters can be
displayed on the LCD at a time. To maintain the continuity of text scrolling across the LCD,
some characters from the previous text segment need to be repeated in the next text segment
that’s written to the DDRAM.
For example, on a 16×2 character LCD, if the text is scrolled one character per step, 15
characters from the current text segment will need to be repeated in the next text segment that’s
written to the DDRAM. For other LCD modules, one less the number of characters per line
needs to be repeated in the text segments. The size of the text segment that can be written to
the DDRAM at a time depends on the number of characters per line that can be stored in the
DDRAM of that LCD module.
So, let’s suppose the following text string has to be scrolled on a line of character LCD: “Quick
brown fox jumps over the lazy dog Quick brown fox jumps over the lazy dog Quick brown fox
jumps over the lazy dog“
The above string has 120 characters. Since only 40 characters can be written to the DDRAM at
one time, 15 characters must be repeated from the previous text segment to maintain the
continuity of text on a 16×2 LCD.
This means the above string must be broken into following strings to scroll across a line of 16×2
LCD:
To display and scroll the above-given text string of 120 characters on a 16×2 LCD, it’s
necessary to break it into five text segments of 40-character lengths (except for the last one) —
with 15 (16-1) characters repeating in each consecutive segment. This is because each line
displays 16 characters at a time while scrolling one character at a time.
The text segments from 1 to 4 have to be scrolled 24 (40-16) times as 40 characters are written
to the DDRAM at a time. These characters are scrolled on a 16-character line while the last text
segment should be scrolled however many times is the length of the segment in characters —
plus additional padding (spaces) between each repetition of the scroll.
If you’re not adding any padding between the repeated text scroll, the text segment “5” needs to
be scrolled 21 times (the length of the segment), or at least five times (21-16) as per choice.
The LiquidCrystal library of Arduino has a number of functions that can be used to scroll text on
a character LCD.
1. scrollDisplayLeft()
2. scrollDisplayRight()
3. autoscroll()
4. noAutoscroll()
5. leftToRight()
6. rightToLeft()
The print() function is used to write textual data to the DDRAM of the character LCD. For
scrolling a long text on a character LCD, the text can be broken up, as explained above, and
each segment can be written to the DDRAM using the print() function.
After writing each segment, it can be scrolled with the help of any of the above-listed functions.
It should be noted, however, that for scrolling text on the LCD, S/C, and RL bits of the “cursor or
display shift,” the LCD command is set. Visit the previous tutorial to learn about LCD
commands.
The scrollDisplayLeft() method
This function scrolls the content from the display (the text and cursor) one space to the left. It
needs no arguments. It simply scrolls the text written to the DDRAM by one space from the
current DDRAM address (as in the address counter of the LCD).
This function uses a “command() function,” which is used to send the LCD commands to the
character LCD.
This function scrolls the content of the display (the text and cursor) one space to the right. It
needs no arguments. It simply scrolls the text written to the DDRAM by one space from the
current DDRAM address (in the address counter of the LCD).
This function sets the direction of the text on a character LCD from left-to-right. It is the default
direction of text. After calling this function, when the characters are written on the DDRAM of
LCD, the characters will display from left to right. This function does not affect any previously
displayed text. It only affects the text that’s passed to the LCD (via print or write function) after
calling it.
This function sets the direction of the text on a character LCD from right-to-left. The default
direction of the text is from left-to-right. After calling this function, when characters are written on
the DDRAM of LCD, the characters will display from right to left. This function does not affect
any previously displayed text. It only affects the text that’s passed to the LCD (via print or write
function) after calling it.
This function serves to auto scroll the text of the LCD. The default direction of the text is from
left to right. The direction of scrolling the text can be set using the leftToRight() function or the
rightToLeft() function.
On calling this function, the text is scrolled from left-to-right or right-to-left by one space,
according to the direction of text. Additionally, this function scrolls the text that’s written in the
DDRAM. It does not alter the content of the DDRAM.
This function turns off the automatic scrolling of the text on the LCD.
All of the functions used to scroll the display of the character LCD use the command() method.
This method is used to pass the LCD commands to the LCD module.
This method also uses another method — send() — to pass commands to the LCD. The
command() method is used to pass commands to the LCD while the write() method is used to
pass data to the LCD module (to write in the DDRAM).
The command(), write() and send() methods have these source codes:
The recipe
In this recipe, we will scroll long text of strings on a 16×2 character LCD. Two different strings
will be scrolled on two lines of the 16×2 LCD.
One of the strings will scroll on the top line of the character LCD and repeatedly display,
“EEWORLDONLINE.” The second string will scroll on the bottom line of the 16×2 LCD. It
displays all of the websites of the EE Network with WTWH Media LLC.
Components required
1. Arduino UNO x1
2. 16×2 character LCD x1
3. 10K Pot x1
4. 330 Ohms Resistor or any low-value resistor x1
5. Breadboard x1
6. Male-to-Male Jumper Wires or Connecting Wires
Circuit connections
The LCD module used in this project is JHD162A. This is a 16×2 LCD module with 5×8
character dots. The LCD module has a 16-pin interface. The LCD is interfaced with Arduino in a
4-bit mode. The pin 1 (GND) and 16 (LED) of the LCD module are connected to ground. Pin 2
(VCC) is connected to the VCC.
The pin 15 (LED+) of the LCD module is connected to the VCC via a small-value resistor. Pin 3
(VEE) is connected to the variable terminal of a pot while the fixed terminals of the pot are
connected to ground and VCC.
The R/W pin is connected to ground because Arduino will only write data to the LCD module.
The RS, EN, DB4, DB5, DB6, and DB7 pins of the LCD are connected to pins 13, 11, 7, 6, 5,
and 4 of Arduino UNO, respectively. The breadboard is supplied to the common ground and the
5V supply rail from one of the ground pins and the 5V pin of Arduino UNO.
Circuit diagram
Arduino Sketch
1. #include <LiquidCrystal.h>
2. `
3. //LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
4. LiquidCrystal lcd(13, 11, 7, 6, 5, 4);
5. `
6. byte c1[8]={B00000,B01010,B00000,B00000,B10001,B01110,B00000,}; //Smile-1
7. byte c2[8]={B00000,B01010,B00100,B00100,B00000,B01110,B10001,}; //Smile-2
8. byte c3[8]={B00100,B01010,B10001,B10001,B01010,B00100,}; //Diamond
9. byte c4[8]={B01110,B01010,B11111,B11011,B11111,B01010,B01110,}; //Brick
10. byte c5[8]={B01010,B00100,B00100,B01010,B10001,B00100,B10001,}; //Hour-glass
11. byte c6[8]={B00100,B01010,B11111,B01010,B10101,B11011,B10001,}; //Hut
12. byte c7[8]={B11111,B10001,B10001,B10001,B10001,B10001,B10001,B11111,}; //Rectangle
13. byte c8[8]={B11111,B11101,B11011,B11101,B11111,B10000,B10000,B10000,}; //Flag-1
14. `
15. byte c9[8]={B00100,B01110,B11111,B00100,B00100,B00100,B00100,B00100,}; //Up-Arrow
16. byte c10[8]={B00100,B00100,B00100,B00100,B00100,B11111,B01110,B00100,}; //Down-Arrow
17. byte c11[8]={B00000,B00000,B01010,B10101,B10001,B01010,B00100,B00000,}; //Blank-Heart
18. byte c12[8]={B00000,B00000,B01010,B11111,B11111,B01110,B00100,B00000,}; //Full-Heart
19. byte c13[8]={B00000,B01010,B00000,B00000,B01110,B10001,B00000,}; //Smile-Sad
20. byte c14[8]={B00100,B00100,B00100,B00100,B00100,B00100,B00100,B00100}; //Pole
21. byte c15[8]={B00000,B00000,B00000,B00100,B00000,B00000,B00000,B00000}; //Dot
22. byte c16[8]={B11111,B10001,B10001,B10001,B11111,B00001,B00001,B00001}; //Flag-2
23. `
24. void setup()
25. {
26. lcd.begin(16, 2);
27. lcd.clear();
28. }
29. `
30. void loop()
31. {
32. lcd.createChar(0 , c1); //Creating custom characters in CG-RAM
33. lcd.createChar(1 , c2);
34. lcd.createChar(2 , c3);
35. lcd.createChar(3 , c4);
36. lcd.createChar(4 , c5);
37. lcd.createChar(5 , c6);
38. lcd.createChar(6 , c7);
39. lcd.createChar(7 , c8);
40. lcd.setCursor(0 ,0);
41. lcd.print(char(0));
42. lcd.setCursor(1 ,0);
43. lcd.print(char(1));
44. lcd.setCursor(2 ,0);
45. lcd.print(char(2));
46. lcd.setCursor(3 ,0);
47. lcd.print(char(3));
48. lcd.setCursor(4 ,0);
49. lcd.print(char(4));
50. lcd.setCursor(5 ,0);
51. lcd.print(char(5));
52. lcd.setCursor(6 ,0);
53. lcd.print(char(6));
54. lcd.setCursor(7 ,0);
55. lcd.print(char(7));
56. delay(2000);
57. lcd.clear();
58. lcd.createChar(8 , c9);
59. lcd.createChar(9 , c10);
60. lcd.createChar(10 , c11);
61. lcd.createChar(11 , c12);
62. lcd.createChar(12 , c13);
63. lcd.createChar(13 , c14);
64. lcd.createChar(14 , c15);
65. lcd.createChar(15 , c16);
66. lcd.setCursor(0 ,0);
67. lcd.print(char(8));
68. lcd.setCursor(1 ,0);
69. lcd.print(char(9));
70. lcd.setCursor(2 ,0);
71. lcd.print(char(10));
72. lcd.setCursor(3 ,0);
73. lcd.print(char(11));
74. lcd.setCursor(4 ,0);
75. lcd.print(char(12));
76. lcd.setCursor(5 ,0);
77. lcd.print(char(13));
78. lcd.setCursor(6 ,0);
79. lcd.print(char(14));
80. lcd.setCursor(7 ,0);
81. lcd.print(char(15));
82. delay(2000);
83. lcd.clear();
84. }
#include <LiquidCrystal.h>
void setup()
{
lcd.begin(16, 2);
lcd.clear();
}
void loop()
{
lcd.createChar(0 , c1); //Creating custom characters in CG-RAM
lcd.createChar(1 , c2);
lcd.createChar(2 , c3);
lcd.createChar(3 , c4);
lcd.createChar(4 , c5);
lcd.createChar(5 , c6);
lcd.createChar(6 , c7);
lcd.createChar(7 , c8);
lcd.setCursor(0 ,0);
lcd.print(char(0));
lcd.setCursor(1 ,0);
lcd.print(char(1));
lcd.setCursor(2 ,0);
lcd.print(char(2));
lcd.setCursor(3 ,0);
lcd.print(char(3));
lcd.setCursor(4 ,0);
lcd.print(char(4));
lcd.setCursor(5 ,0);
lcd.print(char(5));
lcd.setCursor(6 ,0);
lcd.print(char(6));
lcd.setCursor(7 ,0);
lcd.print(char(7));
delay(2000);
lcd.clear();
lcd.createChar(8 , c9);
lcd.createChar(9 , c10);
lcd.createChar(10 , c11);
lcd.createChar(11 , c12);
lcd.createChar(12 , c13);
lcd.createChar(13 , c14);
lcd.createChar(14 , c15);
lcd.createChar(15 , c16);
lcd.setCursor(0 ,0);
lcd.print(char(8));
lcd.setCursor(1 ,0);
lcd.print(char(9));
lcd.setCursor(2 ,0);
lcd.print(char(10));
lcd.setCursor(3 ,0);
lcd.print(char(11));
lcd.setCursor(4 ,0);
lcd.print(char(12));
lcd.setCursor(5 ,0);
lcd.print(char(13));
lcd.setCursor(6 ,0);
lcd.print(char(14));
lcd.setCursor(7 ,0);
lcd.print(char(15));
delay(2000);
lcd.clear();
}
view raw13-LCD-Custom-Characters.ino
The goal is to display two strings on a 16×2 LCD module. One of the strings just displays the text
“EEWORLDONLINE” on line 0 of the LCD. While scrolling on the display, this text must also scroll
continuously on line 0. The text is repeated in a long string with two spaces between each repetition of
the text.
The second string contains the names of websites from the EE Network with WTWH Media
LLC, which include:
1. eeworldonline.com
2. edaboard.com
3. electro-tech-online.com
4. engineersgarage.com
5. analogictips.com
6. connectortips.com
7. microcontrollertips.com
8. powerelectronictips.com
9. sensortips.com
10. testandmeasurementtips.com
11. wireandcabletips.com
12. designfast.com
13. 5gtechnologyworld.com
The second string is 269 characters long and contains this text:
“eeworldonline.com edaboard.com electro-tech-
online.com engineersgarage.com analogictips.com connectortips.com microcontrollertips.com
powerelectronictips.com sensortips.com testandmeasurementtips.com wireandcabletips.com
designfast.com 5gtechnologyworld.com”
To display the scrolling text on line 0, the following text segments are forwarded to the character
LCD:
After the third line segment, the string segments 1 to 3 begin repeating to enable the repeating
text display, “EEWORLDONLINE.” All of the above three text-segments are 40-characters long
and need to be scrolled 24 times (40-16) on a line of the 16×2 LCD.
To display the scrolling text on line 1, the 269-characters string is broken into following 40-
character text segments”
1. “eeworldonline.com engineersgarage.com ”
2. “ersgarage.com edaboard.com electro-tec”
3. “om electro-tech-online.com analogictip”
4. “om analogictips.com connectortips.com ”
5. “nectortips.com microcontrollertips.com ”
6. “rollertips.com powerelectronictips.com ”
7. “ronictips.com sensortips.com testandme”
8. “.com testandmeasurementtips.com wirean”
9. “ips.com wireandcabletips.com designfas”
10. “.com designfast.com 5gtechnologyworld”
11. “technologyworld.com”
All of the text segments are 40-characters long except the last one because the maximum
capacity of the LCD’s DDRAM is 40 characters per line. Each subsequent text segment starts
by repeating the last 15 characters of the previous text segment to ensure continuity of the
scrolling text on the display.
A total of 15 characters are repeated because each line of the display has 16 characters and on
scrolling by one space, the last 15 characters must match with the previous text segment
whenever new data is written to the DDRAM. Each text segment is scrolled 24 times (40-16) as
there are 40 characters stored on the DDRAM at one time and 16 characters are displayed on a
line at a time on the LCD. The last line is scrolled according to the number of characters in it.
This means it is scrolled less than 24 times.
The result is a scrolling display on 16×2 LCD that highlights EEWORLDONLINE on line 0 and
all of the websites that are a part of the EE network on line 1.
Programming guide
The Arduino sketch starts importing the LiquidCrystal library. Then, an object defined by the
variable “lcd” is defined of the LiquidCrystal class.
#include <LiquidCrystal.h>
//LiquidCrystal lcd(RS, E, D4, D5, D6, D7);
LiquidCrystal lcd(13, 11, 7, 6, 5, 4);
A global variable “counter” is defined to scroll the text and the setup() function is defined. In
the setup() function, the LCD is initialized to the 16×2 size by using the begin() method as
follows:
void setup()
{
lcd.begin(16, 2);
}
In the loop() function, the LCD display is first cleared using the clear() method, and the cursor is
set at column 0 of the line 0, using the setCursor() method.
Next, a for loop is run in which the entire display is shifted left by using the scrollDisplayLeft()
method 24 times. After scrolling the display on the LCD, a delay of 300 milliseconds each time is
provided using the delay() function.
void loop()
{
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(” EEWORLDONLINE EEWORLDONLINE EEWORLDO”);
lcd.setCursor(0, 1);
lcd.print(“eeworldonline.com engineersgarage.com “);
for(counter = 0; counter <24; counter++)
{
lcd.scrollDisplayLeft();
delay(300);
}
Similarly, the other text segments are displayed and scrolled. On line 0, three text segments are
repeated continuously until the last trimmed text segment is displayed. On line 1, 11 text
segments are displayed and scrolled. The last text segment on both lines is scrolled five times,
according to the length of the text segments.
To keep things simple, the last text segments on lines 0 and 1 are also kept to the same length.
The body of the loop() function keeps repeating itself until Arduino is shutdown. Therefore, both
long strings keep scrolling on lines 0 and 1 of the LCD module.
Arduino lacks support for C-style string manipulation functions. Otherwise, the sketch could be
reduced to a few lines by storing the entire string to a variable and then trimming the string in
each step for passing to the DDRAM of the LCD module. Even if the standard C’s string library
might have been supported, the use of the string manipulation function would have increased
the footprint of the embedded program.
In the next tutorial, we will discuss how to display custom characters on the character LCD.