LcD leading 0 at start of top 2 lines


Why do I get 0 on the top 2 line
Many thanks

Probably because your sketch writes it there, but we cannot see the image that you tried to post and you did not post your sketch.

I fixed the broken markup on the embedded image in post #1 so that it is now visible.

How do you load the stretch please

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

Please post your full sketch, using code tags when you do

Posting your code using code tags prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

The zeros are part of the values in lines 3 and 4. Your output there is too long and has more than 20 characters.

To better explain @Kai-R post, a 4x20 LCD has its internal buffer arranged so that the lines of the display are in the order 1 > 3 > 2 > 4, so printing past the end of line 3 wraps around to line 2, and printing past the end of line 4 wraps around to line 1. Note that those line numbers are the "human" representation, most LCD libraries number the lines 0 through 3.

To avoid problems like this (@david_2018 explained it) now and in the future, there is quite an elegant approach — because you never know what text length may appear later :slightly_smiling_face:


Depending on what you use:

If you use the String class, something like this would work:

String rightLength(String s, uint8_t len) {
  if (s.length() > len) {
    s.remove(len);
  }
  return s;
}

Usage:

display.print(rightLength(text, 20));




If you use classic char arrays / C strings instead, use something like this:

void printLimited(Print &display, const char *text, uint8_t len) {
  char buf[len + 1];

  strncpy(buf, text, len);
  buf[len] = '\0';

  display.print(buf);
}

Usage:

printLimited(display, text, 20);

//...with whatever you call your display object.

//Here it is for example:
//LiquidCrystal_I2C display(0x27, 20, 4);

I can’t understand but the problem has gone away. The only thing I have changed is the USB power cord. I noticed that touching the old power cord caused the display to flicker

I doubt that the problem has actually been fixed; rather, it’s just been masked. The “glitch” you described can’t really be caused by the USB power cord.

The problem is that a value of -127.00 is too long to fit on the line of the LCD display, so the last 0 is printed as the first character of another line.

Looks like you are reading temperature sensors, a value of -127.00 generally indicates an error while reading the sensor. It's possible a bad USB cable was causing noise or instability in the Vcc line, corrupting the data, or it could be that some other intermittent connection was moved enough when you changed the USB cable that it is now making good contact.

Therefore, as already suggested here, steps should be taken to ensure that the maximum line length for the display is not exceeded during output.

That can often be avoided quite easily. Consider this: