Introduction: Scroll Single LCD Line

The Liquid Crystal Library has two useful functions scrollDisplayLeft() and scrollDisplayRight(). These functions scroll the whole display. That is, they scroll both lines on a 1602 LCD and all four lines on a 2004 LCD. What we often need is the ability to scroll a single line onto these displays rather than scroll the entire display.

This Instructable provides two additional functions, scrollInFromRight (line to display text on, string to be scrolled) and scrollInFromLeft (line to display text on, string to be scrolled). These two functions which scroll lines into the LCD screen combined with the two functions, scrollLineRight (line to display text on, string to be scrolled) and scrollLineLeft (line to display text on, string to be scrolled) from my earlier Instructable, which presented functions to scroll lines off the screen, gives us several powerful ways to control how text can be presented on, or removed from, an LCD screen.

Step 1: What's Needed

- A 1602 or 2004 LCD standalone display, or LCD shield

-- See note below regarding use of a 2004 LCD display

- An Arduino UNO R3 or clone

- A USB cable to connect the Arduino to a computer

- A half-size, 400 tie points, breadboard

- The Arduino IDE

- An experimental platform (optional, but helpful)

The items required are an LCD screen either 1602 or 2004 [if a 2004 is used, it will work without problems to scroll from the right by changing the lcd.begin() function to reflect that you are now using a 20 character x 4 line display. [To scroll in from the left using a 2004 display, a code rewrite of the function scrollInFromLeft() is necessary]. In addition to an LCD you will need an Arduino UNO or clone, the Arduino IDE, and a USB cable to connect the Arduino to a computer.

An LCD shield can be used instead of the standalone LCD shown here. If that is the case, then the pin assignments for the LCD in the sketch below will need to be changed.

- For the independent 1602 LCD display I used the following pin assignments in my sketches:

// LiquidCrystal (rs, enable, d4, d5, d6, d7)

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

and included the Liquid Crystal library LiquidCrystal.h.

- For the LCD shield, I use the following pin assignments in my sketches, and also included the Liquid Crystal library LiquidCrystal.h.

// LiquidCrystal (rs, enable, d4, d5, d6, d7)

LiquidCrystal lcd(8, 13, 9, 4, 5, 6, 7);

Either route will run the code here, i.e., either a LCD shield or a standalone LCD. A 1602 standalone LCD display was used in this Instructable, but as noted a 1602 shield can be used as well if the different pin assignments are taken into account.

I used an “experimental platform” to connect the Arduino UNO to a half-size, 400 tie points , breadboard. (See an earlier Instructable of mine, “Experimental Platform For the Arduino UNO R3, How To Prepare It For Use “). However, an experimental platform is not needed, although for me it makes connecting the LCD to the UNO much easier and quicker.

The assignments I used for connecting the LCD to the UNO can be seen above.

Step 2: Hookup

The LCD is plugged into a breadboard and then hookup wires are connected from the breadboard to the appropriate pins on the Arduino (see step 2 if you have any questions on the connections I used).

I preferred a standalone LCD for this project rather than a shield as it was more satisfying for me, and allowed me to easily see which pins were available. It also allows me to use a potentiometer which has a knob, rather than a shield’s potentiometer which must be adjusted with a screwdriver.

The standalone LCD requires the use of a separate 10k ohm potentiometer. As noted above, I used one with a knob which has its wiper connected to the third LCD pin (counting from the right with the LCD pins facing you). The potentiometer is used to control the LCD’s contrast. The connections are the same for the 1602 and the 2004. However, the statement lcd.begin(16, 2) needs to be changed in the sketch to lcd.begin(20, 4) to show that our LCD has changed from a 16 character by two line display to a 20 character by four line one.

A look at the photographs attached shows the hookup I used, including the experimental platform, and 10k potentiometer.

Step 3: The Sketch

Just enter the attached sketch into the Arduino IDE. Please keep in mind that the Instructable site often removes all greater than and less than signs and the text between them. Thus, be sure and include the text, #include LiquidCrystal.h and enclose the words LiquidCrystal.h inside greater than and less than symbols.

// Sketch to scroll characters onto an LCD screen

#include //See note in text about what is needed here, i.e., LiquidCrystal.h enclosed inside

// greater thanand less than symbols

// This site often removes greater than and less than symbols and the text between them

// LiquidCrystal (rs, enable, d4, d5, d6, d7)

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

// Declare lcd as a LiquidCrystal Object

int i = 0;

int j = 0;

int k = 0;

int delayTime2 = 350; // Delay between shifts


void scrollInFromRight (int line, char str1[]) {

// Written by R. Jordan Kreindler June 2016

i = strlen(str1);

for (j = 16; j >= 0; j--) {

lcd.setCursor(0, line);

for (k = 0; k <= 15; k++) {

lcd.print(" "); // Clear line

}

lcd.setCursor(j, line);

lcd.print(str1);

delay(delayTime2);

}

}


void scrollInFromLeft (int line, char str1[]) {

// Written by R. Jordan Kreindler June 2016

i = 40 - strlen(str1);

line = line - 1;

for (j = i; j <= i + 16; j++) {

for (k = 0; k <= 15; k++) {

lcd.print(" "); // Clear line

}

lcd.setCursor(j, line);

lcd.print(str1);

delay(delayTime2);

}

}


void setup() {

Serial.begin(9600);

Serial.println("Starting test ...");

lcd.begin(16, 2);

lcd.clear();

lcd.print("Test Only");

}

void loop() {

lcd.clear();

scrollInFromRight(0, "Line1 From Right");

scrollInFromRight(1, "Line2 From Right");

lcd.clear();

scrollInFromLeft(0, "Line1 From Left.");

scrollInFromLeft(1, "Line2 From Left.");

lcd.clear();

scrollInFromRight(0, "Line1 From Right");

scrollInFromLeft(1, "Line2 From Left.");

lcd.clear();

}

The two functions: scrollInFromRight (line to display text on, string to be scrolled) and scrollInFromLeft (line to display text on, string to be scrolled) can be moved into your sketch to control the lines that get scrolled onto the LCD screen. These functions provide an elegant way to move new text to the screen.

When combined with the two functions in the sketch contained in the Instructable “Scroll a single LCD line out to left or right, How to” the four functions provide elegant ways to scroll text onto and off an LCD display. These functions allow you to scroll text one line at a time, and do not require that the whole display be scrolled as do the functions, scrollDisplayLeft() and scrollDisplayRight().

This scrolling ability allows us to present lines longer that the display is normally capable of showing. That is, for a 1602 display we are not restricted to only 16 characters per line (although only 16 will show at a time), and for a 2004 we are not restricted to 20 characters per line.

As an aside, you may want to adjust the display time between scrolls to match your needs.

Step 4: Afterwards

That's all there is to it. These functions and the two from my previous Instructable can be added to any sketch you have that uses an LCD and displays text. As noted, the ability to use longer lines is a definite benefit that is possible through the use of scrolling.

If you would like to contact me with any questions or for additional information, or to expand my knowledge in the area presented, I can be reached at transiintbox@gmail.com. (please replace the second 'i' with an 'e' to contact me.