Can't begin LCD I2C in loop?

Good morning everyone.
I have a problem and I hope someone will come to the rescue.
In my code I need to begin the LCD when a switch is turned on: when i turn on this switch the LCD is connected to Vcc and so i need to begin it to send characters.
The problem is that when I turn it on, my Arduino just freezes and i need to turn off the switch and turn it on again to make it work.
Any idea on how to do it?
Could an interrupt help?

I tried different LCD I2C libraries, but everyone had a flaw.
I'm using now the "Arduino-LiquidCrystal-I2C-library-master".

This is a section of the code:

    LCDSWvalue = digitalRead( LCDSW );               //Read LCD switch state  

    if (LCDSWvalue == 1) {
      if (flag == 0) {
        flag = 1;
        delay (5);
        lcd.begin();                                 //starts the LCD as a 16 characters, 2 rows display
        delay (10);
        lcd.clear();                                 //clears the LCD
        delay (10);
      }

Thanks a lot!

LCD module, why not just blank it?

I use those cheapie microwave motion detector modules to un-blank the display when movement is detected and blank with no movement.

void fScreenBlanking( void *pvParameters )
{
  int      TimeOfPause = 10000 * 1000;
  uint64_t PauseStartTime = esp_timer_get_time();
  bool     Pause = false;
  for ( ;; )
  {
    if (!Pause )
    {
      //if motion detect then show display otherwise blank display
      if ( !(gpio_get_level( GPIO_NUM_17)) )
      {
        tft.enableDisplay( false );
      } else {
        Pause = true;
        PauseStartTime = esp_timer_get_time();
        tft.enableDisplay( true );
      }
    } else {
      // still detecting movement reset blanking pause time
      if ( gpio_get_level( GPIO_NUM_17) )
      {
        PauseStartTime = esp_timer_get_time(); // extend pause blanking time
      }
      if ( (esp_timer_get_time() - PauseStartTime) >= TimeOfPause )
      {
        Pause = false;
      }
    }
    vTaskDelay( 125 );
  }
  vTaskDelete( NULL );
} //void fScreenBlanking( void *pvParameters )

I'm sorry I'm a rookie and I don't fully understand your code.
What do you mean with "why not just blank it"?
Thanks for your quick answer :slight_smile:

does the lcd have a backlight pin?

nope, it has a I2C module so it only has 4 pins: data, clock, vcc and gnd.
The switch is directly connected to the VCC pin.

Hi, @darioino
Why do you want to switch off the LCD display?
Is that your complete circuit diagram?

Can you please post a circuit diagram of your project, including power supplies, use a pen(cil) and paper and show an image of it would be fine.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

1 Like

Wire.begin() is called in the lcd.begin() method. After switching off the display, there is no Wire.end().

It may help to add a Wire.end() to the code.

if (LCDSWvalue == 1) {
      if (flag == 0) {
        flag = 1;
        Wire.end();   // added
        delay (5);
        lcd.begin();                                 //starts the LCD as a 16 characters, 2 rows display
        delay (10);
        lcd.clear();                                 //clears the LCD
        delay (10);
      }
1 Like

Thanks everyone for your kind answers!
I'll try to add the wire.end as you suggested!
I'll let you know as soon as possible.
:blush:

My project is a solar panel that follows the sun with a switch that, when pressed, turns on the LCD and lets you see some parameters (voltage, current, position, ...).
For efficiency reasons I need to shut down the LCD when it's not in use.

My full circuit is a bit complicated to draw, but if you need i can try!
It has a servo motor, two LDRs, some resistances, a battery, two switches, the LCD and some modules for the powering and recharging.

An LCD uses very little power, far less than an Arduino. It's not worth the trouble to save such a small amount of power, it will make almost zero difference.

It's the backlight in an LCD that uses most of the power. This is what you need to cut power to for efficiency.

The backlight can be switched on and off from your sketch with some types of i²c adapter. So you could wire a button or switch to an Arduino pin, and have your sketch read that pin and turn the backlight on/off.

Other types of i²c adapter have an extra pair of PCB pins to enable/disable the backlight. Normally there is a jumper fitted to these pins and if you remove the jumper, the backlight is disabled. So you could remove that jumper and connect a button or switch directly to those PCB pins. Simple!

1 Like

Wow thanks a lot for your answer!
I'll try everything you said and I'll let you know.
Thanks :pray:

Specifically, the backlight takes about 22 mA, while the display itself takes less than 2 mA.

Actually, if would take less than 1 mA if you could correct a silly mistake in the design of the I²C backpack. The contrast (blue) potentiometer is connected to Vcc and should not be - but it is generally not easy to disconnect that.

The point is that whatever the "Arduino" you are using, it will likely draw in the order of 20 to 50 mA, far more than the display.

And that is far less than the servo motor will draw as it may activate simply to hold its position, so completely disabling the servo would be the first priority in power saving. That done, if you are operating from a battery, you do not want to use a UNO (or even Nano) as its USB interface draws power, instead a Pro Mini.

Actually, that is a trifle confusing. :dizzy_face: The most commonly available I²C backpack actually has both of these.

As does the less common but cheaper "Keyes" Red board.

Of which I have just bought five to investigate how easy it is to correct the bad wiring of the potentiometer. And also because basically, these are a very useful general-purpose PCF8574 adapter board if you want seven "raw" I/O pins and one transistor buffered output. :grin:

The red board also allows you with due care, to re-mount the i²c pins on the correct end of the board for use with a LCD display. :+1:

1 Like

Here I am. I tried to add that line of code and...

And... you are a genius. It worked. I can't explain how much i'm grateful.
I was searching all over for a solution to this problem and it was so easy. Thanks, Thanks, Thanks.

Many thanks to everyone who helped this rookie.

And thank you so much for this detailed information for the I2C LCDs. I couldn't imagine a team so large and fast helping me for this problem. And most importantly so prepared.

Thanks, Thanks, Thanks.

You are welcome. It was just a suspicion that the I2C connection was the problem. I haven't tried such a construction with the switch on the display myself. So much the better that it actually works :).

But too much honor. I'm certainly not a genius... Then my life would have been different :crazy_face:

1 Like

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