First Hello all, First time poster - long time lurker tho.
I have searched the forum and rest of the internet, but only thread I came across for this is the following (Uno data logging DS18B20 daisy chained sensors - #17 by tynawg9 - Sensors - Arduino Forum). The pins are set up correctly for a Mega even though the posted thread is for a Uno, and I have read the entire thread, and have searched the internet but I am having a problem writing the temperature data that the DS18B20 Sensors are producing to my microsd Card in a .csv file. Everything displays correctly except the data is outputing to zero (0) in the .csv file but the serial monitor actually shows the correct temperature in *C.
Here is my code:
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>
#include <SD.h>
#define ONE_WIRE_BUS_PIN 2
OneWire oneWire(ONE_WIRE_BUS_PIN);
DallasTemperature sensors(&oneWire);
const int chipSelect = 4;
float tempC, Probe01, Probe02, Probe03;
DeviceAddress Probe01 = { 0x28, 0x89, 0x1A, 0x08, 0x80, 0x00, 0x80, 0xBF };
DeviceAddress Probe02 = { 0x28, 0x4A, 0x05, 0x08, 0x80, 0x00, 0x80, 0x54 };
DeviceAddress Probe03 = { 0x28, 0xC6, 0x29, 0x08, 0x80, 0x00, 0x80, 0xD8 };
void setup() /****** SETUP: RUNS ONCE ******/
{
// start serial port to show results
Serial.begin(9600);
delay(300);//Wait for newly restarted system to stabilize
Serial.print("Initializing Temperature Control Library Version ");
Serial.println(DALLASTEMPLIBVERSION);
Serial.print("Initializing");
delay(2000);
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect))
{
Serial.print("failed!");
delay (2000);
return;
}
Serial.println(" init. OK!");
// Initialize the Temperature measurement library
sensors.begin();
// set the resolution to 10 bit (Can be 9 to 12 bits .. lower is faster)
sensors.setResolution(Probe01, 10);
sensors.setResolution(Probe02, 10);
sensors.setResolution(Probe03, 10);
}//--(end setup )---
void loop() /****** LOOP: RUNS CONSTANTLY ******/
{
delay(3000);
Serial.println();
Serial.print("Number of Devices found on bus = ");
Serial.println(sensors.getDeviceCount());
Serial.print("Getting temperatures... ");
Serial.println();
// Command all devices on bus to read temperature
sensors.requestTemperatures();
Serial.print("Probe 01 temperature is: ");
printTemperature(Probe01);
Serial.println();
probe1 = tempC;
Serial.print("Probe 02 temperature is: ");
printTemperature(Probe02);
Serial.println();
probe2 = tempC;
Serial.print("Probe 03 temperature is: ");
printTemperature(Probe03);
Serial.println();
probe3 = tempC;
File datafile = SD.open("tdata.csv", FILE_WRITE);
if(datafile)
{
datafile.print("P-1");
datafile.print(",");
datafile.print(probe1);
datafile.print(",");
datafile.print("P-2");
datafile.print(",");
datafile.print(probe2);
datafile.print(",");
datafile.print("P-3");
datafile.print(",");
datafile.print(probe3);
datafile.print(",");
datafile.println();
datafile.close();
}
else
Serial.println("SD card error.");
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE
}//--(end main loop )---
/*-----( Declare User-written Functions )-----*/
void printTemperature(DeviceAddress deviceAddress){
tempC = sensors.getTempC(deviceAddress);
if (tempC == -127.00){
Serial.print("Error getting temperature ");
}
else{
Serial.print("C: ");
Serial.print(tempC);
//Serial.print(" F: ");
// Serial.print(DallasTemperature::toFahrenheit(tempC));
}
}// End printTemperature
//*********( THE END )***********
Any help would be appreciated, thanks.
Nick