Declaring variable causes SD writing to fail.

I'm trying to write data from my Adafruit MPL3115A2 pressure/altitude/temperature sensor to my micro SD card. I've used the test code from this website to test the SD card and it writes just fine. I've also used the Adafruit code to check to output from my sensor. I cannot, however, write this data directly to my SD card and I don't know what is wrong.

The code, as written, works until unless I comment out line 31

// float tempC = baro.getTemperature();

and line 55

// myFile.println(tempC);

If I add these lines, the Serial Monitor goes blank and nothing is written to the SD Card.

I can't understand why both the sensor and SD card seem to work independently, but they cannot work together.

/*
  SD card read/write

 This example shows how to read and write data to and from an SD card file
 The circuit:
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)

 created   Nov 2010
 by David A. Mellis
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */
#include <Wire.h>

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

Adafruit_MPL3115A2 baro = Adafruit_MPL3115A2();
File myFile;

void setup() {
  
  // float tempC = baro.getTemperature();
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


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

  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
    while (1);
  }
  Serial.println("initialization done.");

  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  myFile = SD.open("test.txt", FILE_WRITE);

  // if the file opened okay, write to it:
  if (myFile) {
    Serial.print("Writing to test.txt...");
    myFile.println("testing 1, 2, 3.");
    // myFile.println(tempC);
    // close the file:
    myFile.close();
    Serial.println("done.");
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }

  // re-open the file for reading:
  myFile = SD.open("test.txt");
  if (myFile) {
    Serial.println("test.txt:");

    // read from the file until there's nothing else in it:
    while (myFile.available()) {
      Serial.write(myFile.read());
    }
    // close the file:
    myFile.close();
  } else {
    // if the file didn't open, print an error:
    Serial.println("error opening test.txt");
  }
}

void loop() {
  // nothing happens after setup
}

sketch_SD_Card_setup.ino (1.88 KB)

That isn't your entire sketch.

You haven't called baro.begin() to initialize the sensor. Look at the Adafruit example more closely.

Pete

After much troubleshooting, in addition to not having the baro.begin statement, my virus scanner appears to be interfering with the compiling process a lot. Turning it off fixes much of the problems I've been having.