SD Card not writing file despite successful initialization

Hi,

I have successfully made a weather station with a DHT11, RTC, and an SD card reader. The SD card initiates, however, it will not write to the card.

Can anyone see an issue with my code? Any help would be much appreciated.

JC

// DHT sensor library - Version: Latest
#include <DHT.h>
#include <DHT_U.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <DHT.h> // for the DHT sensor

//define DHT pin
#define DHTPIN 2     // what pin we're connected to

#define DHTTYPE DHT11   // DHT 11 

File myFile;

// initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);

RTC_DS1307 RTC;

void setup () {
  
  Serial.begin(9600);
while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

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



  
  
  //initializing the DHT sensor
  dht.begin();

  Serial.begin(9600);
  Wire.begin();
  RTC.begin();
  // Check to see if the RTC is keeping time.  If it is, load the time from your computer.
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // This will reflect the time that your sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
}
void loop () {
  DateTime now = RTC.now();
  float t = dht.readTemperature();
  float h = dht.readHumidity();

  if  (isnan(t) /*|| isnan(f)*/) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  

   myFile = SD.open("DATA.txt", FILE_WRITE);
   
  myFile.print(now.day(), DEC);  
  myFile.print('/');
  myFile.print(now.month(), DEC);
  myFile.print('/');
  myFile.print(now.year(), DEC);
  myFile.print(',');
  myFile.print(' ');
  myFile.print(now.hour(), DEC);
  myFile.print(':');
  myFile.print(now.minute(), DEC);
  myFile.print(',');
  myFile.print(' ');
  myFile.print("Temp: ");
  myFile.print(t);
  myFile.print(" *C");
  myFile.print(',');
  myFile.print(' ');
  myFile.print("Hum: ");
  myFile.print(h);
  myFile.println();

  Serial.print(now.day(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.year(), DEC);
  Serial.print(',');
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(',');
  Serial.print(' ');
  Serial.print("Temp: ");
  Serial.print(t);
  Serial.print(" *C");
  Serial.print(',');
  Serial.print(' ');
  Serial.print("Hum: ");
  Serial.print(h);
  Serial.println();



  delay(5000);
}

void loggingTemperature() {

  
  myFile = SD.open("DATA.txt", FILE_WRITE);
  if (myFile) {
    Serial.println("open with success");
  
  }
  myFile.close();
}

In loop(), after you write to the file, where do you ever close the file? Data is not actually written to the card until the file is closed. See the SD card notes page.

Do you know that, in order to use SPI as a master, pin 10 must be an set as an output even if it is not used as a chip select?

Thank you for your help Groundfungus,

I have tried adding the line myFile = SD.close(); to the loop but it gives giving me the following error:

‘class SDLin:SDClass’ has no member named ‘close’

Do you know what this means? I have also tried putting the myFile = SD.close(); after the myFile.print commands but still got the error.

Any hekp would be much appreciated.

JC

// DHT sensor library - Version: Latest
#include <DHT.h>
#include <DHT_U.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <DHT.h> // for the DHT sensor

//define DHT pin
#define DHTPIN 2     // what pin we're connected to

#define DHTTYPE DHT11   // DHT 11 

File myFile;

// initialize DHT sensor for normal 16mhz Arduino
DHT dht(DHTPIN, DHTTYPE);

RTC_DS1307 RTC;

void setup () {
  
  Serial.begin(9600);
while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.print("Initializing SD card...");

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

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



  
  
  //initializing the DHT sensor
  dht.begin();

  Serial.begin(9600);
  Wire.begin();
  RTC.begin();
  // Check to see if the RTC is keeping time.  If it is, load the time from your computer.
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    // This will reflect the time that your sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
  }
}
void loop () {
  DateTime now = RTC.now();
  float t = dht.readTemperature();
  float h = dht.readHumidity();

  if  (isnan(t) /*|| isnan(f)*/) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  

   myFile = SD.open("DATA.txt", FILE_WRITE);
   myFile = SD.close();
   
  myFile.print(now.day(), DEC);  
  myFile.print('/');
  myFile.print(now.month(), DEC);
  myFile.print('/');
  myFile.print(now.year(), DEC);
  myFile.print(',');
  myFile.print(' ');
  myFile.print(now.hour(), DEC);
  myFile.print(':');
  myFile.print(now.minute(), DEC);
  myFile.print(',');
  myFile.print(' ');
  myFile.print("Temp: ");
  myFile.print(t);
  myFile.print(" *C");
  myFile.print(',');
  myFile.print(' ');
  myFile.print("Hum: ");
  myFile.print(h);
  myFile.println();
 
  

  Serial.print(now.day(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.year(), DEC);
  Serial.print(',');
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(',');
  Serial.print(' ');
  Serial.print("Temp: ");
  Serial.print(t);
  Serial.print(" *C");
  Serial.print(',');
  Serial.print(' ');
  Serial.print("Hum: ");
  Serial.print(h);
  Serial.println();



  delay(5000);
}

void loggingTemperature() {

  
  myFile = SD.open("DATA.txt", FILE_WRITE);
  if (myFile) {
    Serial.println("open with success");
  
  }
  myFile.close();
}

You close files, not SD cards.

  myFile.close();

Thanks Paul, that worked perfectly.

jc