GPS locator (GPS data writing to SD card)

Hi, I would like to ask you for elementary question.
I am using GPS Neo-6M module, Arduino Nano + SD card module.

And I am trying to get data from GPS, and write them in some specific format to the file on SD card.
I tried GPS and SD card separately (GPS was getting data and writing to serial monitor, program was writing some data to SD card). Now when I tried to glue it together...... It doesn't work.

What doesn't work?

  • I am not getting all data from GPS module anymore.
  • I am not able to set up how often I want to read data from GPS and save them. (I know that I cannot use Delay)

How often to receive data?

  • I want to be able decide how often I want to collect data about position and how often to save them depending of what I get from GPS, for example:
  • if my speed will be < 1 km/h => I want to ask GPS module for data after 60 sec.
  • If my speed is bigger than 1 km/h I want to ask for data every 30 seconds.
  • if my speed is bigger than 50 km/h I want receive and save data every 5 seconds

Thank you for your help of advice. Connecting more devices is something new for me and communication between devices is something what I didn't handle well :frowning:

#include <TinyGPS++.h>           // Include TinyGPS++ library
#include <SoftwareSerial.h>      // Include software serial library
#include <SPI.h>
#include <SD.h>

int PositiveLED = 8;
int ErrorLED = 7;
 
TinyGPSPlus gps;
File myFile;
 
#define S_RX    5                // Define software serial RX pin
#define S_TX    4                // Define software serial TX pin
 
SoftwareSerial SoftSerial(S_RX, S_TX);    // Configure SoftSerial library

String LatitudeString;
String LongitudeString;
String AltitudeString;
String SpeedString;
String TimeString;
String DateString;
String SatellitesString;

 
void setup(void) 
{
  pinMode(8, OUTPUT); // diode
  pinMode(7, OUTPUT); // error diode 
  Serial.begin(9600);
  SoftSerial.begin(9600);  

  while (!Serial) 
  {
     ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print("Initializing SD card...");
  if (!SD.begin(10)) 
  {    
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

}

void loop() 
{
  while (SoftSerial.available() > 0) 
  {
    if (gps.encode(SoftSerial.read())) 
    {

      String Date = "";
      if (gps.date.isValid()) 
        Date = "Date: " + String(gps.date.year()) + "-" + String(gps.date.month()) + "-" + String(gps.date.day());
  
      String Time = "";
      if (gps.time.isValid()) 
        Time = "Time (GMT): " + String(gps.time.hour()) + ":" + gps.time.minute() + ":" + gps.time.second();

      String Speed = "";
      if (gps.speed.isValid()) 
        Speed = "Speed: " + leadSpaceFourPlusDecimals(gps.speed.kmph(),2) + " km/h";          

     String Precision = "";
     if (gps.hdop.isValid()) 
        Precision = "Prec.: " + leadZeroSpecificDigits(gps.hdop.value(),3);
     
     String LatLog = "";
     if (gps.location.isValid()) 
        LatLog = "Latitude: " + leadSpaceFourPlusDecimals(gps.location.lat(),6) +";" + " Longitude: " + leadSpaceFourPlusDecimals(gps.location.lng(),6);
    
    
     String Message = Date + ";" + Time  + "; " + LatLog + "; " + Speed + "; " + Precision;
     Serial.println(Message);
     
     addLineToFile("Gps.txt", Message);
    }
  }
}

void addLineToFile(char filename[], String line)
{
  
  Serial.println("Try to open file");
  myFile = SD.open(filename, FILE_WRITE);
  if (myFile) 
  { 
    digitalWrite(8,HIGH); // positive signal of writing
    Serial.println("File open!");
    myFile.println(line);
    Serial.println("Wrote: "+line);
    digitalWrite(8,LOW); 
  }
  else
  {
    digitalWrite(7,HIGH); // Negative signal of writing
    Serial.println("Cannot open file");
    digitalWrite(7,LOW);
  }
  myFile.close();
  Serial.println("File closed.");
  
}


String leadZeroSpecificDigits(int input, int digits)
{
    String result = "";
    if(input < 10)
        result = "00";
    if(input >= 10 && input < 100)
        result = "0";
        
    result = result + input;
    return result;
}

String leadZeroTwoDigits(int input)
{
    String result = "";
    if(input < 10)
        result = "0";
    result = result + input;
    return result;
}

String leadSpaceFourPlusDecimals(double input, int decimal)
{
    String result = "";
    if(input >= 0)
      result = " ";

    if(input < 100 || input > -100)
      result = result + " ";

    if(input < 10 || input > -10)
      result = result + " ";

    result = result + String(input, decimal);
    return result;   
}

2020-11-22 03_11_35-COM6.png

2020-11-22 03_11_35-COM6.png

I haven't looked at your code, but when a microSD module and another SPI device both work separately, but not together, it's usually because the typical microSD module doesn't release the MISO line properly when its CS line is not active. If your SD module looks like the attached pic, that's probably the problem. The pic also shows the solution.

Things you need to change;

Dont use Strings on a nano.

Dont use gps.encode() and assume there is new data at ever NMEA senetence.

Also the way you are using gps.encode() can mean that you wont get any updated data from the GPS.

Continuing the discussion from GPS locator (GPS data writing to SD card):

Hi, sorry pick this up, cause it is exactly happened to me.
I modified the SD reader still no touch function for my ILI9341, wonder if any new progress?

Thanks.

Topics merged and old topic reopened for continuation of discussion

1 Like

thanks.
I just wonder if there is program solution?

I don't know anything about your display. But are you saying the touch function works if the SD module is not connected?

1 Like

Thank you ShermanP
unit: ESP32 DEV+ILI9341+SD reader;
all of them works well separately, include touch function of 9341 with TFT_ESPI.h
no touch function when used them together, no SD file open.

And does it work if the SD module is connected, but no SD card is inserted?

Edit: Does that display module have an SD socket on the back? If so, I assume it's wired correctly.