title: Arduino doesn't write to SD card?
body: <h2>Problem</h2>
  
  <p>I have an Arduino on an <a href="https://learn.adafruit.com/adafruit-feather-m0-bluefruit-le/overview" rel="nofollow noreferrer">Adafruit feather mo</a>. I am attempting to store data on an <a href="https://www.adafruit.com/products/2922" rel="nofollow noreferrer">adalogger</a>. The idea is simple. I have a potentiometer and I wish to write data from that potentiometer to an SD card. I can create, open, and close a CSV file, but I cannot write data from the potentiometer to the CSV file on the SD card.</p>
  
  <p>Very rarely, I can get data from the sensor to write into the CSV file. However, I am not convinced this is a hardware issue, because I can see all the data in COM using <code>Serial.println()</code> just like I want from the potentiometer. It just won't write to the SD card. Can someone point out what I could be going wrong?</p>
  
  <h2>Code</h2>
  
  <p>This code works:</p>
  
  <pre><code>**Initalize Stuff** 
  //Project Dependencies 
  #include &lt;Wire.h&gt;
  #include &lt;SPI.h&gt;
  #include &lt;SD.h&gt;
  #include &lt;string.h&gt;
  #include "RTClib.h"
  
  // the setup function runs once when you press reset or power the board
  //Global variables, usually associated with physical assets on the board
  Sd2Card card;
  SdVolume volume;
  SdFile root;
  char filename[13] = "";
  File logfile;
  int potPin = 2; //select the input pin for the potentiometer 
  const int chipSelect = 10;//chip select for SPID 
  File dataFile;
  File sensorData;
  bool fileOpen = false;
  
  void setup() {
    //Step 1: Initialize Serial
    //================================================================
    Serial.begin(57600);
    //Step 1: Initialize the SD Card
    //================================================================
    //Step 1: Initialize the SD Card
    //================================================================
    Serial.println("Initializing SD card...");
    while (!SD.begin(chipSelect)) {
      // see if the card is present and can be initialized:
      Serial.println("...Card failed, or not present");
      delay(2000);
    }
    Serial.println("...Success.");
    if (card.init(SPI_HALF_SPEED, chipSelect))
      Serial.println("...Card initialized.");
    //// print the type of card
      Serial.print("\n...Card type: ");
      switch (card.type()) {
        case SD_CARD_TYPE_SD1:
          Serial.println("SD1");
          break;
        case SD_CARD_TYPE_SD2:
          Serial.println("SD2");
          break;
        case SD_CARD_TYPE_SDHC:
          Serial.println("SDHC");
          break;
        default:
          Serial.println("Unknown");
      }
      sprintf(filename, "%02d%02d%02d%02d.csv", now.month(), now.day(), now.hour(), now.minute());
      Serial.print("Initializing date file: ");
      Serial.println(filename);
      //Step 2: Create and open new logfile
      if (SD.exists(filename)) {
        Serial.print("...file ");
        Serial.print(filename);
        Serial.println(" already created before...Success.");
      } else {
        //File not found, so create it.
        dataFile = SD.open(filename, FILE_WRITE);
        if (!dataFile) {
          Serial.print("...Couldn't create ");
          Serial.println(filename);
        } else {
          Serial.print("...Success opening ");
          Serial.println(filename);
          Serial.print("...Starting size: ");
          Serial.print(dataFile.size());
          Serial.print(" bytes");
        }
        dataFile.close();
      }
    }
  </code></pre>
  
  <p><strong>Loop</strong></p>
  
  <p>This code doesn't work:</p>
  
  <pre><code>// the loop function runs over and over again until power down or reset
  void loop() {
    int WriteEnabled = digitalRead(5); //This is when I turn a switch on to record, this works
    if (WriteEnabled) {
      sensorData = SD.open(filename, FILE_WRITE);
      if (sensorData) {
        uint32_t value = analogRead(potPin);
        //char line[27] = ""; //Define my input line
        //sprintf(line, "%04d/%02d/%02d, %02d:%02d:%02d, %04d", now.year(), now.month(), now.day(), now.hour(), now.minute(), now.second(), value); //Doesn't work either, why?
        String dataString = String(value);
        sensorData.println(dataString);//This doesn't work
        Serial.println(dataString);
      }
    }
    if (!WriteEnabled &amp;&amp; sensorData) {
      sensorData.close();
    }
  }
  </code></pre>
  
  <h2>References</h2>
  
  <p>I have attempted to fix the problem by consulting several resources. Similar questions on Stack Overflow do not answer my problem.</p>
  
  <ul>
  <li><a href="https://www.arduino.cc/en/Tutorial/Datalogger" rel="nofollow noreferrer">Arduino SD Documentation</a> </li>
  <li><a href="https://stackoverflow.com/questions/35121679/arduino-not-writing-to-sd-card">Arduino not writing to the SD card</a></li>
  <li><a href="http://www.arduino.org/learning/reference/sd-println" rel="nofollow noreferrer">SD Library API Documentation</a> </li>
  <li>Tried <code>String</code> and <code>sprint</code>. Should I try <code>flush</code>? But that doesn't match the documentation for SD library. Frustrating. </li>
  <li>Update: Just tried using <code>sensorData.flush()</code> after <code>println()</code> command. Didn't work. </li>
  </ul>
  
  <p>I have changed writing to the SD card by using <code>String()</code> and also <code>sprint</code> to help define the data. Nothing is working. I don't think it is a hardware problem because I can open and close a file, but just not write any data. Can someone please help me out? If someone suspects hardware, please let me know why. I really doubt it because why can I create a file on SD card and see potentiometer values in COM but not store them?</p>