Issue regarding data logging into SD card from DHT 11 sesnor and NEO 6M GPS module

Hi....I wanted to store data on the SD card from DHT 11 and NEO 6M GPS module sensors. The problem is that the code I am using shows proper values in the serial monitor for all the parameters (LAT, LONG, Date, Time, Temperature, Humidity) but only LAT and LONG values get stored in the SD card.
The Temperature and humidity values are stored as 0.00. The data and time values are stored as nothing ( completely blank). The data gets stored in SD in a proper manner when SD card and GPS is used. It is also working properly when DHT sensor and SD card are used. But when the three of them are used combinedly the problem arises while storing the data only. I am attaching the code for the reference.

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <dht.h> // Include library
#define outPin 7 // Defines pin number to which the sensor is connected
dht DHT; // Creates a DHT object
#include <SD.h>
#include <SPI.h>
const int chipSelect = 4;

// Choose two Arduino pins to use for software serial
int RXPin = 3;
int TXPin = 2;

int GPSBaud = 9600;

// Create a TinyGPS++ object
TinyGPSPlus gps;

// Create a software serial port called "gpsSerial"
SoftwareSerial gpsSerial(RXPin, TXPin);

//long LAT, LONG;
float LAT,LONG;
float t, t1, h1;
String my_Str, my_SStr;

void setup()
{
// Start the Arduino hardware serial port at 9600 baud
Serial.begin(9600);

// Start the software serial port at the GPS's default baud
gpsSerial.begin(GPSBaud);

Initialize_SDcard();
}

void loop()
{
// This sketch displays information every time a new sentence is correctly encoded.
while (gpsSerial.available() > 0)
if (gps.encode(gpsSerial.read()))
displayInfo();
//Write_SDcard();

// If 5000 milliseconds pass and there are no characters coming in
// over the software serial port, show a "No GPS detected" error
if (millis() > 5000 && gps.charsProcessed() < 10)
{
Serial.println("No GPS detected");
while(true);
}
}

void displayInfo()
{
int h, m,s,c, Day,Month, Year;

String my_Str; String my_Str1; String my_Str2; String my_Str3; String my_Str4;
String my_SStr; String my_SStr1; String my_SStr2; String my_SStr3; String my_SStr4; String my_SStr5;
int readData = DHT.read11(outPin);

float t = DHT.temperature;        // Read temperature
    float t1;                            // read temperature in Fahrenheit
float h1 = DHT.humidity;           // Read humidity

{

if (gps.location.isValid())
{
Serial.print("Latitude: ");
//Serial.println(gps.location.lat(), 6);
LAT = gps.location.lat();
Serial.println(LAT);
Serial.print("Longitude: ");
//Serial.println(gps.location.lng(), 6);
LONG = gps.location.lng();
Serial.println(LONG);
Serial.print("Altitude: ");
Serial.println(gps.altitude.meters());
Serial.print("Speed in m/s = ");
Serial.println(gps.speed.mps());
//Serial.print("Number of satellites in use = ");
//Serial.println(gps.satellites.value());
}
else
{
Serial.println("Location: Not Available");
}
//Serial.print("Date: ");
if (gps.date.isValid())
{

Day=(gps.date.day());
my_Str1=String(Day);

Month=(gps.date.month());
my_Str2=String(Month);

Year=(gps.date.year());
my_Str3=String(Year);
my_Str4=String("/");
my_Str = my_Str1+ my_Str4  + my_Str2 + my_Str4 + my_Str3;

Serial.print("Date: ");
Serial.println(my_Str);

}
else
{
Serial.println("Not Available");
}

//Serial.print("Time(IST): ");
if (gps.time.isValid())
{

if (gps.time.hour() < 10) ;
h = gps.time.hour()+5;
my_SStr1=String(h);
//if (h < 10) Serial.print(F("0"));
//Serial.print(h);
//Serial.print(":");

if (gps.time.minute() < 10) ;    
m = gps.time.minute()+30;
my_SStr2=String(m);

if (gps.time.second() < 10);
s= gps.time.second();
my_SStr3=String(s);

if (gps.time.centisecond() < 10);
c = gps.time.centisecond();
my_SStr4=String(c);
my_SStr5=String(":");

my_SStr = my_SStr1+ my_SStr5  + my_SStr2 + my_SStr5 + my_SStr3 + my_SStr5 + my_SStr4;

Serial.print("Time (IST): ");
Serial.println(my_SStr);

}
else
{
Serial.println("Not Available");
}

delay(1500);
}

Serial.print("Temperature = ");
Serial.print(t);
Serial.print("°C | ");
t1=((t*9.0)/5.0+32.0); // Convert celsius to fahrenheit
Serial.print(t1);
Serial.println("°F ");
Serial.print("Humidity = ");
Serial.print(h1);
Serial.println("% ");
Serial.print("\n");

    Write_SDcard();

    Serial.println();
    Serial.println();
    delay(8000);

}

void Initialize_SDcard()
{
// 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;
}

// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
// if the file is available, write to it:
if (dataFile) {
dataFile.println("Date,Time, LATITUDE, LONGITUDE,Temperature in Cel, Temperature in F, Air Humidity"); //Write the first row of the excel file
dataFile.close();
}
}

void Write_SDcard()
{
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);

// if the file is available, write to it:
if (dataFile) {
dataFile.print(my_Str); //Store date on SD card
dataFile.print(","); //Move to next column using a ","

dataFile.print(my_SStr); //Store date on SD card
dataFile.print(","); //Move to next column using a ","

dataFile.print(LAT); //Store date on SD card
dataFile.print(","); //Move to next column using a ","

dataFile.print(LONG); //Store date on SD card
dataFile.print(","); //Move to next column using a ","

dataFile.print(t); //Store date on SD card
dataFile.print(","); //Move to next column using a ","

dataFile.print(t1); //Store date on SD card
dataFile.print(","); //Move to next column using a ","

dataFile.print(h1); //Store date on SD card
//dataFile.print(","); //Move to next column using a ","

 dataFile.println(); //End of Row move to next row
dataFile.close(); //Close the file

}
else
Serial.println("OOPS!! SD card writing failed");
}```

Hello nilu-2023

This is a nice project to get started.

Keep it simple and stupid firstly.
Run some tutorials for the hardware selected.
If you are happy with the results of the tutorials you can merge these to your project.

Have a nice day and enjoy coding in C++.

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <dht.h>        // Include library
#define outPin 7        // Defines pin number to which the sensor is connected
dht DHT;                // Creates a DHT object
#include <SD.h>
#include <SPI.h>
const int chipSelect = 4; 

// Choose two Arduino pins to use for software serial
int RXPin = 3;
int TXPin = 2;

int GPSBaud = 9600;

// Create a TinyGPS++ object
TinyGPSPlus gps;

// Create a software serial port called "gpsSerial"
SoftwareSerial gpsSerial(RXPin, TXPin);

//long LAT, LONG;
float LAT,LONG;
float  t, t1, h1;
String my_Str, my_SStr;

void setup()
{
  // Start the Arduino hardware serial port at 9600 baud
  Serial.begin(9600);

  // Start the software serial port at the GPS's default baud
  gpsSerial.begin(GPSBaud);

  Initialize_SDcard();
}

void loop()
{
  // This sketch displays information every time a new sentence is correctly encoded.
  while (gpsSerial.available() > 0)
    if (gps.encode(gpsSerial.read()))
      displayInfo();
    //Write_SDcard(); 

  // If 5000 milliseconds pass and there are no characters coming in
  // over the software serial port, show a "No GPS detected" error
  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println("No GPS detected");
    while(true);
  }
}

void displayInfo()
{
  int h, m,s,c, Day,Month, Year;
  
  String my_Str; String my_Str1; String my_Str2; String my_Str3; String my_Str4;
  String my_SStr; String my_SStr1; String my_SStr2; String my_SStr3; String my_SStr4; String my_SStr5;
  int readData = DHT.read11(outPin);

	float t = DHT.temperature;        // Read temperature
        float t1;                            // read temperature in Fahrenheit
	float h1 = DHT.humidity;           // Read humidity
  
  {

  if (gps.location.isValid())
  {
    Serial.print("Latitude: ");
    //Serial.println(gps.location.lat(), 6);
    LAT = gps.location.lat();
    Serial.println(LAT);
    Serial.print("Longitude: ");
    //Serial.println(gps.location.lng(), 6);
    LONG = gps.location.lng();
    Serial.println(LONG);
    Serial.print("Altitude: ");
    Serial.println(gps.altitude.meters());
    Serial.print("Speed in m/s = ");
    Serial.println(gps.speed.mps());
    //Serial.print("Number of satellites in use = ");
    //Serial.println(gps.satellites.value());
  }
  else
  {
    Serial.println("Location: Not Available");
  }
  //Serial.print("Date: ");
  if (gps.date.isValid())
  {
    
    Day=(gps.date.day());
    my_Str1=String(Day);
    
    Month=(gps.date.month());
    my_Str2=String(Month);
    
    Year=(gps.date.year());
    my_Str3=String(Year);
    my_Str4=String("/");
    my_Str = my_Str1+ my_Str4  + my_Str2 + my_Str4 + my_Str3;
   
    Serial.print("Date: ");
    Serial.println(my_Str);
  }
  else
  {
    Serial.println("Not Available");
  }

  //Serial.print("Time(IST): ");
  if (gps.time.isValid())
  {
    
   if (gps.time.hour() < 10) ;
    h = gps.time.hour()+5;
     my_SStr1=String(h);
    //if (h < 10) Serial.print(F("0"));
    //Serial.print(h);
    //Serial.print(":");
    
    if (gps.time.minute() < 10) ;    
    m = gps.time.minute()+30;
    my_SStr2=String(m);
    
    if (gps.time.second() < 10);
    s= gps.time.second();
    my_SStr3=String(s);
    
    if (gps.time.centisecond() < 10);
    c = gps.time.centisecond();
    my_SStr4=String(c);
    my_SStr5=String(":");
    
    my_SStr = my_SStr1+ my_SStr5  + my_SStr2 + my_SStr5 + my_SStr3 + my_SStr5 + my_SStr4;

    Serial.print("Time (IST): ");
    Serial.println(my_SStr);

  }
  else
  {
    Serial.println("Not Available");
  }

  delay(1500);
 }

  Serial.print("Temperature = ");
	Serial.print(t);
	Serial.print("°C | ");
	t1=((t*9.0)/5.0+32.0);        // Convert celsius to fahrenheit
  Serial.print(t1);
	Serial.println("°F ");
	Serial.print("Humidity = ");
	Serial.print(h1);
	Serial.println("% ");
	Serial.print("\n");


        Write_SDcard();
  
        Serial.println();
        Serial.println();
        delay(8000);
}

void Initialize_SDcard()
{
  // 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;
  }

   // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println("Date,Time, LATITUDE, LONGITUDE,Temperature in Cel, Temperature in F, Air Humidity"); //Write the first row of the excel file
    dataFile.close();
  }
}

void Write_SDcard()
{
    // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.print(my_Str); //Store date on SD card
    dataFile.print(","); //Move to next column using a ","

    dataFile.print(my_SStr); //Store date on SD card
    dataFile.print(","); //Move to next column using a ","

    dataFile.print(LAT); //Store date on SD card
    dataFile.print(","); //Move to next column using a ","

    dataFile.print(LONG); //Store date on SD card
    dataFile.print(","); //Move to next column using a ","

    dataFile.print(t); //Store date on SD card
    dataFile.print(","); //Move to next column using a ","

    dataFile.print(t1); //Store date on SD card
    dataFile.print(","); //Move to next column using a ","

    dataFile.print(h1); //Store date on SD card
    //dataFile.print(","); //Move to next column using a ","

     dataFile.println(); //End of Row move to next row
    dataFile.close(); //Close the file
  }
  else
  Serial.println("OOPS!! SD card writing failed");
}

As far as keeping it simple is concerned, the SD card and 1 sensor ( individual) work perfectly well. But now as per the requirement of my problem I have to combine all these ( GPS + DHT + SD card). Moroever, this code doesn't have any syntax errors or so it runs perfectly and also displays values in the serial monitor. But the data storage in SD card is an issue and we always know when we read the SD card later because while executing the entire code nothing is hampering. But the end desired result is very much frustrating

Hi, Its long time since this post last replied. Hope you have solved your issue.
The major problem is between multiple sensor(either I2C or Digital pin) and SD card. i am still trying to fix my code but no luck yet.
If you have solved the problem. Please provide solution to me too.

Don't know if the OP ever got his code working. His major problem is that he is declaring local variables in the displayInfo() function with the same names as global variables. All of the data except the latitude and longitude are stored in the local variables. The data prints to the Serial monitor correctly, because all the Serial.print statements use the local variables, but when the Write_SDCard() function is called, that function uses the global variables. The use of String variables is also likely to cause problems because of the limited amount of ram available.

I think you are right maybe earlier I was doing this mistake and not able to procure the result. Thanks for your suggestion. I already rectified my mistake and getting the desired result.

I have solved it. I will try to upload the corrected code after this weekend as I am outside now for this weekend.

Here is the workable code

#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <dht.h>        // Include library
#define outPin 7        // Defines pin number to which the sensor is connected
dht DHT;                // Creates a DHT object
#include <SD.h>
#include <SPI.h>
const int chipSelect = 4; 

// Choose two Arduino pins to use for software serial
int RXPin = 3;
int TXPin = 2;

int GPSBaud = 9600;

// Create a TinyGPS++ object
TinyGPSPlus gps;

// Create a software serial port called "gpsSerial"
SoftwareSerial gpsSerial(RXPin, TXPin);

float LAT,LONG;
int DAY, MONTH, YEAR, HOUR, MINUTE, SECOND;

float getTemp() {
         float t = DHT.temperature;
         return t;
}

float getHumidity() {
     float h = DHT.humidity;
         return h;
}

void setup()
{
  // Start the Arduino hardware serial port at 9600 baud
  Serial.begin(9600);

  // Start the software serial port at the GPS's default baud
  gpsSerial.begin(GPSBaud);

  Initialize_SDcard();
}

void loop()
{
  // This sketch displays information every time a new sentence is correctly encoded.
  while (gpsSerial.available() > 0)
    if (gps.encode(gpsSerial.read()))
      displayInfo();
    //Write_SDcard(); 

  // If 5000 milliseconds pass and there are no characters coming in
  // over the software serial port, show a "No GPS detected" error
  if (millis() > 5000 && gps.charsProcessed() < 10)
  {
    Serial.println("No GPS detected");
    while(true);
  }
}

void displayInfo()
{
  
  
 
  if (gps.location.isValid())
  {
    Serial.print("Latitude: ");
    Serial.println(gps.location.lat(), 6);
    //LAT = gps.location.lat();
    //Serial.println(LAT);
    Serial.print("Longitude: ");
    Serial.println(gps.location.lng(), 6);
    //LONG = gps.location.lng();
    //Serial.println(LONG);
    
    
    
  }
  else
  {
    Serial.println("Location: Not Available");
  }

if (gps.date.isValid())
{
 
    Serial.print("DAY: ");
    DAY = (gps.date.day());
    Serial.println(DAY);
   
    Serial.print("MONTH: ");
    MONTH = (gps.date.month());
    Serial.println(MONTH);

   Serial.print("YEAR: ");
    YEAR = (gps.date.year());
    Serial.println(YEAR);
}

else
  {
    Serial.println("Date: Not Available");
  }

if (gps.time.isValid())
  {
    
   if (gps.time.hour() < 10) Serial.print(F("0")) ;
    Serial.print("HOUR: ");
    HOUR = gps.time.hour();
    Serial.println(HOUR); 
    
    if (gps.time.minute() < 10) Serial.print(F("0")) ;
    Serial.print("MINUTE: ");
    MINUTE = gps.time.minute();
    Serial.println(MINUTE); 

   if (gps.time.second() < 10) Serial.print(F("0")) ;
    Serial.print("SECOND: ");
    SECOND = gps.time.second();
    Serial.println(SECOND); 
    
      

  }
  else
  {
    Serial.println("Time: Not Available");
  }

 delay(50);

float readData = DHT.read11(outPin);

Serial.print("Temp in C: ");
Serial.println(getTemp());

Serial.print("Humidity in %: ");
Serial.println(getHumidity());
Serial.print("\n");
Serial.print("\n");

Write_SDcard();
 delay(3000);
}

void Initialize_SDcard()
{
  // 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;
  }

   // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);
  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(" LATITUDE, LONGITUDE,DAY,MONTH,YEAR,HOUR,MINUTE,SECOND,Temperature in Cel,  Air Humidity in % "); //Write the first row of the excel file
    dataFile.close();
  }
}

void Write_SDcard()
{
    // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("LoggerCD.txt", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    

    //dataFile.print(LAT); //Store date on SD card
    dataFile.print(gps.location.lat(), 6);
    dataFile.print(","); //Move to next column using a ","

    //dataFile.print(LONG); //Store date on SD card
    dataFile.print(gps.location.lng(), 6);
    dataFile.print(","); //Move to next column using a ","

    dataFile.print(DAY); //Store date on SD card
    dataFile.print(","); //Move to next column using a ","

    dataFile.print(MONTH); //Store date on SD card
    dataFile.print(","); //Move to next column using a ","

    dataFile.print(YEAR); //Store date on SD card
    dataFile.print(","); //Move to next column using a ","
    
    dataFile.print(HOUR); //Store date on SD card
    dataFile.print(","); //Move to next column using a ","

    dataFile.print(MINUTE); //Store date on SD card
    dataFile.print(","); //Move to next column using a ","

    dataFile.print(SECOND); //Store date on SD card
    dataFile.print(","); //Move to next column using a ","

    dataFile.print(getTemp()); //Store date on SD card
    dataFile.print(","); //Move to next column using a ","

    dataFile.print(getHumidity()); //Store date on SD card
    //dataFile.print(","); //Move to next column using a ","

     dataFile.println(); //End of Row move to next row
    dataFile.close(); //Close the file
  }
  else
  Serial.println("OOPS!! SD card writing failed");
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.