What if LCD fails to run?

Hello to everyone,
I just joined you. I did a short search, but I could not see any information about the program stuck when the 16x2 serial LCD was powered off.

I want the program to continue when the LCD stops working for some reason. I guess there is nothing like try catch?

As seen in the code below, if the LCD power is cut off, the program waits at the lcd.setcursor (0.0) line. How can I keep the program running even if the LCD is not working ?

void loop()
{
  digitalWrite (13, LOW);
  digitalWrite (12, HIGH);
  int chk = DHT.read22(DHT22_PIN);
  //Read data and store it to variables hum and temp
  hum = DHT.humidity;
  temp = DHT.temperature;
  //
  lcd.setCursor(0, 0); //if LCD power is turned off program stuck here
  //
  lcd.print("H: ");
  lcd.print(hum);
  lcd.print("%");
  lcd.setCursor(0, 1);
  lcd.print("T: ");
  lcd.print(temp);
  lcd.print("C ");
  delay(4000); //Delay 2 sec between temperature/humidity check.
  digitalWrite (13, HIGH);
  digitalWrite (12, LOW);
  delay(4000);

}

And we can't see how you have anything wired together, nor can we see what libraries you used for the LCD, IF ANY.
If you used some library, search the source code and see if anything is returned.

Paul

Here is the libraries:

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

and this is the definition in the LiquidCrystal_I2C.h
void setCursor(uint8_t, uint8_t);

The LCD has a power source independent of Arduino.

akifkutlu:
The LCD has a power source independent of Arduino.

Everything returns void that I can tell. Perhaps you can monitor the power source from the Arduino. I don't see a way from the library to detect a fault.

Very few LCD libraries have any code that can cause a lockup.

The LiquidCrystal_I2C library has no idea if the LCD is actually working, it doesn't care.
It just sends instructions to it hoping the LCD is listening and operating on them.

There is nothing in the LiquidCrystal_I2C code that can cause a hang.
The Wire library should return an error to any attempts to address a slave that is not present.
The LiquidCrystal_I2C code ignores any status / error codes returned from Wire.

You didn't say anything about what board you were using but I'm guessing it is an AVR based Arduino and I'm also
guessing that you may be simply powering off the LCD device which can cause a glitch on the i2c signals.
If there are glitches / noise on the i2c bus signals, it can trigger an issue in the AVR Wire library which causes the code to get stuck in an infinite loop and never return.

If you have IDE version 1.8.13 you can make a call to a function, Wire.setWireTimeout() to turn on a timeout to eliminate this loop lockup.
However....
Once you power off the LCD it will not function properly again with the LCD library until it is re-initalized by calling lcd.begin() or lcd.init() for libraries that do not have begin() API function like LiquidCrystal_I2C.

You can't just yank the power and re-apply it or yank the I2C cable and plug it back in or leave the i2c cable disconnected and plug it in later.
The LCD must be re-initalized each time the LCD powers up.

--- bill

How are you powering the LCD device? Specifically.
Show us how you have wired up the 4 pins on the LCD backpack. (power, gnd, SDA, SCL)
And some more information about your external power supply, why you need this, and how it is connected to the Arduino.

--- bill

Thank you bill, your answer is quite helpful. my whole project includes Arduino Uno, relays (four), servo motors (two), RTC , DHT22 and I2C LCD display. I really need to use separate powers for the I/O devices. That is why I need to test the software in case of partial power failure. I am intentionally turning off the LCD power to see what is going on. LCD display only gives information about the environment. with or without LCD the remaining equipment should continue to work. But when the LCD goes off the program stops functioning.
Thanks again.

You should consider powering the LCD from the Arduino rather than using a separate power supply.
Most 16x2 LCDs will draw less than 20ma when the backlight is on and some as low around 5ma.
You could turn off the backlight when not in use to save some power as the backlight is what consumes most of the power with that type of device.

Things will get quite a bit more complicated for the sketch s/w if you want to fully power down the LCD independently from the Arduino since the Arduino sketch software will have to be involved with reinitalizing the LCD once power is restored as the sketch will not be able to display anything until the LCD is initialized again.

--- bill

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.