Conflict between Wire and SoftwareSerial?

Hey guys,

I've got a few different Arduino boards here but so far I've only tried this code on the Leonardo.

I'm talking to a serial-enabled LCD via SoftwareSerial. I've also got a barometer that I'm talking to over I2C with the Wire library. Everything is fine and dandy until I run into a call to Wire.begin(). As soon as that code hits the LCD stops responding with any new text. Here's a code snippet:

#include 
#include 

SoftwareSerial lcd(3,2);

int counter = 0;

void setup() {
  lcd.begin(9600);
  clear();
}

void loop() {
  if (counter == 5) {
    Wire.begin();
  }
  clear();
  lcd.print(counter++);
  delay(1000);
}

void clear() {
  lcd.write(254);
  lcd.write(128);
  lcd.write("                ");
  lcd.write("                ");
}

void loop() {
  if (counter == 5) {
    Wire.begin();
  }
  clear();
  Serial2.print(counter++);
  delay(1000);
}

void clear() {
  Serial2.write(254);
  Serial2.write(128);
  Serial2.write("                ");
  Serial2.write("                ");
}

The LCD will count up ... 0...1...2...3...4... then nothing more, just stay on 4 forever. If I comment out Wire.begin() then everything carries on as normal. If I don't setup the serial connection to the LCD then I can read from the barometer just fine.

So it seems like there's some kind of conflict between the Wire library and SoftwareSerial. Maybe a timing issue with sending signals out at clock ticks or something? I can't image that Wire is trying to use digital pins 2 or 3 for anything...

Any help would be appreciated!

Thanks, Rob

void loop() {
  if (counter == 5) {
    Wire.begin();
  }
  clear();
  Serial2.print(counter++);
  delay(1000);
}

Code tags please, like above.

How to use this forum

Why are you doing Wire.begin like that? Do it in setup.

http://arduino.cc/en/Main/arduinoBoardLeonardo

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

You are using pins 2 and 3 both for serial and I2C. Of course you can't use them for both things at the same time. Why not use hardware serial anyway?

Read the reference of the SoftwareSerial and Wire library. http://arduino.cc/en/Reference/SoftwareSerial http://arduino.cc/en/reference/wire

For the Leonardo, I2C is on pin 2 and 3 as Nick Gammon wrote. And about the SoftwareSerial library: "Not all pins on the Leonardo support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI)".

I'm talking to a serial-enabled LCD via SoftwareSerial.

On a board with 2 hardware serial port? When you don't use either hardware serial port? Why?

The serial port on the Leonardo at pin 0 and 1 (Serial1) is a hardware serial port and it is not used for uploading a sketch or the serial monitor. So that hardware serial port is not used yet, so why don't you use it ?

I'm experiencing a similar issue with MPU6050 orientation sensors and HC05 bluetooth modules on a Uno. Did you got to a solution?

(I know i'm 6 years late..)