I bought a Sainsmart LCD module with an I2C interface (marked LCD2004) Not one of the sketches available seem to work properly. All of the LiquidCrystal_I2C libraries have seemed to have problems. None of the code verifies without errors except for one and nobody can say how they got this and similar modules to work. I have a problem with individuals and Companies who post code sketches that don't even verify and have some bugs that are perpetuated from one sketch to another which means to me we have some 'virtual' experts out there.
If one adds the Serial instructions to this sketch to get the Serial Monitor to work it can be seen being sent but it doesn't show on the LCD.
I had terrible trouble finding a library that worked but after downloading the malpartidia LiquidCrystal_IC2 v1.2 from bitbucket and installing and deleting it umpteen times over the course of a week it suddenly and magically became visible to the Arduino IDE.
So why all this confusion? Either some people know the answer and are refusing to tell or no-one does and the modules will never work. I cannot believe the latter! If the situation is ever resolved - and its been going on an awful long time now, can the solution be posted as a sticky or something?
How do I get the following sketch to pass characters to the LCD?
/* Demonstration sketch for PCF8574T I2C LCD Backpack
Uses library from https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads GNU General Public License, version 3 (GPL-3.0) */
#include <Wire.h>
//#include <LCD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7); // 0x3F is the I2C bus address for Sainsmart LCD2004 and many others
void setup()
{
lcd.begin(20,4); // initialize the lcd for 20 chars 4 lines, turn on backlight
// activate LCD module
lcd.begin (16,2); // for 16 x 2 LCD module
lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(LOW);
}
void loop()
{
lcd.home (); // set cursor to 0,0
lcd.print(" tronixlabs.com");
lcd.setCursor (0,1); // go to start of 2nd line
lcd.print(millis());
delay(1000);
lcd.setBacklight(HIGH); // Backlight off
delay(1000);
lcd.setBacklight(LOW); // Backlight on
delay(50);
}
Maybe there is more than one "LiquidCrystal_I2C.h" but I use a library of the same name, and I think your code is illogical junk, written by somebody who hasn't the faintest idea of what I2C is about.
You might try this - assuming 0x3F is the correct address. I don't use the backlight command.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,20,4);
void setup()
{
Serial.begin(9600);
lcd.init(); // initialize the lcd
// Print our characters on the LCD
lcd.backlight(); //Backlight ON if under program control
lcd.setCursor(2,0); //Start at character 3 on line 0
lcd.print("1 sec delays??");
delay(1000);
lcd.setCursor(2,1);
lcd.print("From YourDuino");
delay(1000);
lcd.setCursor(0,2);
lcd.print("20 by 4 Line Display");
delay(1000);
lcd.setCursor(0,3);
delay(1000);
lcd.print("http://YourDuino.com");
}
void loop()
{
{
// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
delay(100);
// clear the screen
lcd.clear();
// read all the available characters
while (Serial.available() > 0) {
// display each character to the LCD
lcd.write(Serial.read());
}
}
}
}
Are 20x4 I2C LCD modules and code a worldwide conspiracy?
Effectively? . . . Yes.
All of the LiquidCrystal_I2C libraries have seemed to have problems.
. . .
So why all this confusion?
Several decades ago Hitachi came up with the HD44780U controller for the character mode LCDs. This controller uses a 14-pin parallel interface
As these LCDs became more popular other manufacturers came out with their own versions of the controller that are virtual clones. Programs written for one of them work with all of the others. Therefore it was relatively easy to come up with a library that works with all of them as well.
The same is not true for the I2C interfaces that have sprung up to reduce the number of I/O pins required to drive those LCD controllers. Although many of them use the same IC, typically the pcf8574 or pcf8574A, there are still several problems.
First of all, these ICs have three 'address' pins that are implemented by the design of pc board on which they are mounted. Therefore either of those chips will have one of eight possible addresses depending upon how the pc board (and possibly some jumpers on that board) is (are) configured. Since the 'A' model has a different address range than the non 'A' model there are 16 possible addresses for the same pc board depending on which chip is used and how the address pins are implemented.
Next comes the interconnections between the I/O pins of the pcf8574 and the I/O pins of the LCD module. These interconnections are determined by how the pc board is designed and there are no standards or pseudo standards.
Finally there is the matter of controlling the backlight, which is completely separate from the LCD controller itself. There is a sticky thread at the beginning of this forum that deals with this subject.
As a result of these variables each interface module design requires a different library or at least a different version of a similar library, but virtually all of the manufacturers have used the same name for their version of the library.
Currently the malpartidia LiquidCrystal_IC2 library is the library of choice.
Use the sketch found here i2cLCDguesser.zip to determine the parameters to use with that library.
Thanks to floresta for an interesting reply, it does in a lot of spade-work I missed. I have come across the blue LCD item previously and the code didn't work at all in my download of the Arduino IDE. That's not too surprising because none did. Then I gave in to all of those who said their module did not have the I2C bus address suggested and scanned mine. True enough, it was not 0x27 but 0x3F. The code I posted above was the first one to work but then it seemed only for the backlight. Then I gave in and adjusted the contrast from a central setting. I'd expected a control width gradual adjustment but it's quite sharp isn't it? It would have been nice to know this!
Now it all works but there's something I don't quite understand and I don't believe in magic. I installed and tried every !!!!!!! library I could find but none seemed to work. I stuck with the illogical junk sketch however because it is simple to understand (well, not simple) I tried the malpartida LiquidCrystal_I2C library, time and time again - I installed it the Arduino zip file way and installed it manually extracting it first before putting it in the file in 'my documents'.
I'm not a beginner at working with Windows Explorer, anyone with MS Flight Sim will know exactly what I mean. The magic occurred when for some reason the sketch suddenly recognised the library - what happened there? I know I'm getting old but I will not have it that I got the installation of the library wrong so many, many times.
Now, I WAS beginning to curse Sainsmart but their module looks the part, it certainly isn't junk. It came from Germany, not China where a lot of duff stuff can be found. However at around £12 from Amazon It was the cheapest. I just looked on Farnell's website finding similar modules at the same price but without VAT and P&P!
The point is that I had expected to have no more trouble with the LCD than I had getting an RTC module to work, but what a shock. It's harder for me because my memory is a lot shorter than a goldfish's. I am trying to get an understanding of C++ but it's very difficult if you can't remember what you learnt a minute or two before!
What confuses me most is the code in a library - which is written by experts and too advanced for me.
One last question. I have been trying to install Atmel Studio 6 but it will not install despite the fact I've tried everything I know. It hangs on 'first time start' during install and then hangs several times because it apparently can't find MS studio which is obviously in place. Anyone know about this?
Oh, and I shall try Nick Pyner's code when I get off this soap box.
Now then Nick Pyner - your code doesn't verify, not on my version of the Arduino IDE at any rate. I have to say that the error report is gobbledegook to me, Even the very first line reports an 'invalid conversion from 'int' to 't_backlighPol' - where does that come from?
Arduino: 1.0.6 (Windows 7), Board: "Arduino Uno"
C:\Program Files (x86)\Arduino\hardware\tools\avr\bin\avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=106 -IC:\Program Files (x86)\Arduino\hardware\arduino\cores\arduino -IC:\Program Files (x86)\Arduino\hardware\arduino\variants\standard -IC:\Program Files (x86)\Arduino\libraries\Wire -IC:\Users\EE\Documents\Arduino\libraries\LiquidCrystal_I2C C:\Users\EE\AppData\Local\Temp\build6071210821650336395.tmp\sketch_nov24a.cpp -o C:\Users\EE\AppData\Local\Temp\build6071210821650336395.tmp\sketch_nov24a.cpp.o
sketch_nov24a:5: error: invalid conversion from 'int' to 't_backlighPol'
sketch_nov24a:5: error: initializing argument 3 of 'LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t, uint8_t, t_backlighPol)'
C:\Users\EE\Documents\Arduino\libraries\LiquidCrystal_I2C/LiquidCrystal_I2C.h: In function 'void setup()':
C:\Users\EE\Documents\Arduino\libraries\LiquidCrystal_I2C/LiquidCrystal_I2C.h:154: error: 'int LiquidCrystal_I2C::init()' is private
sketch_nov24a:10: error: within this context
Who knows? It works fine for me. It's not my programme, I assumed it came with the YWRobot library but apparently it comes from YourDuino. Not that this matters, as they all start the same way
I hear you man. Not sure is a conspiracy, but I can tell you that a guy who was running www.web4robot.com had an outstanding I2C LCD + keypad backpack. Unfortunately, he is not around anymore and the website is down. I should have bought a few when I had the chance.
This means that you have the 'A' version of the chip and all of the address lines are pulled high. You could run another similar board in parallel (using the same I2C pins) as long as you used either the non 'A" version of the chip or if you changed at least one of the jumpers.
The code I posted above was the first one to work but then it seemed only for the backlight.
That's because you used the correct address so the Arduino could communicate with the adapter. The adapter could not communicate with the LCD controller because the pin information was wrong. I don't know about the backlight - I think you got lucky.
Then I gave in and adjusted the contrast from a central setting. I'd expected a control width gradual adjustment but it's quite sharp isn't it? It would have been nice to know this!
A central setting is almost always wrong because the optimum contrast voltage is typically about 10% of the supply voltage. It's shouldn't be too sharp but the optimum setting is close to (or at) one end of the potentiometer range.
The point is that I had expected to have no more trouble with the LCD than I had getting an RTC module to work, but what a shock
There's a big difference because the RTC module is the end destination of the I2C communications from the Arduino. The LCD adapter module is only a middleman, it has to relay information from the Arduino to another device which is where many variables come in.
What confuses me most is the code in a library - which is written by experts and too advanced for me.
Like everything else it is complicated because it is versatile. In this case FM wanted to retain compatibility with the original LiquidCrystal library commands while providing the ability to use a standard parallel connection or any of a wide variety of I2C variations as I outlined previously.
One last question. I have been trying to install Atmel Studio 6 but it will not install despite the fact I've tried everything I know. It hangs on 'first time start' during install and then hangs several times because it apparently can't find MS studio which is obviously in place. Anyone know about this?
You will have better luck getting information at the AVR freaks forum. --> http://www.avrfreaks.net/