# 20x4 I2C LCD crashing on Spark Core

**URL:** <https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631>\
**Category:** Troubleshooting\
**Created:** [December 6, 2014, 3:32pm UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631 "2014-12-06T15:32:23Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![qwerty009](https://avatars.discourse-cdn.com/v4/letter/q/97f17d/32.png) [@qwerty009](https://community.particle.io/u/qwerty009)\
**Post date:** [December 6, 2014, 3:32pm UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/1 "2014-12-06T15:32:23Z")

</div>

I’m currently trying to build central heating controller for my house but i ran into bit of trouble regarding LCD.  
This is the library i used: [https://gist.github.com/anonymous/dc7ecc5e6574105b4fbd](https://gist.github.com/anonymous/dc7ecc5e6574105b4fbd)

Everything is working fine but after certain time has passed LCD locks up and displays some random chars.  
If i then run lcd-\>clear(); in code then it comes back and works until next crash.

Probably smth related to library? Can someone fix it or point me towards some better library?

LCD is use is : [http://www.dx.com/p/arduino-iic-i2c-serial-3-2-lcd-2004-module-display-138611#.VIMhTsmwiBU](http://www.dx.com/p/arduino-iic-i2c-serial-3-2-lcd-2004-module-display-138611#.VIMhTsmwiBU)

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 6, 2014, 3:45pm UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/2 "2014-12-06T15:45:51Z")

</div>

Could you please show your own code too? Or are you experiancing this problem with the provided `LiquidCrystal_I2C_example.ino`?

The description you provided would also fit to some buffer overflow.

---

<div class="post-metadata">

**Author:** ![qwerty009](https://avatars.discourse-cdn.com/v4/letter/q/97f17d/32.png) [@qwerty009](https://community.particle.io/u/qwerty009)\
**Post date:** [December 6, 2014, 5:05pm UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/3 "2014-12-06T17:05:56Z")

</div>

It also happens when i use example.ino  
If i increase loop delay LCD works normally longer. In my code i have delay(1000);

I have 2’nd LCD too and it has same problem (to rule out hardware issue).

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 7, 2014, 9:34am UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/4 "2014-12-07T09:34:48Z")

</div>

Since I can’t test this lib without one of these displays, I can only guess and throw some further ideas at you.

Have you got a way to check if it’s an electrical issue (e.g. missing I2C pull-up resistors, 3.3V not enough, …)?

I’ve used the SPI\_LCD lib successfully. Do you see any chance that you “port” this for I2C yourself?

We can also tag @BDub as he did some research on a similar display  
[http://community.spark.io/t/liquidcrystal-shift-register/2179/32](http://community.spark.io/t/liquidcrystal-shift-register/2179/32)

Some more reads here (if you haven’t already been there)

> **[Using an I2C / SPI LCD on the Spark Core](https://community.particle.io/t/using-an-i2c-spi-lcd-on-the-spark-core/2116)**
>
> Hey guys! So, here I am loving my Spark but I have to admit I am more hardware than software. Thus my love of what is being done here with the Spark Core… but I am running into some issues - specifically with I2C at the moment. The situation: A...

> **[LCD i2c (Lib+Info+CustomCharacters)](https://community.particle.io/t/lcd-i2c-lib-info-customcharacters/4670)**
>
> Hitchhikers Guide to run a i2c 16x2 LiquidCrystalDisplay . First, for all, and forever: All starships “Heart of Gold” goes to @sej7278: He helped me out and without him I were totally lost. What do I need Challenge (to escape the earth)...

---

<div class="post-metadata">

**Author:** ![qwerty009](https://avatars.discourse-cdn.com/v4/letter/q/97f17d/32.png) [@qwerty009](https://community.particle.io/u/qwerty009)\
**Post date:** [December 7, 2014, 7:23pm UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/5 "2014-12-07T19:23:25Z")

</div>

I use 5V to power LCD and i have 4,7k resistors in place.  
I have to use lcd-\>init(); (in first post i wrote clear(); it was typo) for lcd to display properly again once corrupt letters appear.

Unfortunetly there is no way i can port or modify library since my programming skills are weak at best.

My curreny code (sketch to test sensors and lcd):

```
// This #include statement was automatically added by the Spark IDE.
#include "OneWire/OneWire.h"

// This #include statement was automatically added by the Spark IDE.
#include "spark-dallas-temperature/spark-dallas-temperature.h"

//#include "application.h"
#include "LiquidCrystal_I2C.h"
#include "SparkTime/SparkTime.h" 

DallasTemperature dallas(new OneWire(D3));

int led1 = D4;

LiquidCrystal_I2C *lcd;
UDP UDPClient;
SparkTime rtc;

unsigned long currentTime;
unsigned long lastTime = 0UL;
String timeStr;

void setup(){
    dallas.begin();
    rtc.begin(&UDPClient, "north-america.pool.ntp.org");
    rtc.setTimeZone(+2); // gmt offset
    Serial.begin(9600);
    lcd = new LiquidCrystal_I2C(0x27, 20, 4); // set the LCD address to 0x20 for a 16x2 //SparkCore bug, address is actually 27 but shifted (0x27*2)
    lcd->init(); // initialize the lcd
    lcd->backlight();
    lcd->clear();
    pinMode(led1, OUTPUT);
    }
    

void loop(){
 
    currentTime = rtc.now();
    if (currentTime != lastTime) {
        byte sec = rtc.second(currentTime);
        
         
        // Just the time in 12 hour format
        timeStr = "";
        timeStr += rtc.hour12String(currentTime);
        timeStr += ":";
        timeStr += rtc.minuteString(currentTime);
        timeStr += ":";
        timeStr += rtc.secondString(currentTime);    
        timeStr += " ";    
        timeStr += rtc.AMPMString(currentTime);
        
        lastTime = currentTime;
    }
    
    lcd->setCursor(5,0);
    lcd->print(timeStr);

    dallas.requestTemperatures();
    delay(1000);

    float Radikasse = dallas.getTempCByIndex( 0 );
    float Toatemp = dallas.getTempCByIndex( 1 );
    float Toatemp2 = dallas.getTempCByIndex( 2 );

    Serial.println("Temperature: "); Serial.println(Radikasse) ;Serial.println(Toatemp) ;
    lcd->setCursor(5,1);
    lcd->print(Radikasse, 1);
    lcd->setCursor(5,2);
    lcd->print(Toatemp, 1);  
    lcd->setCursor(5,3);
    lcd->print(Toatemp2, 1);
    
    if (Toatemp <23) {
        digitalWrite(led1, LOW);
    } else {
        lcd->init();
        //digitalWrite(led1,HIGH);
    }
    

}
```

---

<div class="post-metadata">

**Author:** ![qwerty009](https://avatars.discourse-cdn.com/v4/letter/q/97f17d/32.png) [@qwerty009](https://community.particle.io/u/qwerty009)\
**Post date:** [December 15, 2014, 2:06pm UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/7 "2014-12-15T14:06:51Z")

</div>

After playing around with it i can say how this text corruption acts:  
Some of my text corrupts and part of it remains intact and text in all the lines (my lcd has 4 lines) starts running from left to right.

Might it be some sort of memory overflow or what causes that sort of behaviour?  
Sorry for my bad English 🙂

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [December 15, 2014, 2:22pm UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/8 "2014-12-15T14:22:54Z")

</div>

@qwerty009, I suggest you add Serial.print() before any of your lcd-\>print commands to see what you are sending to the display. Second, what is the purpose of the lcd-\>init() in:

```
if (Toatemp <23) {
    digitalWrite(led1, LOW);
} else {
    lcd->init();
    //digitalWrite(led1,HIGH);
}

```

Did LiquidCrystal\_I2C\_example.ini work without any problems? Are there pull-up resistors on the serial board of the LCD? If not, did you add some in your circuit?

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 15, 2014, 2:23pm UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/9 "2014-12-15T14:23:57Z")

</div>

@qwerty009, it could well be that the source of the problem isn’t actually the LCD, but the `String`.  
The `String` class is known to have some memory issues, if not used with care.

The first thing to check would be to `Serial.print()` the string you want to display on your LCD.

The way you’re using `String` (a lot of frequent concatinations) causes the `String` class to permanently reallocate the string, which may - due to running out of heap space - cause the corruption of data.  
Maybe you should try `char[]` instead.

* * *

Darn, again I was tooooooooooooo slow - @peekay123 's response just popped up, while writing this

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 15, 2014, 2:30pm UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/10 "2014-12-15T14:30:04Z")

</div>

> [@peekay123](#):
>
> Did LiquidCrystal\_I2C\_example.ini work without any problems? Are there pull-up resistors on the serial board of the LCD? If not, did you add some in your circuit?

I did ask this already 😉

> [@ScruffR](#):
>
> Have you got a way to check if it's an electrical issue (e.g. missing I2C pull-up resistors, 3.3V not enough, ...)?

> [@qwerty009](#):
>
> I use 5V to power LCD and i have 4,7k resistors in place.

While my above answer might apply to @qwerty009 's code, I've no clue why it would happen with the `example.ino`, as he pointed out in the third post.

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [December 15, 2014, 2:47pm UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/11 "2014-12-15T14:47:34Z")

</div>

@ScruffR, LOL I need to better read the posts! Your comments on the String stuff are valid. However, the first thing to focus on is the example. I would first remove the loop() code and modify the lcd = new line in setup() for the 20x4 display. If that works, then add the loop() code back in and see if that works. If that works, I would let it run for a while to see if it corrupts.

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 15, 2014, 2:50pm UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/12 "2014-12-15T14:50:52Z")

</div>

The problem seems to be that the issue only seems to appear after running for some time - so it’s bound to be in connection with the loop, that first runs OK but eventually starts tripping up.

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [December 15, 2014, 3:03pm UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/13 "2014-12-15T15:03:51Z")

</div>

@ScruffR, I would increase the delay(200) to delay(1000) and see what that does. Other members have brought up a similar issue with I2C dropping or freezing after some time. This may be related.

---

<div class="post-metadata">

**Author:** ![qwerty009](https://avatars.discourse-cdn.com/v4/letter/q/97f17d/32.png) [@qwerty009](https://community.particle.io/u/qwerty009)\
**Post date:** [December 15, 2014, 4:09pm UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/14 "2014-12-15T16:09:20Z")

</div>

Increasing delay() in Loop from 200ms to 1000ms makes it alot more stable, but it still crashes (example file).  
200ms delay it lasts usually max 90sec, with 1000ms delay it lasts several minutes.

Peekay what did you mean to modify LCD new to 20x4? Don’t i have it set up for it? If not what would i need to do exactly?

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [December 15, 2014, 4:15pm UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/15 "2014-12-15T16:15:49Z")

</div>

@qwerty009, line 8 of your example is:

```
   lcd = new LiquidCrystal_I2C(0x27, 16, 2);

```

I thought you had a 20x4 display, not 16x2

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 15, 2014, 4:16pm UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/16 "2014-12-15T16:16:02Z")

</div>

When you say a lot longer - is it about 5 times as long 😉 which would suggest that it is more likely to be a case of iterations rather than timing.

---

<div class="post-metadata">

**Author:** ![qwerty009](https://avatars.discourse-cdn.com/v4/letter/q/97f17d/32.png) [@qwerty009](https://community.particle.io/u/qwerty009)\
**Post date:** [December 15, 2014, 5:17pm UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/17 "2014-12-15T17:17:33Z")

</div>

Here’s my current code:

[code]// This #include statement was automatically added by the Spark IDE.  
#include “analogSmooth.h”

// This #include statement was automatically added by the Spark IDE.  
#include “OneWire/OneWire.h”

// This #include statement was automatically added by the Spark IDE.

#include “LiquidCrystal\_I2C.h”  
#include “SparkTime/SparkTime.h”

String writeAPIKey = “xxx”;  
String channelID = “20611”;

// TCP socket initialize  
TCPClient client;

int ledRed = A0; //if ON then indicates that there is 230V present and boiler pump is OFF  
int ledGreen = A1; // is ON then boiler pump is ON

int radikatemp = A2;  
int toatemp = A3;  
int pliiditemp = A4;  
int pliidiveetemp = A5;

int relayRadiator = D2; //relay to turn ON/OFF radiator pump  
int relayBoiler = D3; // relay to turn ON/OFF boiler pump.

int c = 0;

LiquidCrystal\_I2C \*lcd;  
UDP UDPClient;  
SparkTime rtc;

unsigned long currentTime;  
unsigned long lastTime = 0UL;  
String timeStr;

AnalogSmooth as100 = AnalogSmooth(100);  
AnalogSmooth as101 = AnalogSmooth(100);  
AnalogSmooth as102 = AnalogSmooth(100);  
AnalogSmooth as103 = AnalogSmooth(100);

void setup(){

```
Serial.begin(9600);
Serial.println("===Starting===");
rtc.begin(&UDPClient, "north-america.pool.ntp.org");
rtc.setTimeZone(+2); // gmt offset
lcd = new LiquidCrystal_I2C(0x27, 20, 4); // set the LCD address to 0x20 for a 16x2 //SparkCore bug, address is actually 27 but shifted (0x27*2)
lcd->init(); // initialize the lcd
lcd->backlight();
lcd->clear();

pinMode(radikatemp, INPUT_PULLDOWN);
pinMode(toatemp, INPUT_PULLDOWN);
pinMode(pliiditemp, INPUT_PULLDOWN);
pinMode(pliidiveetemp, INPUT_PULLDOWN);

pinMode(ledRed, OUTPUT);
pinMode(ledGreen, OUTPUT);
pinMode(relayRadiator, OUTPUT);
pinMode(relayBoiler, OUTPUT);

digitalWrite(relayRadiator, LOW);
digitalWrite(relayBoiler, LOW);
analogWrite(ledRed,150);
}

```

void loop(){

```
currentTime = rtc.now();

if (currentTime != lastTime) {
    timeStr = "";
    timeStr += rtc.hour12String(currentTime);
    timeStr += ":";
    timeStr += rtc.minuteString(currentTime);
    timeStr += ":";
    timeStr += rtc.secondString(currentTime);    
    timeStr += " ";    
    timeStr += rtc.AMPMString(currentTime);
    
    lastTime = currentTime;
}

lcd->setCursor(5,0);
lcd->print(timeStr);

double radikaC = (as100.analogReadSmooth(radikatemp) * 3.3) / 4095*100; //getting the voltage reading from the temperature sensor
double toaC = (as101.analogReadSmooth(toatemp) * 3.3) / 4095*100;
double pliidiC = (as102.analogReadSmooth(pliiditemp) * 3.3) / 4095*100;

```

// double pliidiveeC = (analogRead(pliidiveetemp) \* 3.3) / 4095\*100;

```
lcd->setCursor(1,1);
lcd->print("Radikasse");
lcd->setCursor(11,1);
lcd->print(radikaC, 1);
lcd->setCursor(1,2);
lcd->print("Toatemp");
lcd->setCursor(11,2);
lcd->print(toaC, 1);  
lcd->setCursor(1,3);
lcd->print("Pliita");
lcd->setCursor(11,3);
lcd->print(pliidiC, 1);

```

c = tempkontroll(radikaC, toaC, c);

```
byte sec = rtc.second(currentTime);
  if (sec == 10) {
          if(Spark.connected())
            {
                ThingSpeakUpdate("field1="+String(toaC)+"&field2="+String(radikaC));
            }
}

```

byte min = rtc.minute(currentTime);  
if ((min == 10 && sec ==30) || (min == 20 && sec ==30) || (min == 30 && sec ==30) || (min == 40 && sec ==30) || (min == 50 && sec ==30) || (min == 01&& sec ==30)) {  
lcd-\>init();  
}

delay(1000);

}

void ThingSpeakUpdate(String tsData)  
{  
Serial.println("Date string: " + tsData);

```
Serial.println("...Connecting to Thingspeak");
 
// Connecting and sending data to Thingspeak
if(client.connect("api.thingspeak.com", 80))
{
    Serial.println("...Connection succesful, updating datastreams");
     
    client.print("POST /update HTTP/1.1\n");
    client.print("Host: api.thingspeak.com\n");
    client.print("Connection: close\n");
    client.print("X-THINGSPEAKAPIKEY: "+writeAPIKey+"\n");
    client.print("Content-Type: application/x-www-form-urlencoded\n");
    client.print("Content-Length: ");
    client.print(tsData.length());
    client.print("\n\n");
     
    client.println(tsData); //the ""ln" is important here.
 
    // This delay is pivitol without it the TCP client will often close before the data is fully sent
    delay(200);
     
    Serial.println("Thingspeak update sent.");
}
else{
    // Failed to connect to Thingspeak
    Serial.println("Unable to connect to Thingspeak.");
}
 
if(!client.connected()){
    client.stop();
}
client.flush();
client.stop();

```

}

int tempkontroll(double radikaC, double toaC, int c)  
{

```
    if(toaC < 21 && radikaC > 35 && c == 0) {
    c = 1;
    digitalWrite(relayRadiator, HIGH); //paneb relee "HIGH" asendisse
    }

    else if((toaC > 22 || radikaC < 35) && c == 1) {
    c = 0;
    digitalWrite(relayRadiator, LOW); //paneb relee "LOW" asendisse
    } 
    return c;
}

```

[/code]  
atm i added

` byte min = rtc.minute(currentTime);
      if ((min == 10 && sec ==30) || (min == 20 && sec ==30) || (min == 30 && sec ==30) || (min == 40 && sec ==30) || (min == 50 && sec ==30) || (min == 01&& sec ==30)) {
            lcd->init();
        }` that should do the trick but it’s not a solution.

ScruffR - yea id say bout 5x longer. What do you suggest me to try?

---

<div class="post-metadata">

**Author:** ![peekay123](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/peekay123/32/810_2.png) [@peekay123](https://community.particle.io/u/peekay123)\
**Post date:** [December 15, 2014, 5:27pm UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/18 "2014-12-15T17:27:52Z")

</div>

@qwerty009, are you trying to reset the display every 10 minutes?

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [December 15, 2014, 5:28pm UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/19 "2014-12-15T17:28:24Z")

</div>

> [@ScruffR](#):
>
> The first thing to check would be to Serial.print() the string you want to display on your LCD.

and

> [@ScruffR](#):
>
> Maybe you should try char instead.

* * *

And, does this `lcd->init();` actually cure the problem?

---

<div class="post-metadata">

**Author:** ![qwerty009](https://avatars.discourse-cdn.com/v4/letter/q/97f17d/32.png) [@qwerty009](https://community.particle.io/u/qwerty009)\
**Post date:** [December 16, 2014, 6:11am UTC](https://community.particle.io/t/20x4-i2c-lcd-crashing-on-spark-core/8631/20 "2014-12-16T06:11:45Z")

</div>

> [@ScruffR](#):
>
> lcd-\>init();

Yes lcd-\>init(); resets lcd and cures it if it's been crashed.
