RadioHead RF22 interferes with Serial communication

Dear all,

I have a sketch running on Arduino Uno 328, Duemilanove 328, Pro Mini 328 3.3V 8MHz and on a standalone 328P on a custom board with 3.3V and 8MHz (just on different boards to test and see I can replicate the issue on all of them). The project uses a GPS sensor and a RFM22B radio module. In order to make use of the radio module, I use the RF22 Library of RadioHead.

The GPS is connected via serial to the microcontroller. It is delivering a constant stream of data at 9600 baud. The sketch reads and interprets the data continuously and sends a very limited set of data out via the radio module every 5 seconds.

Unfortunately, I can see that whenever I do send data via the RadioHead library like below, the chance that I lose some data from the incoming serial string of the GPS increases significantly. If I comment out the rf22.send() commands, I have 100% correct data from the GPS. If I do send, I get 1 to 2 bytes wrong every now and then (i.e. it affects the incoming bytes at the time of sending, but it does not happen every time). Let’s say it happens about 40% of the time that something gets lost/mixed up in the serial buffer. For the serial communication I tried hardware serial, SoftSerial as well as NeoSWSerial (with buffer and interrupts) and I get the exact same behaviour/data quality with all of them.

Again, I am 99.99% sure it has something to do with this radio transmission part. I can’t for sure say it is the library though - do you think it could be electrical interference? Do you know of any interference of the library with something that is used for Serial communication?

Thanks for any support!

Here’s how I send data:

    if (rf22.send(data1, sizeof(data1))) {
      if (debug) {
        Serial.println("Success!");
      }
    } else {
      if (debug) {
        Serial.println("Error");
      }
    }

    rf22.waitPacketSent();

Here’s an example of a mixed uf message. I x-ed out the GPS coordinates:

$GPGGA,202908.000,xx24.4392,N,00x32.⸮799,E,1,06,1.2,527.0,M,0.0,M,,*67

Don't expect full answers unless you post the full code.

Thank you. I just thought it would be more efficient to show you the code that seems to generate the issues, rather than posting 500 lines of code and let you search through the important parts.

Here’s short sketch that shows the same issue. Every 5 seconds, the chance is there that one or two bytes are wrong. It does not happen every time, and it only happens shortly after sending (when the buffer is cleared that filled up during sending). It never happens in between.

#include <SPI.h>
#include <RH_RF22.h>
#include <NeoSWSerial.h>

RH_RF22 rf22;
NeoSWSerial gps_serial(5, 4);

unsigned long start;

void setup() {

  Serial.begin(9600);
  gps_serial.begin(9600);

  rf22.init();

}

void loop() {

  while (gps_serial.available() > 0) {
    Serial.print(gps_serial.read());
  }

  if (millis() > start + 5000) {
    rf22.send('test', 4);
    rf22.waitPacketSent();
    start = millis();
  }
}

I just thought it would be more efficient to show you the code that seems to generate the issues

That's the problem in a nutshell. You don't actually know what is causing the problem and posting a snippet of code does not allow it to be seen in context. You have done exactly the right thing by producing a small program that demonstrates the problem. It would help, of course, if it compiled :slight_smile:

Do you mean it doesn’t compile for you? I works for me on the Uno and it runs, producing the error…

Edit: Arduino 1.8.5

Sketch uses 9784 bytes (30%) of program storage space. Maximum is 32256 bytes.
Global variables use 521 bytes (25%) of dynamic memory, leaving 1527 bytes for local variables. Maximum is 2048 bytes.

GoingForGold:
Do you mean it doesn't compile for you? I works for me on the Uno and it runs, producing the error..

Edit: Arduino 1.8.5

Sketch uses 9784 bytes (30%) of program storage space. Maximum is 32256 bytes.

Global variables use 521 bytes (25%) of dynamic memory, leaving 1527 bytes for local variables. Maximum is 2048 bytes.

As posted it won't compile. Where does the loop() function end ? Come to that, where does the if statement code block end ?

let’s try again :slight_smile:

#include <SPI.h>
#include <RH_RF22.h>
#include <NeoSWSerial.h>

RH_RF22 rf22;
NeoSWSerial gps_serial(5, 4);

unsigned long start;

void setup() {

  Serial.begin(9600);
  gps_serial.begin(9600);

  rf22.init();

}

void loop() {

  while (gps_serial.available() > 0) {
    Serial.print(gps_serial.read());
  }

  if (millis() > start + 5000) {
    rf22.send('test', 4);
    rf22.waitPacketSent();
    start = millis();
  }
}

I just realized that I might need to specify the issue a little more so I am not misunderstood:

The garbled serial data is INCOMING to the arduino. So it is the one from the GPS to the Arduino, NOT from Arduino to PC!

Any help is appreciated :slight_smile: