SD file will not open/write

I am super new and looking for help.

I have an uno with a micro SD module and a moisture sensor. I found a small 2G micro SD card, and everything initializes fine, I used the built-in cardinfo to verify the SD.

But when I try to open/write to the file it doesn't work. I've used the built-in datalogger as well and it still kicks back errors.

I'm having trouble figuring out what else to look for. I've tried fixing my code, using different methods that I've found on youtube, but nothing is working so far. Any ideas would be awesome. Thanks guys.

#include <SPI.h>
#include <SD.h>

File dataFile;//Create the file object
String fileName = "test.csv";//Name the file
const int chipSelect = 10;

//first calibrate dry sensor value and pure water. We might want to consider calibrating under extremely dry conditions if we want more accuracy in drier values. 
const int dry = 580; // value for dry sensor
const int wet = 300; // value for wet sensor
void setup()
 { 
   Serial.begin(9600);//this is the baud rate, don't change
   while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


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

  // 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:
    while (1);
  }
  Serial.println("card initialized.");
  if (SD.exists(fileName)) //check for existing or duplicate file names
   {
    Serial.println("File exists");
   }
   else
  Serial.println("File does not exist");

  dataFile = SD.open(fileName,FILE_WRITE);//Create the file
  dataFile.close();
  
  // if the file was created successfully, open and write to it:
  if (dataFile) {
    Serial.println("File opened ok");
    // print the headings for our data
    dataFile.println("Time,Percent Moisture,Reading");
    Serial.println("Time,Percent Moisture,Reading");
 dataFile.close();
 }
 }
 
 void loop()
 {
  int sensorVal = analogRead(A1);
  int percentMoisture = map(sensorVal, wet, dry, 100, 0); 

//Write data to file
  if (dataFile) {
    dataFile.print(percentMoisture);
    dataFile.print("%");
    dataFile.print(",");
    dataFile.println(analogRead(A1));
    
    Serial.print(percentMoisture);
    Serial.print("%");
    Serial.print(",");
    Serial.println(analogRead(A1));//A0 is where we have the AOUT plugged in, change this if you move the wire to a new location

  dataFile.close(); //Close the file
  }
  else
  Serial.println("OOPS!! SD card writing failed");
  
delay(5000);//this is the delay between readings in milliseconds
}

Serial monitor output:

the file is closed

Hi!

I have an idea, but I´m unable to test code right now. Your IF statements all have brackets, but your ELSE statements don´t... See an example

Shouldn´t it be:

if (SD.exists(fileName)) //check for existing or duplicate file names
   {
    Serial.println("File exists");
   }
   else
   {
  Serial.println("File does not exist");
  dataFile = SD.open(fileName,FILE_WRITE);//Create the file
  dataFile.close();
   }

PS: There´s no need to close the file if you´ll be writing to it right after
PS2: in your void loop() function you´re not opening the file again, though you´re closing it (and you closed it other 2 times before, at setup)

I think it isn't even creating the file somehow. I used the built-in data logger to test it, and it still doesn't work.

I went through and removed some of the close commands. I even tried removing the if statement all together to see if it would just open and write, and nothing showed up. I checked the card again and it can't find any files, so it seems its not even creating the file in the first place.

How did you format the SD card? Your initial post shows that the sketch thinks it is FAT16.

Just off the top if my head, but do the SD library functions take a filename of type String? Maybe they do as you don't mention any compilation errors.

After writing, you must close the file, or it will appear to be empty.

Pick a working example from the SD library and study it carefully, before just making stuff up and hoping it will work.

do the SD library functions take a filename of type String?

No. Use C-strings or character constants.

Are you saying the CardInfo example in the SD.h library works ok, but the Datalogger example doesn't work? Do you have the CS line set to D4 in both cases? What error do you get in the serial monitor when Datalogger runs?

The cardinfo sketch in the example library says the card is correctly formatted to FAT16, there is a built in error message if the card is not FAT formatted.

And I would assume the library recognizes spring type because that is what is used in the example datalogger.

Correct, the cardinfo sketch works great. The card initializes and is formatted correctly. The datalogger does not work though, same issue, will not open file to write. The only thing I changed in the example was my CS pin for the card reader.

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