1

I'm a beginner in Arduino world and I want to create a simple SD datalogger that works with GPS (module NEO-6M) and light sensor. I know there are a number of tutorials on GPS logging including:

but my problem is that those tutorials are either too complex (i.e. include extra stuff like displays) or they don't use TinyGPS or TinyGPS++ library. In the first step I've managed to get GPS working and displaying the result on my laptop screen (via Serial.print()) using example code from TinyGPS library. In the next step I managed to use tutorial for Adafruit Data Logger to log data on SD card. The problem: light sensor logs ok, but for GPS it only logs a strings of 99.999999. Tried various tweaks but so far not sure what I got wrong. I use Arduino Uno, NEO-6M GPS, Catalex MicroSD Card Adapter, simple LDR. Here is my current code:

    #include <TinyGPS++.h>
    #include <SoftwareSerial.h>
    #include "SD.h"

    //input for the light detecting sensor (LDR)
    int LDR = A3;   

    //inputs and triggers for GPS
    TinyGPSPlus gps;
    SoftwareSerial ss (7, 8); // TX=7, RX=8 

    // digital pin 4 for the SD cs line
    const int chipSelect = 4;
    File logfile;// the logging file

    //data logger timing and other 
    #define LOG_INTERVAL  1000 // mills between entries
    #define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
    uint32_t syncTime = 0; // time of last sync()
    #define ECHO_TO_SERIAL   1 // echo data to serial port
    #define WAIT_TO_START    0 // Wait for serial input in setup()    

    void setup() {
      Serial.begin(9600);
      ss.begin(9600);
      Serial.println();

      #if WAIT_TO_START
        Serial.println("Type any character to start");
        while (!Serial.available());
      #endif //WAIT_TO_START

      // initialize the SD card
      Serial.print("Initializing SD card...");

      pinMode(10, OUTPUT);

      // see if the card is present and can be initialized:
      if (!SD.begin(chipSelect)) {
        Serial.println("Card failed, or not present");
        // don't do anything more:
        return;
      }
      Serial.println("card initialized.");

      // create a new file
      char filename[] = "LOGGER00.CSV";
      for (uint8_t i = 0; i < 100; i++) {
        filename[6] = i/10 + '0';
        filename[7] = i%10 + '0';
        if (! SD.exists(filename)) {
          // only open a new file if it doesn't exist
          logfile = SD.open(filename, FILE_WRITE); 
          break;  // leave the loop!
        }
      }

      Serial.print("Logging to: ");
      Serial.println(filename);

      logfile.println("date,time,lat,long,light");

      #if ECHO_TO_SERIAL
        Serial.println("lat,log,date,time,altitude,light");
      #endif// attempt to write out the header to the file
    }

void loop(void) {  

   // delay for the amount of time we want between readings
   delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));

   //here is GPS data (if I got it right)
   while (ss.available() > 0)
   gps.encode(ss.read());
   logfile.print(gps.date.value());
   logfile.print(", ");
   logfile.print(gps.time.value());
   logfile.print(", ");
   logfile.print(gps.location.lat(), 6);
   logfile.print(", ");
   logfile.print(gps.location.lng(), 6);
   logfile.print(", ");
   #if ECHO_TO_SERIAL
      Serial.print(gps.date.value());
      Serial.print(", ");  
      Serial.print(gps.time.value());
      Serial.print(", ");  
      Serial.print(gps.location.lat(), 6);
      Serial.print(", ");  
      Serial.print(gps.location.lng(), 6);
      Serial.print(", ");  
   #endif

   //Read light intensity
   float lux = 500/(10.72/(5-(photocellReading * 0.00488759))*(photocellReading * 0.00488759));
   logfile.println(lux);
   #if ECHO_TO_SERIAL
      Serial.println(lux);
   #endif //ECHO_TO_SERIAL

   //write data to disk
   if ((millis() - syncTime) < SYNC_INTERVAL) return;
   syncTime = millis();
   logfile.flush();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.