Difference Between .begin and .init

I was just following a video tutorial writing the sketch the same way they did for an LCD with a I2C. I used the Liquid Crystal I2C library same as they did. The problem was that when I ran the code check I got,

The code that I got the error for was,

lcd.begin();
Serial.begin(9600);
lcd.backlight();

But when I used,

lcd.init();
Serial.begin(9600);
lcd.backlight();

it worked fine.

So I was wondering what the difference between .begin and .init. is. I looked around and found "Initialize List" and other things but I'm not sure that's what I'm looking for.

Here is the entire sketch.

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

LiquidCrystal_I2C lcd(0x27,16,2);


void setup() {
  // put your setup code here, to run once:
  lcd.init();
  Serial.begin(9600);
  lcd.backlight();

}

void loop() {
  // put your main code here, to run repeatedly:
  lcd.clear();
  lcd.print("Hello World!");
  lcd.setCursor (0,1);
  lcd.print("Patty Boy");
  delay(10000);

}
1 Like

There are several libraries with the name LiquidCrystal_I2C. Some use the begin() function and some the init() function. If you find code on the net that you want to use you will need to figure out which of the several libraries that the code was written for. Or make a few simple changes and use the hd44780 library.

Those LiquidCrystal_I2C libraries are old and most are not maintained. The newest and best library for I2C LCD with the hd44780 controller (1602, 2004) is the hd44780 library by Bill Perry. The library is available via the IDE library manager. A huge advantage of the hd44780 library over the others is the automatic detection of I2C address and LCD to I2C backpack pin mapping.

It is usually very easy to change code written for one of the LiquidCrystal_I2C libraries to work with the much better hd44780 library. The changes entail only the right inlcudes, the constructor and the begin() function. If you have trouble I would be glad to make the required changes for you.

1 Like

Welcome to the wonderful world of libraries with the same name but different! You may have to try different "LiquidCrystal_I2C.h" libraries. You can open them and see what they are using in the .cpp file. I found one that used both commands, the second was aliased to the first.

1 Like

OK, great. Thanks I will look into it.

Here is your code modified to use the hd44780 library.

//#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header

//LiquidCrystal_I2C lcd(0x27,16,2);
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip


void setup()
{
   // put your setup code here, to run once:
   //lcd.init();
   lcd.begin(16, 2);
   Serial.begin(9600);
   lcd.backlight();
}

void loop()
{
   // put your main code here, to run repeatedly:
   lcd.clear();
   lcd.print("Hello World!");
   lcd.setCursor (0, 1);
   lcd.print("Patty Boy");
   delay(10000);
}

Just install the hd44780 library using the library manager, upload and run.

If you are interested in such things you should check the files in your library folder.

in the LiquidCrystal_I2C.cpp you will find

void LiquidCrystal_I2C::init(){
	init_priv();
}

void LiquidCrystal_I2C::init_priv()
{
	Wire.begin();
	_displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS;
	begin(_cols, _rows);  
}

void LiquidCrystal_I2C::begin(uint8_t cols, uint8_t lines, uint8_t dotsize) {
	if (lines > 1) {
		_displayfunction |= LCD_2LINE;
	}

more or less this means:

init() - without parameters - will call init_priv();
init_priv() will start the I2C layer, set an internal variable and call begin() with parameters
begin() will need parameters

in general it should be sufficient to use the functions according to the provided examples included with the library.

According to the LCD API 1.0 init() should be used to initialize the display. Other libraries might do it differently but then they do not follow the suggested API for LCDs

"begin" and "init" are both just arbitrary names for functions defined by whomever wrote the library. As you've noticed, it would be nice if all libraries were consistent to avoid confusing, but the function names COULD be anything (it's a little surprising that Arduino didn't choose "setup")

Do you realise that neither is a given. Library developer can call methods how they like and add and remove them at will. Some developers choose to follow some sort of convention with method naming, like .begin for example to be more in line with arduino methods, some prefer different names, like .init (I personally prefer that). But one thing is certain, you do not guess what it is, you open up the library and look up methods or even better read the docs if you value your time

I do now. I have been using Arduino/C++ for only three months and in the last two weeks I have been working with projects that have updated libraries or many different ones. It is fun learning.

Thanks for all the help and info.

I just used it and it worked great. I was also looking around at the examples that came with it and it looks like some good info.
Learning all this stuff will make me better at writing the sketches and using Arduino. I don't want to just copy and paste everything for the projects.

Quite a few people recommend using the hd44780 library and I do agree that it is a great library with some unique features.

I have been using one of the ubiquitous LiquidCrystal_I2C libraries for a long time. I have updated it a while back to also allow using it with Wire1.

I found that the hd44780 library adds about an extra 2K overhead to a sketch and since I regularly use smaller MCUs with only 8K, or maybe 16K, that is a big overhead. Compiling the same sketch with each of the libraries results in the following:

My current LiquidCrystal_I2C library:

Sketch uses 5236 bytes (15%) of program storage space. Maximum is 32768 bytes.
Global variables use 280 bytes (13%) of dynamic memory, leaving 1768 bytes for local variables. Maximum is 2048 bytes.

hd44780 library:

Sketch uses 7298 bytes (22%) of program storage space. Maximum is 32768 bytes.
Global variables use 335 bytes (16%) of dynamic memory, leaving 1713 bytes for local variables. Maximum is 2048 bytes.

Normally I only use a few of the most common available LCD functions. Should I need the addition features of the hd44780 library, and have the available memory, I will certainly use it. Generally, however, I am quite happy to use the LiquidCrystal_I2C library.

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