I have two otherwise identical projects, but they differ in the I2C address of the LCD display (0x27 and 0x3F). I cannot change them onboard by cutting or soldering bridges.
In the global section of the sketch I have compiler directive #ifdef O1
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address #endif
#ifdef O2
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address #endif
It would be more elegant if I could scan the two addresses and then initialize the LCD with the address found, but I am unsure if that would work, as the scanning typically would be in the setup-part of the sketch and I have no idea if it then could be carried over to the global declaration part.
The hd44780 LCD library automatically detects the I2C address (and I2C expander to LCD pin wiring). And I think that it is the best I2C LCD library out there.
paai:
Does that library also report the actual IC2 address?
There is a way to get it if you actually need it.
The API functions to get it are not actually part of the formal API and hence are not documented.
Typically a sketch has a single LCD device and does not actually need to know the i2c address.
If you want to get it/show it for diagnostic purposes, the library comes with a full diagnostic sketch that will show you the i2c address, pin mappings and backlight control configuration.
Anyway, I can get the actual address using a few lines from Arduino Playground - I2cScanner. As I know that one project has its LCD on 0x27 and the other one on 0x39 , it is a nice way to check on which hardware the sketch is running.
(They are controllers for annealing kilns in a small glass blowing shop and they also report current temperatures to my Linux computer. I may want to add a website to them, and then I need to know them apart).
I get that you may want to know the address to be able to physically tell them apart but I'm
still not understanding why the i2c address needs to be known by the sketch as I'm assuming you only have one LCD device being used by the Arduino board & sketch.
If the sketch is working with the LCD device why does the i2c address need to be known by the sketch?
The hd44780 library comes with a couple of sketches that will show the address on the LCD display
if you want to see it, but other than that, why would you need the address?