Closed OpreaFlorin closed 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.
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:
For the slave, the code is simplified :
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?