SD card

Hi all. There are several examples on how to log onto a SD card. My problem is that I have my arduino, I mounted a MC SD card holder from sparkfun. When I startup the board empty, I cannot log. It seams to not recognice the card. If I have inserted a SD card, and then startup, It all works fine...

Is there a interupt to fix this?

Best, Andy :)

I’m not sure I follow. You’re confused because you can’t log to an SD card if it isn’t in the board? That makes good sense doesn’t it. I’ve never seen any disk drive that could write to a disk that wasn’t in the drive.

Maybe you could show us the code and explain the problem a little better.

Is there a interupt to fix this?

No. What is the problem? How can you expect to log to a card you haven't inserted?

Hi Guys! Of course it cannot log when the card is not present…
But after startup, I insert a SD card. Then something is happening… I really dont know what…
To be more spesific, It seams to arduino freezes…

I want to start logging when “Card detection” is high. When SD card is present…

*********************** Code **********************************************************************************

#include <math.h>
#include <Wire.h>
#include <SD.h>

const int ThermistorValue = 10000; // 10kohm resistor
const int chipSelect = 10; //
const int CD = 9; // Card Detect

int ledPin = 8;
int CDState = 0;

void setup() {

pinMode(CD, INPUT);
pinMode(ledPin, OUTPUT);

while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {Serial.println(“Card failed, or not present”);}

void loop() {

CDState = digitalRead(CD);
int Temp = Thermister(analogRead(0));


digitalWrite(ledPin, LOW);
String dataString = String(Temp);
File dataFile =“datalog3.txt”, FILE_WRITE);

if (dataFile) {
// print to the serial port too:
digitalWrite(ledPin, HIGH);
Serial.println(“Initializing SD card…”);


double Thermister(int RawADC) {
double k;
double c;
k = log(((10240000/RawADC) - ThermistorValue));

k = 1 / (0.001129148 + (0.000234125 * k) + (0.0000000876741 * k * k * k));
c = k - 273.15;
return c;

*********************** Code **********************************************************************************

But after startup, I insert a SD card. Then something is happening... I really dont know what... To be more spesific, It seams to arduino freezes...

Some Serial.print() statements would tell you whether that was true, and if so, where the problem is.

But, look at your code. On startup, setup() is run, and fails in the call to SD.begin(), if there is no card.

Then, you insert a card, and (maybe) CDState is set to HIGH. If so, you try to open a file, without ever calling SD.begin() again.

You need to move the call to SD.begin() into loop(), in the CDState == HIGH block, but make sure to call it only once.

PaulS, Thanks :slight_smile:
I´ll try this now!
If it´s not working, I´ll post it here :slight_smile:

PaulS :) Thanks! Now it works :D