I2C LCD garbles text

I have a 16x2 LCD (from DFRobot) that I'm driving with I2C connection. It had been working fine for a long time but as my project has gotten bigger, it has started printing garbled text. I haven't been able to reproduce this with a small program. I'm wondering if there is a memory problem. Here is a snipet of the code. I call this routine to display a line of text on the LCD. There are some debugging serial.print commands as well.

void LCDLine1(String LCDText)
{
  Serial.print("receiving: ");
  Serial.print(LCDText);
  Serial.print(" in LCDLine1, len=");
  Serial.println(LCDText.length());
  lcd.clear();
  lcd.backlight();
  lcd.print(LCDText);
  Serial.println(LCDText);
}

This works most of the time but a few calls randomly print jiberish on the LCD. Sometimes, the entire program crashes and restarts. Here is one result showing that I'm passing a valid String object to the routine ("Tilt axis order"). It is received properly. But after the lcd.print line, the String is garbled.

monitor output:

...
receiving: Tilt axis order in LCDLine1, len=15
Tilt axis (jiberish follows but doesn't show up here)

Probably related, one of the variables I read from the EEPROM gets randomly changed even though I don't have any code to change it. It is used as a constant.

I'm using Arduino Uno board and a 16x2 LCD from DFRobot (http://www.dfrobot.com/index.php?route=product/product&path=53&product_id=135). It is driven from my USB cable. I have two motors but they aren't drawing power when the LCD freaks out. The project is 23428 bytes (of 32256 maximum).

I'd have a look at
http://www.arduino.cc/playground/Code/AvailableMemory

you might need to switch to a mega or teensy++ etc.

Sounds like an out-of-memory issue to me.
Maybe you can find some solutions here:

The PROGMEM is especially useful since I sense that you are using lots of strings.

So what gets stored in those 32256 bytes? Does that memory have to hold the program, all variables, and EEPROM bytes? Is any reserved for the OS or is that someplace different? Or maybe there isn't really an operating system?

This will be a standalone project so no computer memory will be available. I presume storing things in Flash is only aplicable if you are connected to a computer.

This will explain :slight_smile:

The arduino doesn't not use computer memory at all.

It looks like PROGMEM will be useful for freeing up some memory.

But if I call my LCD printing routine with a fixed string like

LCDLine1("print me first");
LCDLine1("print me second");

LCDLine1(String str) {
  lcd.print(str);
}

how is this handled in memory?

Is the fixed string "print me first" overwritten in the function with "print me second" so that I only use up 15 characters for memory?

No. Every string is stored in both FLASH and SRAM. I think I was pretty clear in that blog post.

Thanks for the blog posts liudr. In addition to reading http://arduino.cc/forum/index.php/topic,78244.15.html I have successfully moved my strings to PROGMEM and have some free space now. No more garbled LCD messages.

Alright! Just don't get too excited with newly reclaimed SRAM and risk it all on String classes. Using String classes sucks on 2K SRAM. I've seen plenty troubles from using the String class.

1 Like