GPS and LoRa modules not working together

Hi everyone,
I’ve been working on a project that uses a u-blox GPS NEO6MV2 with a NiceRF 2AD66 LoRa device which uses a SX1276 chip. Using the GPS and the LoRa separately they work just fine with the example programs of their libraries (LoRa from sandeepmistry, TinyGPS and TInyGPS++ from mikalhart), even though the GPS had trouble finding a fix sometimes. However, when I used both at the same time, to transmit the gps coordinates, it simply did not work. I tried using a Teensy 4.0 and an Arduino Nano as microcontrollers. Does anyone have ideas for a solution?

Code using both at the same time:

#include <SPI.h>
#include <LoRa.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
/*
   This sample sketch demonstrates the normal use of a TinyGPS++ (TinyGPSPlus) object.
   It requires the use of SoftwareSerial, and assumes that you have a
   4800-baud serial GPS device hooked up on pins 4(rx) and 3(tx).
*/
boolean once = true;
float LAT = 0, LNG = 0;
long wtime = 0;
static const int RXPin = 4, TXPin = 3;
static const uint32_t GPSBaud = 9600;

// The TinyGPS++ object
TinyGPSPlus gps;

// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin);

int counter = 0;
void setup() {
  Serial.begin(9600);
  ss.begin(GPSBaud);

  Serial.println(F("DeviceExample.ino"));
  Serial.println(F("A simple demonstration of TinyGPS++ with an attached GPS module"));
  Serial.print(F("Testing TinyGPS++ library v. ")); Serial.println(TinyGPSPlus::libraryVersion());
  Serial.println(F("by Mikal Hart"));
  Serial.println();

  Serial.println("LoRa Sender");

  if (!LoRa.begin(915E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }

}

void loop() {
/*------------------------------Coleta de dados do GPS------------------------*/  
  while (ss.available() > 0)
    if (gps.encode(ss.read()))

  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println(F("No GPS detected: check wiring."));
    while(true);
  }

/*------------------------------Verificação de dados do GPS------------------------*/  
//Quantidade de satélites:

Serial.print(gps.satellites.value());

//LATITUDE+LONGITUDE 
  if (gps.location.isValid())
  {
    LAT = (gps.location.lat(), 6);
    Serial.print(F(","));
    LNG = (gps.location.lng(), 6);
    if(once == true){
    wtime = millis();
    once = false;
    }
    Serial.print(wtime);
  }
  else
  {
    Serial.println(F("INVALID LOCATION"));
  }

//DATA
  Serial.print(F("  Date/Time: "));
  if (gps.date.isValid())
  {
    Serial.print(gps.date.month());
    Serial.print(F("/"));
    Serial.print(gps.date.day());
    Serial.print(F("/"));
    Serial.print(gps.date.year());
  }
  else
  {
    Serial.print(F("INVALID DATA"));
  }
//TIME
    Serial.print(F(" "));
  if (gps.time.isValid())
  {
    if (gps.time.hour() < 10) Serial.print(F("0"));
    Serial.print(gps.time.hour());
    Serial.print(F(":"));
    if (gps.time.minute() < 10) Serial.print(F("0"));
    Serial.print(gps.time.minute());
    Serial.print(F(":"));
    if (gps.time.second() < 10) Serial.print(F("0"));
    Serial.print(gps.time.second());
    Serial.print(F("."));
    if (gps.time.centisecond() < 10) Serial.print(F("0"));
    Serial.print(gps.time.centisecond());
  }
  else
  {
    Serial.print(F("INVALID TIME"));
  }
    
 Serial.println();



  LoRa.beginPacket();
  LoRa.print("Packet number: ");
  LoRa.print(counter);
  LoRa.print(gps.satellites.value());
  if (gps.location.isValid())
  {
    LoRa.print(LAT);
    Serial.print(F(","));
    LoRa.print(LNG);
  }
  else
  {
    Serial.println(F("INVALID LOCATION"));
  }
   Serial.print(F("  Date/Time: "));
  if (gps.date.isValid())
  {
    Serial.print(gps.date.month());
    Serial.print(F("/"));
    Serial.print(gps.date.day());
    Serial.print(F("/"));
    Serial.print(gps.date.year());
  }
  else
  {
    Serial.print(F("INVALID DATA"));
  }
  LoRa.endPacket();

  counter++;
}

Well karma for posting the code, a lot of people dont realise its helpful.

However, whilst you probably knows what 'it simply did not work' means, I doubt anyone else does.

Also useful to tell us on what pins you have connected to the LoRa device.

What logic level conversion did you use when you connected the LoRa module to the Nano ?

I'll guess that you have a timer conflict. Software serial certainly uses one and the Arduino Lora library uses SPI which I think does too. I think that there's an AltsoftSerial library that uses a different one.

wildbill:
I'll guess that you have a timer conflict. Software serial certainly uses one and the Arduino Lora library uses SPI which I think does too. I think that there's an AltsoftSerial library that uses a different one.

SoftwareSerial does interact with SPI (and Serial.print()) in some, but not all circumstances, packet transmissions are normally OK.

But if you do get a timing issue its easy to deal with just use ss.begin() when you want to read the GPS and ss.end() when you have finished and want to use the LoRa.

srnet:
Well karma for posting the code, a lot of people dont realise its helpful.

However, whilst you probably knows what 'it simply did not work' means, I doubt anyone else does.

Also useful to tell us on what pins you have connected to the LoRa device.

What logic level conversion did you use when you connected the LoRa module to the Nano ?

Sorry, I forgot to mention it, but what I meant by "not working" was: the GPS was incapable of finding a fix while the LoRa was sending a packet, saying "INVALID LOCATION" like the program tells to (I kept the GPS ON for a very long time, so I don't think that is the problem), and when the GPS found a fix in of the tests, the LoRa stopped sending packets. The weird thing is that no error message was printed.

About the pins used with the LoRa device, I connected LoRa's MISO, MOSI, CLK and CS to arduino's D12, D11, D13 and D10 in order, and I did not use any LoRa's DI pin (DI0, DI1 and DI2) . However I used the same pins for the receiver modules and it worked just fine.
I want to mention as well that while using Teensy 4.0 board, I did try it out several of the TX and RX ports with the GPS.

And I did not make any logic level conversion.

hanashiro:
And I did not make any logic level conversion.

Bad, you need to fix that.

I wish you luck with that application, there are a numbe of issues with the logic.

A common mistake, which I see often, is this;

if (gps.encode(ss.read()))

And if thats true the program goes off and does a heap of stuff. Problem is that although a sentence might of been encoded, you dont know if the fix (location) has been updated. Lots of sentences dont contain fix information.

if (gps.location.isValid())

So if the GPS obtains one fix, but then stops working, it will always think it has a valid fix.

You really need to be checking the GPS in this way;

if (GPSserial.available() > 0)
    {
      GPSchar = GPSserial.read();
      gps.encode(GPSchar);
    }

    if (gps.location.isUpdated() && gps.altitude.isUpdated())
    {
      GPSfix = true;
      Serial.print(F("Have GPS Fix "));
      //do lots of useful new stuff
     }

You also do not appear to have picked up that the library your using needs to read the DIO0 pin of the LoRa device.

If you want an example of a fully working LoRa\GPS tracker code look here in \examples\SX127x_examples\tracker;