LiquidCrystal_I2C V2.0 Timing Issues

I use the LiquidCrystal_I2C library in almost all my Projects. The Speed has been an issue, especially at start up. It takes over a full second to init the Display and that just doesn’t appear very professional when someone is sitting there waiting for the thing to come on.

I used the following sketch to test the Timing. (adapted from the Standard hello world sample from the library)

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

LiquidCrystal_I2C lcd(0x27,20,4);  // set the LCD address to 0x27 for a 20 chars and 4 line display
unsigned int timeBegin;
unsigned int timeEnd;
void setup()
{ timeBegin = micros();
  lcd.init();                      // initialize the lcd 
  timeEnd = micros();
  // Print a message to the LCD.
  lcd.backlight();
  lcd.print("Hello, world!");

  lcd.setCursor(0,2);
  lcd.print(timeEnd - timeBegin);
  lcd.print(" us");

  lcd.setCursor(0,3);
  lcd.print("Test 10");
}

void loop()
{
}

The first run reported 1024708 us, or just over a second.

Looking at the library, I see the begin method which I will include here for clarity. (But I also include comments (//Comment #x) in reference to my questions which come later.

	// SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION!
	// according to datasheet, we need at least 40ms after power rises above 2.7V
	// before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50
//Comment #4
	delayMicroseconds(50000);

	// Now we pull both RS and R/W low to begin commands
	expanderWrite(_backlightval);	// reset expanderand turn backlight off (Bit 8 =1)
//Comment #1
	delay(1000);

  	//put the LCD into 4 bit mode
	// this is according to the hitachi HD44780 datasheet
	// figure 24, pg 46
	
	// we start in 8bit mode, try to set 4 bit mode
	write4bits(0x03);
	delayMicroseconds(4500); // wait min 4.1ms
	
	// second try
	write4bits(0x03);
//Comment #2
	delayMicroseconds(4500); // wait min 4.1ms
	
	// third go!
	write4bits(0x03); 
//Comment #3
	delayMicroseconds(150);
	
	// finally, set to 4-bit interface
	write4bits(0x02);

In reference to Comment #1, what do we Need this 1 second delay for? I see no reason for it in the data sheet and quite frankly, everything works fine (but faster) if you just comment it out. Can anyone see an actual reason for this delay=

In reference to Comment #2, the way I read the datasheet, this delay can easily be reduced from delayMicroseconds(4500) to delayMicroseconds(150). Am I reading it wrong or can anyone see an actual reason for the 4500 microsecond delay. It works fine for me (granted with given Hardware) if I reduce it to 150.

In reference to Comment #3, I see no reason for this delay at all and again, it works for me if I comment it out.

In reference to Comment #4, this one is simply strange. According to the Arduino reference delayMicroseconds(50000) isn’t even supported. I looked at earlier Versions of the library and it used to be delay(50). If I Change it, the total time for the init jumps even higher! Frankly, I have reduced it as low as delay(10) and it worked, even after a power on reset. (I realize this may be on a given Hardware Setup and others may need a longer delay.)

However, the Timing issues go even further.

In the clear method, there is a delayMicroseconds(2000) which seems to work fine if reduced to 900.

In the home method, there is another delayMicroseconds(2000) which seems to work fine if commented out.

Making all of These changes, the init takes about 26 ms instead of over a second.

If it sounds too good to be true, it probably is… so what have I done wrong and why is this not all a good idea?

Read the timing for a HD44780

For example for the 4500 microseconds, this is to wait at least 4.1ms as per the doc page 45 (and table 6 page 24/25 too)

There are probably gross approximation in the library and depending on your hardware some stuff can work faster I suppose... never dug much into it

I did read the Hitachi datasheet. The datasheet says, as you indicated, that 4.1ms is required after the FIRST write but much less is required (100 µs) after the SECOND write and None seems to be required after the THIRD.

The library contains two 4.5ms and one 150us delays but the datasheet seems to indicate that only one 4.5ms and one 150us are required.

The library also contains one 50ms delay at the beginning of the initialization, apparently because the datasheet says the LCD Needs this time to become stable after power up but what the library doesn't take into account is that the Arduino does a lot of processing before the user sketch is even started (depending on if/which bootloader is used, etc.)

As for the clear command, table 6 of the datasheet doesn't provide any data as to time required. The library applies a delay(2000) which seems to be radically overstated.

As for the home command, table 6 of the datasheet says 1.52ms and the library applies a delay of 2000ms.