arduino / ArduinoCore-avr

The Official Arduino AVR core
https://www.arduino.cc
1.24k stars 1.05k forks source link

Wire + SoftwareSerial #280

Closed OpreaFlorin closed 5 years ago

OpreaFlorin commented 5 years ago

I am using Arduino Uno R3 board and Adafruit FONA 800 Shield - Voice/Data Cellular GSM for Arduino together. I am sending commands to SIM800 and I am receiving responses. To do this I am using a SoftwareSerial (pins 2 and 3). So far so good. This module is designed to be a slave into a I2C network where another Arduino Uno R3 is the master (only one master, only one slave). Server-slave examples works perfectly . The problem appears when I communicate with FONA 800 Shield using SoftwareSerial and in the samme time communicate with master by using Wire.

This is the master code:

#include <Wire.h>

void setup() {
  Serial.begin(115200);
  Wire.begin(); // join i2c bus (address optional for master)
}

byte x = 0;

void loop() {
  Wire.beginTransmission(8); // transmit to device arduino/Arduino#8
  Wire.write(x);        // sends five bytes
  Wire.endTransmission();    // stop transmitting
  x++;
  delay(1);
}

For the slave, the code is simplified :

void setup() {
  Wire.begin(8);                // join i2c bus with address
  Wire.onReceive(receiveEvent); // register event
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany) {
    while(Wire.available()) Wire.read();
}

void loop() {
    // This is where I communicate with FONA 800 using SoftwareSerial
}

The problem is that the input from SoftwareSerial is altered when I2C is working. I am using SDA/SCL pins and a common GND for I2C and pin 2-3 for FONA. When a communication takes place in I2C, the interrupt is messing up the input from SoftwareSerial , receiving altered chars (garbage). If I send response to master (write), the output communication with SoftwareSerial is also destroyed, altering characters. (attering = changing, not adding or cutting characters)

I switch the FONA on Arduino Mega, using different pins - same result. Only using a Serial1 instead of a SoftwareSerial makes the those problems disappear.

If a decrease the speed for SoftwareSerial, the problems appears more rarely. So, the conclusion is that i cannot use Wire in conjunction with SoftwareSerial because SoftwareSerial will be messed up. I believe that under the hood Wire and SoftwareSerial are using someting common that makes interfere with each other. If you can see the I2C communication is intense to accentuate this behaviour, Also, I rarely miss a byte sent by master. How can I help you improve this behaviour?

cmaglie commented 5 years ago

Unfortunately SoftwareSerial, being a Serial "emulated" in software, suffers interruptions from other devices. There is little you can do about it. Also you can't predict when a new byte will arrive from serial so you can't do much to prevent corrupted readings (like temporarily disabling Wire for the time of serial communication for example).

BTW there is an alternative software serial library called ALTSoftSerial (made by @PaulStoffregen), you can install it via Library Manager. This library is not pure-software as the original SoftwareSerial but use an hardware Timer of the AVR chip to generate precise timings, this may solve your issue.