Log Data Whilst SD In

Hi All,

I'm trying to do something seemingly simple but I get the following error every time I insert the card.

  • initialization failed!
  • Couldn't create file...

The line below gives me 1 when inserted 0 when it isn't.

reading = digitalRead(sdInPin);

What am I doing wrong?

Thanks in advance,
Charles

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

// --------- GENERAL -----------

int sampleTime = 10;
long int previous = 0;
long int startTime = 0;
int sdInPin = 2;
boolean starting = true;
boolean reading = false;

File dataFile;
const int chipSelect = 8;

void setup() {
  Serial.begin(115200);
}


void loop() {

  reading = digitalRead(sdInPin);

  if (reading) {

    if (starting) {
      Serial.print("Starting: ");
      Serial.println(starting);
      loadSd();
      startTime = millis();
      starting = false;

    }

    if (millis() - previous > sampleTime) {

      String dataString = "";

      // Add all the data to dataString

      Serial.println(dataString);
      dataFile.println(dataString);
      dataFile.flush();

      previous = millis();
    }
  } else if (starting == false) {
    starting = true;
  }

}

void loadSd() {

  pinMode(SS, OUTPUT);

  if (!SD.begin()) {
    Serial.println("initialization failed!");
  }

  // create a new numbered file

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

  dataFile = SD.open(filename, FILE_WRITE);

  if (!dataFile) {
    Serial.println("Couldn't create file...");
  }
}
//Mux control pins
int sR0 = 9;
int sR1 = 10;
int sR2 = 11;
int sR3 = 12;

What Arduino are you using? How have you wired the SD module?
Pin 11 is likely MOSI and Pin 12 MISO on the SPI bus. I also think that pin 10 may need to be reserved as an output for the SD library.

I'm using an Arduino Micro and I doubt it's the wiring as the examples run fine.

I'm using pin 10 for the multiplexer but I've disabled it for testing and the SD still doesn't work.

if (!SD.begin()) {
    Serial.println("initialization failed!");
  }

SD.begin(uint8_t csPin) takes the chip select pin as an input parameter.

if (!SD.begin(8)) {
    Serial.println("initialization failed!");
  }

Awesome, it now works thanks! :slight_smile:

Although the data is being stored as it should I do get a "initialization failed!" error after the first time the card is inserted.

Any ideas as to why this is happening?

Although the data is being stored as it should I do get a "initialization failed!" error after the first time the card is inserted.

Any ideas as to why this is happening?

It looks like you are attempting to use some sort of card detect circuit attached to pin 2. Do you have the schematic for your module? Is it consistent with

The line below gives me 1 when inserted 0 when it isn't.
Code: [Select]
reading = digitalRead(sdInPin);