SD Initialization fails on second write

I am attempting to build a small sensor network for my greenhouse as a first Arduino project. I have several sensors attached and working though there is still some calibration work to be done. One of the things that I would like to do with this is to write the sensor outputs to a file on an SD card.

My setup includes:

Arduino UNO R3
Seeed Studio Grove Base Shield (stacked on top of Arduino)
Arduino Ethernet Shield R3 with Wiznet5100 (Ethernet and microSD output) - jumpered to Base Shield as follows:

Digital Pin 4
Digital Pin 10
Powered as normally

Freetronics TEMT6000 light sensor attached to A1-Reset-Grd as in data sheet

DHT22 Temperature and Humidity Pro sensor attached to A2 using supplied sensor cable

HP20X Barometric Pressure sensor attached to I2C using supplied sensor cable

Homebrew galvanic moisture sensor attached to A0 using schematic found online

All of this works. I can upload a sketch and run all day long dumping sensor values.

I want to log them to the SD card though and eventually to send them via XBee to my PC.

Right now the sketch and the hardware only includes the SD card output.

When I run Serial Monitor I see that the SD card initially writes the first set of values that it sees.

On the next write the SD card initialization fails.

I have been through a lot of posts looking for a solution that works for me but without success. I do have the digitalWrite setting 10 HIGH.

I have also swapped in a Seeed Studio W5200 Ethernet Shield with SD output and grabbed those libraries but for that setup it won’t even write the first time. I did swap the chipSelect to 8 (instead of 4 for the Arduino borad) as noted in something I saw on Adafruit I think.

One thing that may be problematic is the memory usage by variables in the sketch reportedly leaves only 652 bytes after compile. I understand from somewhere else that the SD write needs at least 512 to function so I tweaked things to get to this point.

I am posting the code here in case anyone has a chance to give me a hint at what is wrong.

You don’t have to go easy on me. If something in the code stinks I’d like to know about it. It’s been 30 years since I messed with C coding so it may take me a while to get my C legs back.


Greenhouse_Sensors5.ino (4.55 KB)

One other bit of information that is potentially very important is the size of the SD card that I am currently using.

The unit has an 8GB microSD card inserted right now.

I think this may be part of the problem as I understand from somewhere that the software will try to determine how much usable space it has available before each write and that it may cause a time-out on the second and subsequent writes.

I'm going to try to find a smaller capacity card for this setup.

In the meantime, if anyone sees anything else in the code mess I'm all ears.

EDIT: Have tried 8, 4 and 2 GB cards with no difference in the results. It still writes correctly the first time and fails on subsequent writes.

Any ideas?

This section of code should be run only once in setup, not every time when you want to access the SD card.

  Serial.print("Initializing SD card...");
  // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
  // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
   pinMode(10, OUTPUT);
   digitalWrite(10, HIGH);
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
  Serial.println("initialization done.");


Thanks for taking a look at this. I did as you indicated and moved the SD initialization into the void(setup) and I am now able to write normally to the SD card.

The only issue that I have at the moment is that the sensor output from the HP20X written to the SD card is always 0.00 for the three values (Temperature, Pressure, Altitude).

The HP20X output to the Serial Monitor is normal, varying according to conditions.

I am looking into this at the moment and will post code that shows the SD correction and the properly formatted output as soon as I get it solved.

I appreciate the quick response.