Scroll a Single LCD Line in From Left or Right

19,785

60

10

Introduction: Scroll a Single LCD Line in From Left or Right

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.

Share

    Recommendations

    • Creative Misuse Contest

      Creative Misuse Contest
    • Water Contest

      Water Contest
    • Fix It! Contest

      Fix It! Contest

    10 Discussions

    And what if you have text longer than 16 symbols, that will just stop at the first position.

    2 replies

    The 1602 display buffers text beyond the visible 16 characters displayed on the screen, although not initially presented on the display. That is, there are character positions (buffers) available past those displayed that can be written to and read from.

    Hey just tried editing your code to allow a longer character message and it doesnt work. It will only display for the message 16 characters long and everything else gets cut off. how might I fix this?

    I believe I understand what you would like to do. I will take a look at what you sent and get back to you.

    The very best

    1 more answer

    I looked at your code and it appears you do not have anything after the include statement. In my Instructable paper I noted:

    #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


    Thus, if you replace the include statement you have, you should have no issue. That is, use

    #include <LiquidCrystal.h>

    instead of the #include you have that includes no text.

    The very best, sorry if there was some confusion. However, the Instructable editor appears to remove all text between < and > signs, as well as the "greater than" and "less than" signs themselves.

    Jordan

    Hello, I want place my word in row 1 as scrolling text, and row 2 as fixed text, can you explain how about it?

    i use this code, but i wrong.






    /*
    * MONSTER ARDUINO V2 : BASIC SENSOR
    * Program Mengontrol Lampu Backlight LCD
    * www.ElangSakti.com
    */

    // library LCD dengan modul I2C
    #include


    // jumlah kolom dan baris LCD (16x2)
    #define LCD_COL 16
    #define LCD_ROW 2

    // ===== Konfigurasi LCD ===========

    // LCD1602 dengan Modul I2C
    // Alamat I2C di 0x27
    // lcd_Addr, EN, RW, RS, D4, D5, D6, D7, PIN_BACKLIGHT, Pol
    LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);



    int i = 0;

    int j = 0;

    int k = 0;

    int delayTime2 = 500; // Delay between shifts



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


    i = strlen(str1);

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

    lcd.setCursor(j, line);



    lcd.print(str1);; // Clear line
    delay(delayTime2);
    }



    }






    void setup() {

    // Nyalakan Backlight
    lcd.setBacklight(BACKLIGHT_ON);
    lcd.begin(16, 2);
    Serial.begin(9600);



    lcd.clear();



    }

    void loop() {
    {
    scrollInFromRight(1, "DAFTAR PENGHUNI KOS KALISEGARA UNNES: ");
    }
    {

    lcd.clear();
    lcd.setCursor(0,1);
    lcd.print("TONI WIDIYANTO");
    delay(1000);
    lcd.setCursor(0,1);
    lcd.print("JUNANTO ");
    delay(1000);
    lcd.setCursor(0,1);
    lcd.print("ARIS TRIYONO ");
    delay(1000);
    lcd.setCursor(0,1);
    lcd.print("M. TEGUH A. ");
    delay(1000);
    lcd.setCursor(0,1);
    lcd.print("KUSWORO ");
    delay(1000);
    }
    }

    Nice Instructable! It will be great having the ability to move the display one line at a time.

    For learning the 16 pin connector works fine but for projects we like the I2C versions and they only cost about $2 more. The I2C uses only 2 data pins and makes wiring very easy. Well worth it if putting the display in a project box.

    FYI- For inserting code use the 'code' option under formatting. It is a little quirky to use but you don't lose any characters. You can see it on our Instructables.

    1 reply

    Thank you for your kind comments.

    I have not needed to include my displays in project boxes, yet, and find the ability to “plug” LCDs into a breadboard stabilizes them if the appropriate “support feet” are used. See an earlier Instructable of mine, “Stabilize and level a 1602 or 2004 breadboard-mounted LCD display, A simple way to”.

    I agree with your thoughts on I2C as it does allow many more pins on an Arduino
    to be used when necessary. I also appreciate your comments on posting code. Thanks.

    The very best.