title: SD.h not compatible with other libraries in Arduino/C++ environment
body: <p>I'm having some very weird issues using the following hardware elements:</p>
  
  <ul>
  <li><a href="http://arduino.cc/en/Main/ArduinoBoardUno" rel="nofollow">Arduino Uno</a></li>
  <li><a href="http://en.wikipedia.org/wiki/Wi-Fi" rel="nofollow">Wi-Fi</a> <a href="http://www.arduino.cc/en/Main/ArduinoShields" rel="nofollow">shield</a></li>
  <li>GPS receiver</li>
  <li>Accelerometer</li>
  <li>Barometer</li>
  </ul>
  
  <p>I wanted to off-load the sensor readings to an SD card as needed, but before I can even code the SD functions, the mere inclusion of the <code>SD.h</code> library renders my code useless.</p>
  
  <p>My code is as follows:</p>
  
  <pre><code>#include &lt;SoftwareSerial.h&gt;
  #include &lt;TinyGPS.h&gt;
  #include &lt;SD.h&gt;
  
  /* This sample code demonstrates the normal use of a TinyGPS object.
     It requires the use of SoftwareSerial, and assumes that you have a
     4800-baud serial GPS device hooked up on pins 3(rx) and 4(tx).
  */
  
  //For baraometer
  #include &lt;Wire.h&gt;
  
  #define BMP085_ADDRESS 0x77  // I2C address of BMP085
  
  const unsigned char OSS = 2;  // Oversampling Setting
  
  // Calibration values
  int ac1;
  int ac2;
  int ac3;
  unsigned int ac4;
  unsigned int ac5;
  unsigned int ac6;
  int b1;
  int b2;
  int mb;
  int mc;
  int md;
  
  // b5 is calculated in bmp085GetTemperature(...), this variable is also used in bmp085GetPressure(...)
  // So ...Temperature(...) must be called before ...Pressure(...).
  long b5;
  //End of baraometer
  
  //ACcelerometer
  // These constants describe the pins. They won't change:
  const int xpin = A1;                  // x-axis of the accelerometer
  const int ypin = A2;                  // y-axis
  const int zpin = A3;                  // z-axis (only on 3-axis models)
  //end of accel
  
  TinyGPS gps;
  SoftwareSerial nss(3, 4);
  
  static void gpsdump(TinyGPS &amp;gps);
  static bool feedgps();
  static void print_float(float val, float invalid, int len, int prec);
  static void print_int(unsigned long val, unsigned long invalid, int len);
  static void print_date(TinyGPS &amp;gps);
  static void print_str(const char *str, int len);
  
  void setup()
  {
    //Make sure the analog-to-digital converter takes its reference voltage from
    // the AREF pin
    analogReference(EXTERNAL);
  
    pinMode(xpin, INPUT);
    pinMode(ypin, INPUT);
    pinMode(zpin, INPUT);
  
    //Barometer
    Wire.begin();
    bmp085Calibration();
  
    //GPS
    Serial.begin(115200);
    nss.begin(57600);
  
    Serial.print("Testing TinyGPS library v. "); Serial.println(TinyGPS::library_version());
    Serial.println("by Mikal Hart");
    Serial.println();
    Serial.print("Sizeof(gpsobject) = "); Serial.println(sizeof(TinyGPS));
    Serial.println();
    Serial.println("Sats HDOP Latitude Longitude Fix  Date       Time       Date Alt     Course Speed Card  Distance Course Card  Chars Sentences Checksum");
    Serial.println("          (deg)    (deg)     Age                        Age  (m)     --- from GPS ----  ---- to London  ----  RX    RX        Fail");
    Serial.println("--------------------------------------------------------------------------------------------------------------------------------------");
  }
  
  void loop()
  {
    //Accelerometer
    Serial.print( analogRead(xpin));
    Serial.print("\t");
  
    //Add a small delay between pin readings.  I read that you should
    //do this but haven't tested the importance
      delay(1);
  
    Serial.print( analogRead(ypin));
    Serial.print("\t");
    //add a small delay between pin readings.  I read that you should
    //do this but haven't tested the importance
      delay(1);
  
    Serial.print( analogRead(zpin));
    Serial.print("\n");  // delay before next reading:
  
  
  
  
  
  
    bool newdata = false;
    unsigned long start = millis();
  
    // Every second we print an update
    while (millis() - start &lt; 1000)
    {
      if (feedgps())
        newdata = true;
    }
  
  
    //barometer
    float temperature = bmp085GetTemperature(bmp085ReadUT()); //MUST be called first
    float pressure = bmp085GetPressure(bmp085ReadUP());
    float atm = pressure / 101325; // "standard atmosphere"
    float altitude = calcAltitude(pressure); //Uncompensated caculation - in Meters
  
    Serial.print("Temperature: ");
    Serial.print(temperature, 2); //display 2 decimal places
    Serial.println(" C");
  
    Serial.print("Pressure: ");
    Serial.print(pressure, 0); //whole number only.
    Serial.println(" Pa");
  
    Serial.print("Standard Atmosphere: ");
    Serial.println(atm, 4); //display 4 decimal places
  
    Serial.print("Altitude: ");
    Serial.print(altitude, 2); //display 2 decimal places
    Serial.println(" M");
  
    Serial.println();//line break
    //end of barometer
  
  
  
    gpsdump(gps);
  }
  
  static void gpsdump(TinyGPS &amp;gps)
  {
    float flat, flon;
    unsigned long age, date, time, chars = 0;
    unsigned short sentences = 0, failed = 0;
    static const float LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  
    print_int(gps.satellites(), TinyGPS::GPS_INVALID_SATELLITES, 5);
    print_int(gps.hdop(), TinyGPS::GPS_INVALID_HDOP, 5);
    gps.f_get_position(&amp;flat, &amp;flon, &amp;age);
    print_float(flat, TinyGPS::GPS_INVALID_F_ANGLE, 9, 5);
    print_float(flon, TinyGPS::GPS_INVALID_F_ANGLE, 10, 5);
    print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
  
    print_date(gps);
  
    print_float(gps.f_altitude(), TinyGPS::GPS_INVALID_F_ALTITUDE, 8, 2);
    print_float(gps.f_course(), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
    print_float(gps.f_speed_kmph(), TinyGPS::GPS_INVALID_F_SPEED, 6, 2);
    print_str(gps.f_course() == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(gps.f_course()), 6);
    print_int(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0UL : (unsigned long)TinyGPS::distance_between(flat, flon, LONDON_LAT, LONDON_LON) / 1000, 0xFFFFFFFF, 9);
    print_float(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : TinyGPS::course_to(flat, flon, 51.508131, -0.128002), TinyGPS::GPS_INVALID_F_ANGLE, 7, 2);
    print_str(flat == TinyGPS::GPS_INVALID_F_ANGLE ? "*** " : TinyGPS::cardinal(TinyGPS::course_to(flat, flon, LONDON_LAT, LONDON_LON)), 6);
  
    gps.stats(&amp;chars, &amp;sentences, &amp;failed);
    print_int(chars, 0xFFFFFFFF, 6);
    print_int(sentences, 0xFFFFFFFF, 10);
    print_int(failed, 0xFFFFFFFF, 9);
    Serial.println();
  }
  
  static void print_int(unsigned long val, unsigned long invalid, int len)
  {
    char sz[32];
    if (val == invalid)
      strcpy(sz, "*******");
    else
      sprintf(sz, "%ld", val);
    sz[len] = 0;
    for (int i=strlen(sz); i&lt;len; ++i)
      sz[i] = ' ';
    if (len &gt; 0)
      sz[len-1] = ' ';
    Serial.print(sz);
    feedgps();
  }
  
  static void print_float(float val, float invalid, int len, int prec)
  {
    char sz[32];
    if (val == invalid)
    {
      strcpy(sz, "*******");
      sz[len] = 0;
          if (len &gt; 0)
            sz[len-1] = ' ';
      for (int i=7; i&lt;len; ++i)
          sz[i] = ' ';
      Serial.print(sz);
    }
    else
    {
      Serial.print(val, prec);
      int vi = abs((int)val);
      int flen = prec + (val &lt; 0.0 ? 2 : 1);
      flen += vi &gt;= 1000 ? 4 : vi &gt;= 100 ? 3 : vi &gt;= 10 ? 2 : 1;
      for (int i=flen; i&lt;len; ++i)
        Serial.print(" ");
    }
    feedgps();
  }
  
  static void print_date(TinyGPS &amp;gps)
  {
    int year;
    byte month, day, hour, minute, second, hundredths;
    unsigned long age;
    gps.crack_datetime(&amp;year, &amp;month, &amp;day, &amp;hour, &amp;minute, &amp;second, &amp;hundredths, &amp;age);
    if (age == TinyGPS::GPS_INVALID_AGE)
      Serial.print("*******    *******    ");
    else
    {
      char sz[32];
      sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d   ",
          month, day, year, hour, minute, second);
      Serial.print(sz);
    }
    print_int(age, TinyGPS::GPS_INVALID_AGE, 5);
    feedgps();
  }
  
  static void print_str(const char *str, int len)
  {
    int slen = strlen(str);
    for (int i=0; i&lt;len; ++i)
      Serial.print(i&lt;slen ? str[i] : ' ');
    feedgps();
  }
  
  static bool feedgps()
  {
    while (nss.available())
    {
      if (gps.encode(nss.read()))
        return true;
    }
    return false;
  }
  
  // Stores all of the bmp085's calibration values into global variables
  // Calibration values are required to calculate temp and pressure
  // This function should be called at the beginning of the program
  void bmp085Calibration()
  {
    Serial.write("\n\nCalibrating ... ");
    ac1 = bmp085ReadInt(0xAA);
    ac2 = bmp085ReadInt(0xAC);
    ac3 = bmp085ReadInt(0xAE);
    ac4 = bmp085ReadInt(0xB0);
    ac5 = bmp085ReadInt(0xB2);
    ac6 = bmp085ReadInt(0xB4);
    b1 = bmp085ReadInt(0xB6);
    b2 = bmp085ReadInt(0xB8);
    mb = bmp085ReadInt(0xBA);
    mc = bmp085ReadInt(0xBC);
    md = bmp085ReadInt(0xBE);
    Serial.write("Calibrated\n\n");
  }
  
  // Calculate temperature in deg C
  float bmp085GetTemperature(unsigned int ut){
    long x1, x2;
  
    x1 = (((long)ut - (long)ac6)*(long)ac5) &gt;&gt; 15;
    x2 = ((long)mc &lt;&lt; 11)/(x1 + md);
    b5 = x1 + x2;
  
    float temp = ((b5 + 8)&gt;&gt;4);
    temp = temp /10;
  
    return temp;
  }
  
  // Calculate pressure given up
  // calibration values must be known
  // b5 is also required so bmp085GetTemperature(...) must be called first.
  // Value returned will be pressure in units of Pa.
  long bmp085GetPressure(unsigned long up){
    long x1, x2, x3, b3, b6, p;
    unsigned long b4, b7;
  
    b6 = b5 - 4000;
    // Calculate B3
    x1 = (b2 * (b6 * b6)&gt;&gt;12)&gt;&gt;11;
    x2 = (ac2 * b6)&gt;&gt;11;
    x3 = x1 + x2;
    b3 = (((((long)ac1)*4 + x3)&lt;&lt;OSS) + 2)&gt;&gt;2;
  
    // Calculate B4
    x1 = (ac3 * b6)&gt;&gt;13;
    x2 = (b1 * ((b6 * b6)&gt;&gt;12))&gt;&gt;16;
    x3 = ((x1 + x2) + 2)&gt;&gt;2;
    b4 = (ac4 * (unsigned long)(x3 + 32768))&gt;&gt;15;
  
    b7 = ((unsigned long)(up - b3) * (50000&gt;&gt;OSS));
    if (b7 &lt; 0x80000000)
      p = (b7&lt;&lt;1)/b4;
    else
      p = (b7/b4)&lt;&lt;1;
  
    x1 = (p&gt;&gt;8) * (p&gt;&gt;8);
    x1 = (x1 * 3038)&gt;&gt;16;
    x2 = (-7357 * p)&gt;&gt;16;
    p += (x1 + x2 + 3791)&gt;&gt;4;
  
    long temp = p;
    return temp;
  }
  
  // Read 1 byte from the BMP085 at 'address'
  char bmp085Read(byte address)
  {
    Wire.beginTransmission(BMP085_ADDRESS);
    Wire.write(address);
    Wire.endTransmission();
    Wire.requestFrom(BMP085_ADDRESS, 1);
    while(!Wire.available()) {};
    return Wire.read();
  }
  
  // Read 2 bytes from the BMP085
  // First byte will be from 'address'
  // Second byte will be from 'address'+1
  int bmp085ReadInt(byte address)
  {
    unsigned char msb, lsb;
  
    Wire.beginTransmission(BMP085_ADDRESS);
    Wire.write(address);
    Wire.endTransmission();
  
    Wire.requestFrom(BMP085_ADDRESS, 2);
    while(Wire.available()&lt;2)
      ;
    msb = Wire.read();
    lsb = Wire.read();
  
    return (int) msb&lt;&lt;8 | lsb;
  }
  
  // Read the uncompensated temperature value
  unsigned int bmp085ReadUT(){
    unsigned int ut;
  
    // Write 0x2E into Register 0xF4
    // This requests a temperature reading
    Wire.beginTransmission(BMP085_ADDRESS);
    Wire.write((byte)0xF4);
    Wire.write((byte)0x2E);
    Wire.endTransmission();
  
    // Wait at least 4.5 ms
    delay(5);
  
    // Read two bytes from registers 0xF6 and 0xF7
    ut = bmp085ReadInt(0xF6);
    return ut;
  }
  
  // Read the uncompensated pressure value
  unsigned long bmp085ReadUP(){
    unsigned char msb, lsb, xlsb;
    unsigned long up = 0;
  
    // Write 0x34+(OSS&lt;&lt;6) into register 0xF4
    // Request a pressure reading w/ oversampling setting
    Wire.beginTransmission(BMP085_ADDRESS);
    Wire.write(0xF4);
    Wire.write(0x34 + (OSS&lt;&lt;6));
    Wire.endTransmission();
  
    // Wait for conversion, delay time dependent on OSS
    delay(2 + (3&lt;&lt;OSS));
  
    // Read register 0xF6 (MSB), 0xF7 (LSB), and 0xF8 (XLSB)
    msb = bmp085Read(0xF6);
    lsb = bmp085Read(0xF7);
    xlsb = bmp085Read(0xF8);
  
    up = (((unsigned long) msb &lt;&lt; 16) | ((unsigned long) lsb &lt;&lt; 8) | (unsigned long) xlsb) &gt;&gt; (8-OSS);
  
    return up;
  }
  
  void writeRegister(int deviceAddress, byte address, byte val) {
    Wire.beginTransmission(deviceAddress); // Start transmission to device
    Wire.write(address);       // Send register address
    Wire.write(val);           // Send value to write
    Wire.endTransmission();    // End transmission
  }
  
  int readRegister(int deviceAddress, byte address){
    int v;
    Wire.beginTransmission(deviceAddress);
    Wire.write(address); // Register to read
    Wire.endTransmission();
  
    Wire.requestFrom(deviceAddress, 1); // Read a byte
  
    while(!Wire.available()) {
      // waiting
    }
  
    v = Wire.read();
    return v;
  }
  
  float calcAltitude(float pressure){
  
    float A = pressure/101325;
    float B = 1/5.25588;
    float C = pow(A,B);
    C = 1 - C;
    C = C /0.0000225577;
  
    return C;
  }
  </code></pre>
  
  <p>Granted, right now, it is merely a conglomeration of multiple example sketches, but they work. I get a sampled reading from the accelerometer, the GPS unit and the barometer once a second. However once I simply add the line <code>#include &lt;SD.h&gt;</code> to the sketch, it fails to run correctly. The serial monitor does not display anything. I have similar versions of the above sketch (omitted as they are much lengthier), but I get the same result: either jumbled text or nothing on the Serial monitor. If I comment out the line that include the SD.h library, everything works fine....</p>
  
  <p>Are there known issues with the <code>SD.h</code> library or conflicts? And yes, I am NOT using the necessary pins for the SD access (digital pin #4) for my sensor connections....</p>
  
  <p>UPDATE:</p>
  
  <p>I at least figured out it has something to do with the SoftSerial (SoftSerial.h) library and the use of the SoftSerial object (which I called nss). I can load all libraries and get everything to work if I do not call nss.begin. Is there a reason why that would conflict?</p>