LCD 1602 i2c blocks

Hello
I have many projects using LCD 1602 (and2004). In all cases it is used with an I2C interface and the following code (as example):

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <LCD.h>

int theta=5;

LiquidCrystal_I2C  lcd(0x3F, 2, 1, 0, 4, 5, 6, 7); //or 0x27

void setup() {

  Wire.begin();

  lcd.begin (16, 2); //
  lcd.setBacklightPin(3, POSITIVE);
  lcd.setBacklight(HIGH);
}

void loop() {

lcdDisplay();
delay(1000);
}

void lcdDisplay(void){
// example for display a variable to lcd

lcd.setCursor(0, 0);
lcd.print("t: ");
lcd.print(theta);
lcd.print("   ");

}

In ONE and only one of the projects (Uno with 1602) I noticed that sometimes display is freezing and not coming back to life ever. Needs reset. I have changed Uno, Lcd, I2C interface, power supply -no solution. This project is in a harsh enviroment (electro-magnetic noise).

So, I wonder : Is there any way to “test” I2C line? And, if result of such a test is “blocked”, can I freely .end all relevant parts (wire, lcd) and then .begin them again?

On second thought, is there any obstruct in .end everything (in regular intervals) and .begin them again immediately after?

Thank you

The best solution is to solve the EMI issue. There are people here that can help you do that, if you post detailed schematics and detailed, clear and complete images of your hardware.

Typically if there is an i2c issue, the host (arduino) and the LCD become out of nibble sync and the display starts displaying garbage. You have said the display is "freezing". Are you sure the Arduino processor is still alive and not crashed?

BTW, setBacklight() in that library takes a dimlevel not HIGH/LOW. When you send it a HIGH, which is currently defined as 1 you are asking the library to set the display as dim as possible. Since that backpack does not support dimming, the library sets the backlight on. The appropriate value would be BACKLIGHT_ON, or use the backlight()/noBacklight() functions instead.

In terms of testing the i2c line. My hd44780 library includes a diagnostic test to test the i2c signals and the LCD module. The i2c test merely tests for pullups, but the LCD test does a check of the internal RAM of the LCD. It can be installed using the library manager. hd44780 is faster than fm's newLiquidCrystal library. See the github page readme and wiki for details on the library. The i/o class to use is the hd44780_I2Cexp https://github.com/duinoWitchery/hd44780

--- bill

The standard Arduino I2C library does not deal with some errors and the Arduino will hang, seen it often.

I recall there is an I2C library out there that has a timeout, so a simple and perhaps inevitable at some point in time bit of corrupt data does not stop your project.

srnet: The standard Arduino I2C library does not deal with some errors and the Arduino will hang, seen it often.

I recall there is an I2C library out there that has a timeout, so a simple and perhaps inevitable at some point in time bit of corrupt data does not stop your project.

In my testing of the IDE bundled AVR i2c library (Wire) I did notice that if the ack bit is corrupted (set) either by a misbehaving slave or by a glitch on the SDA line right as the last bit of a read transfer is happening, the Master (the AVR) will assume it is from another master on the bus and will back off, but do to a coding issue in the Wire library with no timeout on a multi master backoff, the AVR is now hung waiting forever for the other non existent master to start and finish its transfer.

--- bill