I seem to be getting an error that a lot of people are getting.
Dug out an old Sainsmart 2004A LCD display, 4 lines of 20 characters, with the I2C interface on the back. I have used these successfully in the past, but now I'm having trouble.
Installed the LiquidCrystal I2C library from Frank de Brabander, it says it installed it successfully.
When I hook up Vcc, GND, SDA, and SCL, the display lights up nice and bright. But when I try to compile a sketch to write something to the display. I get the error message:
compilation terminated.
Compilation error: LCD.h: No such file or directory
Where can I find this LCD.h? And do I need to "install it manually", as some people on the forum have said? If so, how do I install it manually?
Thanks for your reply, noiasca. At your suggestion I loaded the Arduino "Hello World" sketch, compiled and ran it. It produced no errors, and also no output, i.e. I couldn't find "Hello World" printed or displayed in the Output section. I think I set up the Serial Monitor correctly before running the sketch.
In the Output section it said:
Sketch uses 1832 bytes (0%) of program storage space. Maximum is 253952 bytes.
Global variables use 200 bytes (2%) of dynamic memory, leaving 7992 bytes for local variables. Maximum is 8192 bytes.
No indications of any errors that I can see.
Earlier this evening I ran the "Blink" program, and that ran well, flashing the LED light on the Arduino board regularly. Now that I have tried the "Hello World" sketch, the LED light is no longer blinking.
I'm running an Arduino Mega 2560 board on COM5 with no shields.
The sum total of the "Hello World" sketch was:
void setup() {
// put your setup code here, to run once:
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Hello World!");
}
void loop() {
// put your main code here, to run repeatedly:
Once you find the LCD.h file there is no guarantee that the code you have will work with the whichever version of the LiquidCrystal library that you have. Then you have to know the right I2C address and the right LCD to backpack pin mapping. The best library for those I2C LCDs at this time is the hd44780 library. It automatically takes care of the I2C address and pin mapping and older code is easily modified to work with the library.
To install the hd44780 library. The hd44780 library is the best available for I2C LCDs. The library is available in the Library Manager. Go to Library Manager (in the IDE menus, Sketch, Include Libraries, Manage Libraries) and in the Topics dropdown choose Display and in the Filter your search box enter hd44780. Select and install the hd44780 library by Bill Perry.
The class that you want to use is the hd44780_I2Cexp class. There are examples to show how to use the library. The nice thing about the hd44780 library is that it will autodetect the I2C address and the I2C backpack to LCD pin mapping.
In the examples, there is a diagnostic sketch that will help us to help you if you still have trouble with the display. Run the diagnostic sketch and post the results.
The "Hello World" sketch that you posted will only output to the serial monitor. Did you open the serial monitor and make sure that the baud rate was 9600? Is anything printed?
Post the LCD sketch that you were trying and I can fix it to run with the hd44780 library (that you will need to install) so you can see how easy it is.
Thanks for your help! I haven't modified the Hello World example that I know of. Can you post it from your source, and I'll copy/paste it into my sketch.
BTW, what does "in code tags" mean?
I wired each of the four pins on the I2C board on the back of the LCD display, to its identically-named pin on the Arduino board.
Thanks for your help. If you can change it to run with the hd44780 library, that would be a huge help! It will get me on the right track to understanding how the sketch and the LCD display talk to each other.
@noiasca, Even with LiquidCrystal_I2C library that is installed with the IDE one still must change the LCD I2C address and the LCD to backpack pin mapping unless one is lucky enough that those values match the library defaults, exactly. The advantage of using the hd44780 library is that the I2C address and pin mapping are automatically determined.
OP, here is a simple "Hello World" sketch that should just work if the hd44780 library is properly installed and the LCD is properly wired.
// hd44780 simple "Hello World" by c gounding AKA groundFungus
// hd44780 library see https://github.com/duinoWitchery/hd44780
// the hd44780 library is available through the IDE library manager
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip
// LCD geometry
const int LCD_COLS = 20;
const int LCD_ROWS = 4;
void setup()
{
lcd.begin(LCD_COLS, LCD_ROWS);
lcd.clear();
lcd.print("Hello World");
lcd.setCursor(0, 1);
lcd.print("Millis ");
}
void loop()
{
updateLCD();
}
void updateLCD()
{
static unsigned long lcdTimer = 0;
unsigned long lcdInterval = 500; // update 2 times per second
if (millis() - lcdTimer >= lcdInterval)
{
lcdTimer = millis();
lcd.setCursor(8, 1);
lcd.print(" "); // overwrite old data
lcd.setCursor(8, 1); // reset the cursor
lcd.print(millis());
}
}
I assume 99% percent of the available I2C PCF Backpacks can be used with 0x27 or 0x3F.
The exact address can be determined with the I2C Scanner.
Determine what I2C address is needed and use it.
That is true, but the pin mapping is a different story. There are several pin mapping schemes. If the LCD does not happen to match the library default it can be a pain to figure out. That is why I recommend that library.
Yes, you can detect the Slave address by a scanner or by looking at the backpack pcb.
PCF8574 or PCF8574A and look at the A0-A2 wiring to determine the address.
Then you look at the chip D0-D7 mapping. There are two common schemes.
However the biggest problem is the name. "LiquidCrystal I2C library from Frank de Brabander"
Where does it come from?
It does not seem to be available from the IDE Library Manager.
There are several different "LiquidCrystal I2C" libraries all with the same class name LiquidCrystal_I2C but subtle differences.
It is MUCH safer to install hd44780.h library via the IDE Library Manager.
It will detect address and wiring automatically.
And comes with diagnostic sketch to report any problems.
noiasca and groundFungus, installation of the hd44780.h library made a world of difference. noiasca, the HelloWorld you recommended still didn't work, but groundFungus, yours did (the one showing milliseconds)!
I also found another example called TestRun20X04 that worked, and contains quite a few details on cursor movement, backlight manipulation, custom characters etc., and that worked too. Quite a treasure trove.
To answer the initial question.
LCD.h is header file that is included in the newLiquidCrystal library package.
If you had a sketch that used LiquidCrystal_I2C class and included LCD.h then likely that sketch was written for the LiquidCrystal_I2C i/o class that comes with the newLiquidCrystal library vs a sketch that used the LiquidCrystal_I2C library.
This kind of stuff demonstrates some of the issues that can show up when using "LiquidCrystal_I2C" in that there are multiple libraries that provide a LiquidCrystal_I2C.h header file and they work differently.