Error switching between multiple serial ports using GPS and an SD card

Hi guys,

I’m working on a school project in which I’m designing a weather balloon payload. I’m currently using an arduino nano along with an lm135 temperature sensor, an adxl335 accelerometer, an HlH-4030 humidity sensor, MPX4115 pressure sensor, and an FGPMMOPA6H GPS standalone module with an attena. As you may have guessed, I’m basically recording atmospheric data and graphing it.

Currently, I have just begun the phase of the design where I’m implementing the GPS unit, and I’ve run into my first roadblock. To record all of my sensor data, I’m writing the data to an SD card, and I’ve something funky is going on with SoftwareSerial.

Below, is my code for this project, and I’ve done a lot of testing to try and pinpoint the problem. First off, I know it’s not the hardware because when I print the GPS data to the built in serial monitor instead of the SD card, I have no issues whatsoever. Secondly, my SD card is recording the sensor data excluding the GPS just fine. The problem occurs when I try to print both like I’m doing below.

Ultimately I’d like to parse the GPS data for the $GPGGA identifier and its information, but I’ve chosen to just code the basics for now and worry about that later. As far as the errors I’ve been getting go, there have been too many inconsistencies to choose just one. I’ve gotten text documents full of sanskrit looking characters like an image converted to text, data that looks close to correct except the GPS data will stop recording after some time x, and even data where the title info in my setup loop has been repeated multiple times like the arduino reset itself for no reason.

I’m convinced that the error is in my code though because the serial port will print out things correctly. I’m just looking for suggestions or fixes, so I can test some things out and hopefully fix this issue during my next lab period Tuesday.

Thanks for any help you can offer,
-Brendon

#include <SoftwareSerial.h>
SoftwareSerial sdcard(8,9); //RX,TX for the SD card
SoftwareSerial gps(6,7); //RX,TX for the gps
void setup() {
  // put your setup code here, to run once
  sdcard.begin(9600);
  sdcard.println("time,xcomponent,ycomponent,zcomponent,humidity,temperature,pressure");
  gps.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  gps.listen();
  delay(50);
  char GPSdata[400];
  for(int i = 0; i<400; ++i){
    GPSdata[i] = '\0';
  }
  int counter = 0;
  while(gps.available() > 0) {
    char incomingdata = gps.read();
    GPSdata[counter] = incomingdata;
    ++counter;
    delay(2);
    if(counter>399){
      break;
    }
  }
  float zcomponent = analogRead(0);
  float ycomponent = analogRead(1);
  float xcomponent = analogRead(2);
  float humidity = analogRead(4);
  float temperature = analogRead(5);
  float pressure = analogRead(3);
  sdcard.listen();
  delay(50);
   for(int j = 0; j<=counter; ++j){
    sdcard.print(GPSdata[j]);
  }
  sdcard.print('\n');
  sdcard.print(millis());
  sdcard.print(",");
  sdcard.print(xcomponent);
  sdcard.print(",");
  sdcard.print(ycomponent);
  sdcard.print(",");
  sdcard.print(zcomponent);
  sdcard.print(",");
  sdcard.print(humidity);
  sdcard.print(",");
  sdcard.print(temperature);
  sdcard.print(",");
  sdcard.println(pressure);
  

}

You have a Serial SD card shield? (Most common ones use a standard SPI interface). Which one is that??

Using 2 software serials at the same time is a challenge and you need to be extra careful not to loose data or lock up things for too long. You have a hardware Serial on your unit you won't be using to connect to your USB once the balloon is up in the air - why not attach the GPS unit to the hardware Serial directly? (needs to be disconnected while you upload code); that will provide much better reliability for your code and remove the challenge of using 2 software serials objects (Serial: 0 (RX) and 1 (TX) will then be used to receive (RX) and transmit (TX) TTL serial data. These pins are connected to the corresponding pins of the FTDI USB-to-TTL Serial chip so that is why it's better to disconnect the GPS during code upload.)

The way you listen to your gps data will lead to disappointment quickly.... could really use some improvement - read Robin's post on Serial basics