In the previous tutorial, we discussed:
- The functional blocks of a character LCD
- How character LCD works and is interfaced with a controller
- How to display text on a character LCD
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 text
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:
1: “Quick brown fox jumps over the lazy dog ”
2: “r the lazy dog Quick brown fox jumps ove”
3: “n fox jumps over the lazy dog Quick brow”
4: ” dog Quick brown fox jumps over the lazy”
5: “s over the lazy dog ”
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.
Scrolling long text
The LiquidCrystal library of Arduino has a number of functions that can be used to scroll text on a character LCD.
These methods are useful in scrolling text across an LCD display:
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 has this syntax:
lcd.scrollDisplayLeft() // lcd is an object of LiquidCrystal Class
Additionally, it has this source code:
This function uses a “command() function,” which is used to send the LCD commands to the character LCD.
The scrollDisplayRight() method
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 has this syntax:
lcd.scrollDisplayRight() //lcd is an object of LiquidCrystal Class
Additionally, it has this source code:
The leftToRight() method
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.
It has this syntax:
lcd.leftToRight() //lcd is an object of LiquidCrystal Class
This is the source code:
The rightToLeft() method
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.
It has this syntax:
lcd.rightToLeft() //lcd is an object of LiquidCrystal Class
This is the source code:
The autoscroll() method
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.
It has this syntax:
lcd.autoscroll() //lcd is an object of LiquidCrystal Class
This is the source code:
The noAutoscroll() method
This function turns off the automatic scrolling of the text on the LCD.
It has this syntax:
lcd.noAutoscroll() //lcd is an object of LiquidCrystal Class
This is the source code:
The command(), write() and send() methods
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.
Arduino Sketch
How the project works
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:
- 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
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:
1. ” EEWORLDONLINE EEWORLDONLINE EEWORLDO”
2. “NLINE EEWORLDONLINE EEWORLDONLINE EEW”
3. “ORLDONLINE EEWORLDONLINE EEWORLDONLINE”
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.
The text ” EEWORLDONLINE EEWORLDONLINE EEWORLDO” is printed using the print() method on the “lcd” object. Similarly, the text “”eeworldonline.com engineersgarage.com ” is printed at column 0 of line 1.
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.
Demonstration video
You may also like:
Filed Under: Arduino Projects, Tutorials
Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.
Tell Us What You Think!!
You must be logged in to post a comment.