Compatibility issue with LiquidCrystal.h and Wire.h


I have an issue with the usage of LiquidCrystal.h and Wire.h in the same sketch:

This is the minimal sketch to reproduce the error: The issue is: as soon as I uncomment the line with the code Wire.begin() my LCD is totally corrupted (both lines). If this line is commented everything is fine and the LCD displays all characters in both lines perfectly.

I work under Arduino 1.0.1 and I use the libraries LiquidCrystal.h and Wire.h from this installation.

The sketch is here:

#include "Wire.h"

// include the LCD library code
#include "LiquidCrystal.h"

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
    // set up the LCD's number of columns and rows: 
    lcd.begin(16, 2);
    // as soon as I uncomment the next line my LCD gets corrupted
    // Wire.begin();

void loop() {
    // Print a message to the LCD.
    lcd.setCursor(0, 0);
    lcd.print("LCD Test");

    lcd.setCursor(0, 1);
    // delay
    delay(500);               // wait for 500ms

Any idea how to fix this ?

Thanks in advance.

What Arduino board do you have?

I have a new Olimexino-32u4 (Leonardo compatible), operated and power supplied from the USB port. Under Arduino IDE it appears correctly as Arduino Leonardo.

It would be helpful to mention that in the original post.

From that page:

TWI: 2 (SDA) and 3 (SCL). Support TWI communication using the Wire library.

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

The Wire hardware on that board uses pins 2 and 3. You are using them for the LCD. Once initialized, the Wire library "takes over" those pins. Choose different ones for the LCD.

My first thought was conflicting pin assignments but that looks ok. (Wire.h looks like it only uses A4/A5) As a test, try changing to LiquidCrystal lcd(12, 11, 5, 4, 3, 2); to something like [s]LiquidCrystal lcd(A5, A4, A3, A2, 3, 2);[/s] Hell scratch that. I just got Warning - while you were reading 3 new replies have been posted. You may wish to review your post. Nick beat me to it. =(

Thank you very much Nick for the fast reply. Now I understand the issue and I think I can easily resolve it. My wrong assumption was that SCA and SCL lines are separate from the Dxy pins. Now I have also seen in the schematic of the board that they are connected to each other but also put to different board headers. So in effect we have them duplicated on the headers ? Does it make sense ?

They brought them out to separate pins because on the Uno SDA/SCL are A4/A5. By doing that manufacturers of shields can get the SDA/SCL from those extra pins, without worrying about which pins on the underlying board they should use.

Hi Nick, o.k. understood and thank you again: By changing the pins I got my program and LCD working as expected. I am very happy that I got so fast the issue resolved with your help.