OneWire, DallasTemerature, SD

Having a little issue with SD and OneWire. I have started over about 3 times and every approach fails.

Hardware is working. I have verified this with individual sketches only dealing with either SD or Temp Sensors. The connections should be obious via my code.

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

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 10

// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire);

//SD Card
// ** MOSI - pin 11
// ** MISO - pin 12
// ** CLK - pin 13
// ** CS - pin 4
const int chipSelect = 4;


void setup(void)
{
  Serial.println(freeRam());
  // start serial port
  Serial.begin(9600);
  Serial.println("Dallas Temperature IC Control Library Demo");

  // Start up the library
  sensors.begin();
  Serial.println(freeRam());
  Serial.print("Locating devices...");
  Serial.print("Found ");
  Serial.print(sensors.getDeviceCount(), DEC);
  Serial.println(" devices.");
  Serial.println(freeRam());
  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");
  Serial.println(freeRam());
}

void loop(void)
{ 
  Serial.println(freeRam());
  // call sensors.requestTemperatures() to issue a global temperature 
  // request to all devices on the bus
  Serial.print("Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  Serial.println(freeRam());
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("data.csv", FILE_WRITE);

  // if the file is available, write to it:
  if (dataFile) {
    dataFile.print(sensors.getTempFByIndex(0));
    dataFile.print(",");
    dataFile.print(sensors.getTempFByIndex(1));
    dataFile.print(",");
    dataFile.println(sensors.getTempFByIndex(2));
    dataFile.close();
    }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening file");
  }

  
  Serial.print("Temperature for the device 1 (index 0) is: ");
  Serial.println(sensors.getTempFByIndex(0));
  Serial.print("Temperature for the device 2 (index 1) is: ");
  Serial.println(sensors.getTempFByIndex(1));    
  Serial.print("Temperature for the device 3 (index 2) is: ");
  Serial.println(sensors.getTempFByIndex(2));
  delay(1000);    
}
int freeRam () {
  extern int __heap_start, *__brkval; 
  int v; 
  return (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval); 
}

I was originally thinking i was running out of ram but it doesn't look to be the case.

This is my serial output when i run the above code.

Dallas Temperature IC Control Library Demo
822
Locating devices...Found 3 devices.
822
Initializing SD card...card initialized.
822
793
Requesting temperatures...DONE
793
error opening file
Temperature for the device 1 (index 0) is: -196.60
Temperature for the device 2 (index 1) is: -196.60
Temperature for the device 3 (index 2) is: -196.60
793
Requesting temperatures...DONE
793
error opening file
Temperature for the device 1 (index 0) is: -196.60
Temperature for the device 2 (index 1) is: -196.60
Temperature for the device 3 (index 2) is: -196.60
793
Requesting temperatures...DONE

If I comment out the SD portion, this is my output.

//  // see if the card is present and can be initialized:
//  if (!SD.begin(chipSelect)) {
//    Serial.println("Card failed, or not present");
//    // don't do anything more:
//    return;
//  }
//  Serial.println("card initialized.");

Output

Dallas Temperature IC Control Library Demo
869
Locating devices...Found 3 devices.
869
Initializing SD card...869
840
Requesting temperatures...DONE
840
error opening file
Temperature for the device 1 (index 0) is: 76.10
Temperature for the device 2 (index 1) is: 99.84
Temperature for the device 3 (index 2) is: 56.41
840
Requesting temperatures...DONE
840
error opening file
Temperature for the device 1 (index 0) is: 76.10
Temperature for the device 2 (index 1) is: 99.61
Temperature for the device 3 (index 2) is: 56.41
840
Requesting temperatures...DONE

Temp Values are correct. Ambient, Near Candle, Was ice water.

Did i miss something about using OneWire and SD not getting along?

Solved... Changed

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 10

to

// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 2

Moved the wire and its working. Must be something hokey with Dallas_Temperature_Lib and the bit banging.

twilliamson:
Must be something hokey with Dallas_Temperature_Lib and the bit banging.

No, just slack-arsed coding for the SD, and putting the sensor on the wrong pin. You don't say which Arduino you are using, but I'm surprised that you have it working and you may still find it unreliable as you do not appear to have secured pin 10 or 53 as output, which is standard procedure. Check the standard examples for SD operations. I don't know what bit banging is, but I bet it isn't a part of the problem.

One thing to remember is that (on an UNO) Pin 10 is the SPI hardware SlaveSelect pin and MUST be an OUTPUT when the Arduino is the SPI Master. OneWire will switch the bus pin between OUTPUT and INPUT. Doing that on Pin 10 will flip the SPI bus between Master and Slave. That will mess up the communications with the SD card on the SPI bus.