LCD - Text wraps to wrong lines

I have the HD44780 20 x 4 LCD that I got from Hacktronics (20 x 4 LCD White on Blue).

When I output a really long string of text using lcd.print(), the text wrap goes from the 1st line to the 3rd, then the 2nd, and finally to the 4th. If I manually set the line using lcd.setCursor(), they print in the correct order.

I have my Arduino Duemilanove wired to the LCD using the setup at Arduino Character LCD Tutorial.

The code I've attached below shows an example of what's happing. I initially print using setCursor() to position the text on specific lines. This displays:

Hello, World
hacktronics.com
Row 3
Row 4

It then delays for 15 seconds and then displays:

Why doesn't word wra
goes from line 1 to
p work correctly it
3 to 2 then 4
#include <LiquidCrystal.h>

// Connections:
// rs (LCD pin 4) to Arduino pin 12
// rw (LCD pin 5) to Arduino pin 11
// enable (LCD pin 6) to Arduino pin 10
// LCD pin 15 to Arduino pin 13
// LCD pins d4(11), d5(12), d6(13), d7(14) to Arduino pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

int backLight = 13;    // pin 13 will control the backlight

void setup()
{
  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
  lcd.begin(20,4);
  lcd.clear();                  // start with a blank screen
  lcd.setCursor(0,0);           // set cursor to column 0, row 0 (the first row)
  lcd.print("Hello, World");    // change this text to whatever you like. keep it clean.
  lcd.setCursor(0,1);           // set cursor to column 0, row 1
  lcd.print("hacktronics.com");
  
  // if you have a 4 row LCD, uncomment out these lines to write to the bottom rows
  lcd.setCursor(0,2);         // set cursor to column 0, row 2
  lcd.print("Row 3");
  lcd.setCursor(0,3);         // set cursor to column 0, row 3
  lcd.print("Row 4");
  lcd.setCursor(7,2);
  delay(15000);
  lcd.clear();
  lcd.setCursor(0,0);
  lcd.print("Why doesn't word wrap work correctly it goes from line 1 to 3 to 2 then 4");
  
}

void loop()
{
}

try delaying each line...? im still learning myself looks like its delaying every line at once and thats why possibly getting the clutter

Well.. I had the same question.. I thought it was an error.. turns out, that's just how the HD47780 and compatible chips work.

To get each line to display correctly, you're going to need to use setCursor for each piece of information you want to display.. there's no easy way to make it scroll the correct way:/

Brady D:

I have written an explanation of the unusual (but normal) line wrapping that you are experiencing. You can find it at http://web.alfredstate.edu/weimandn. Use the LCD Addressing link.

Don

florest

Thanks for the excellent article describing the issue.

Shouldn't it be possible for the LiquidCrystal library to account for this since you are telling it the display size with the lcd.begin() statement?

I wouldn't say it's "not possible" but you would have to buffer everything that comes into the LCD, and then measure the length of the message, then have the setCursor decide where to put it.

When you're using the lcd.begin(20, 4); all you're enabling is the extra rows. ( I think.. which is weird.. because if you use lcd.begin(16, 2) with a 20x4 LCD, you can still use each line with the setCursor.. and the scrolling acts exactly the same.)

I also bought the LCD from hacktronics.com

This is the standard behavior for these character LCDs. I wrote some code that will wrap the characters correctly. I don't have my LCD hooked up right now to test this code again, but I think it worked the last time I tried it. :-?

Try it out.

#include <LiquidCrystal.h>

// Connections:
// rs (LCD pin 4) to Arduino pin 12
// rw (LCD pin 5) to Arduino pin 11
// enable (LCD pin 6) to Arduino pin 10
// LCD pin 15 to Arduino pin 13
// LCD pins d4, d5, d6, d7 to Arduino pins 5, 4, 3, 2
LiquidCrystal lcd(12, 11, 10, 5, 4, 3, 2);

int backLight = 13;    // pin 13 will control the backlight
char stringIn[] =  "111111111111111111112222222222222222222233333333333333333333444444444444444444h";

void setup()
{
  
  pinMode(backLight, OUTPUT);
  digitalWrite(backLight, HIGH); // turn backlight on. Replace 'HIGH' with 'LOW' to turn it off.
  lcd.clear();                  // start with a blank screen
  lcd.setCursor(0,0);           // set cursor to column 0, row 0 (the first row)
  lcd.print("Hello, World");    // change this text to whatever you like. keep it clean.
  lcd.setCursor(0,1);           // set cursor to column 0, row 1
  lcd.print("hacktronics.com");
  
  // if you have a 4 row LCD, uncomment out these lines to write to the bottom rows
  //lcd.setCursor(0,2);         // set cursor to column 0, row 2
  //lcd.print("Row 3");
  //lcd.setCursor(0,3);         // set cursor to column 0, row 3
  //lcd.print("Row 4");
}

void stringToLCD() {
    int lineCount = 0;
    int lineNumber = 0;
    byte stillProcessing = 1;
    byte charCount = 1;
    lcd.clear();
    lcd.setCursor(0,0);

    while(stillProcessing) {
         if (++lineCount > 20) {    // have we printed 20 characters yet (+1 for the logic)
              lineNumber += 1;
              lcd.setCursor(0,lineNumber);   // move cursor down
              lineCount = 1;
         }

         lcd.print(stringIn[charCount - 1]);

         if (!stringIn[charCount]) {   // no more chars to process?
              stillProcessing = 0;
         }
         charCount += 1;
    }
}

void loop()
{
  delay(3000);
  stringToLCD();

  
}