Empty Serial Monitor and SD file with temperature Datalogger

Hey!

I am working on a project to log data from 6 Temperature sensors (DS18B20) with an arduino UNO and Ubuntu 15.10 OS.
I am using a DS3231 Real Time clock and a MicroSD board using SPI. I am running the Temperature sensors in parasite mode as described in the datasheet.

From the code I expected to get an output of Date, time and 6 temperature values in each row of the serial monitor as well as in the .txt file on the sd card. The first time i uploaded the code to the arduino it worked perfectly.

From thwe 2nd time I dont get any output on the Serial Monitor. On the SD card the file is created and the headers are written in the .txt file, but there is no values of measurments logged.

I can find the adresses of all the temperature sensors with the programm provided here , I checked the availability of the SD card with a small programm and the red light on the RTC is on as soon as I connect it with my computer.

I cannot figure out why it is not working and I would apreciate any Ideas. Below you will find the code I uploaded and a fritzing diagramm.

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

int chipSelect=10;//Set chipselect 10d

File sensorData ;// Variable for writing our Data File

// Init the DS3231 using the hardware interface
// DS3231 SDA is connected to Analog 4, SCL to Analog 5
DS3231  rtc(SDA, SCL);
 

// The data wire is plugged into port 2 on the Arduino 
#define oneWire_BUS 2 


// ADC resolution of 12 bit 
#define temperatureResolution 12  

unsigned int delay_milliseconds = 168; 
// ... 
unsigned int seconds = 0; 

/* Setup a oneWire instance to communicate with any OneWire devices 
 * (not just Maxim/Dallas temperature ICs) */ 
OneWire oneWire(oneWire_BUS); 
// Pass our oneWire reference to Dallas Temperature. 
DallasTemperature sensors(&oneWire); 

// Arrays to hold device addresses 
DeviceAddress Sensor_B = { 0x28, 0xFF, 0x7F, 0xCD, 0x00, 0x16, 0x04, 0xC9 }; 
DeviceAddress Sensor_C = { 0x28, 0xFF, 0x6F, 0xC2, 0x00, 0x16, 0x04, 0x2F }; 
DeviceAddress Sensor_D = { 0x28, 0xFF, 0x4F, 0xF8, 0x00, 0x16, 0x04, 0x84 }; 
DeviceAddress Sensor_E = { 0x28, 0xFF, 0xFD, 0x0D, 0x01, 0x16, 0x04, 0x13 };
DeviceAddress Sensor_G = { 0x28, 0xFF, 0x4D, 0xE5, 0x00, 0x16, 0x04, 0xA2 };
DeviceAddress Sensor_H = { 0x28, 0xFF, 0xD8, 0x15, 0x05, 0x16, 0x03, 0x00 }; 


// Variables to store temperature values
float tB;
float tC;
float tD;
float tE;
float tG;
float tH;

void setup(void) 
{  // Start serial port 
  Serial.begin(9600);
  //Initialize the rtc object
  //rtc.begin();
  //rtc.setDOW(WEDNESDAY);     // Set Day-of-Week to SUNDAY
  //rtc.setTime(12, 28, 0);     // Set the time to 12:00:00 (24hr format)
  //rtc.setDate(20, 4, 2016);    //Set the date to January 1st, 2014
   
  // Start up the library 
  sensors.begin(); 
  // Set the resolution to 12 bit 
  sensors.setResolution(Sensor_B, temperatureResolution); 
  sensors.setResolution(Sensor_C, temperatureResolution); 
  sensors.setResolution(Sensor_D, temperatureResolution); 
  sensors.setResolution(Sensor_E, temperatureResolution);  
  sensors.setResolution(Sensor_G, temperatureResolution); 
  sensors.setResolution(Sensor_H, temperatureResolution);
 
  
  SD.begin(chipSelect);//Initialize SD Card with Chipselect 
  
     sensorData= SD.open("testData.txt", FILE_WRITE); //open File DATA.txt on SD card as file to write to

if (sensorData) {
  sensorData.print("Date"); sensorData.print("\t");
  sensorData.print("Time"); sensorData.print("\t");
  sensorData.print("C"); sensorData.print("\t");
  sensorData.print("B"); sensorData.print("\t");
  sensorData.print("D"); sensorData.print("\t");
  sensorData.print("E"); sensorData.print("\t"); 
  sensorData.print("G"); sensorData.print("\t");
  sensorData.print("H"); sensorData.print("\t");
  sensorData.println(); 
}
  
  sensorData.close();

  
} 



// Function to get the temperature for a device 
float getTemperature(DeviceAddress deviceAddress) 
{ 
  float tempC = sensors.getTempC(deviceAddress); 
  if (tempC <= -127.00)
  { 
    Serial.print("NA");
  }
  else
  {
    return(tempC); 
  }
} 


// Function to print the temperature or temperature difference
void printTemperatures(void) 
{ Serial.print(rtc.getDateStr()); Serial.print("\t");
  Serial.print(rtc.getTimeStr()); Serial.print("\t");
  Serial.print(tB); Serial.print("\t");  
  Serial.print(tC); Serial.print("\t"); 
  Serial.print(tD); Serial.print("\t"); 
  Serial.print(tE); Serial.print("\t");
  Serial.print(tG); Serial.print("\t"); 
  Serial.print(tH); Serial.print("\t"); 
  Serial.println(); 
  
     sensorData= SD.open("testData.txt", FILE_WRITE); //open File DATA.txt on SD card as file to write to

if (sensorData) {
  sensorData.print(rtc.getDateStr()); sensorData.print("\t");
  sensorData.print(rtc.getTimeStr()); sensorData.print("\t");
  sensorData.print(tB); sensorData.print("\t");
  sensorData.print(tC); sensorData.print("\t");
  sensorData.print(tD); sensorData.print("\t");
  sensorData.print(tE); sensorData.print("\t");
  sensorData.print(tG); sensorData.print("\t");
  sensorData.print(tH); sensorData.print("\t");
  sensorData.println(); 
}
  
  sensorData.close();
}

void loop(void) 
{ 
  // Issue a global temperature request to all devices on the bus
  sensors.requestTemperatures();
  // Get temperature values
  tC = getTemperature(Sensor_B); 
  tB = getTemperature(Sensor_C); 
  tD = getTemperature(Sensor_D); 
  tE = getTemperature(Sensor_E);
  tG = getTemperature(Sensor_G); 
  tH = getTemperature(Sensor_H);
  
  // Print the device information  
  printTemperatures();
  // Delay time
  delay(delay_milliseconds);
}

You have rtc.begin commented out. Therefore I don't expect the clock to work.

There is no need to call 12 bit resolution, it is that by default.

A delay of 168 millisec seems very strange and I won't ask why, but it surely isn't enough. The sensors require 750 at that resolution, and the best you are likely to see is 85 as an error. Why don't you use
delay(1000);
to make it easier on everybody, including yourself. You might tune it a bit later if you need.

I'm not sure if

     sensorData= SD.open("testData.txt", FILE_WRITE);
if (sensorData) {
  sensorData.print(rtc.getDateStr()); sensorData.print("\t");
  sensorData.print(rtc.getTimeStr()); sensorData.print("\t");
  sensorData.print(tB); sensorData.print("\t");
  sensorData.print(tC); sensorData.print("\t");
  sensorData.print(tD); sensorData.print("\t");
  sensorData.print(tE); sensorData.print("\t");
  sensorData.print(tG); sensorData.print("\t");
  sensorData.print(tH); sensorData.print("\t");
  sensorData.println();
}

is nonsense or not, but it is likely to be, and is certainly unnecessary. If you want to open the file, print to it. For example

void WriteSD()
{  
           myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
  myFile.print(hour);
  myFile.print(":");
  myFile.print(minute);
  myFile.print(":");
  myFile.print(second);
  myFile.print(",");

  myFile.print(InTemp);
  myFile.print(",");
  myFile.print(OutTemp);
  myFile.print(",");
  myFile.print(DrainTemp);
  myFile.print(",");
  myFile.println(ShrTemp);
       myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE     
}]

I would stay away from parasite power. I know it is mentioned in the datasheet, but I recall an implication that it is a method of last resort, and I bet you don't have a good reason to use it.

Thanks a lot! :o :o I didnt see the uncommented rtc.begin().

And thank you for further Information. I didnt comment my sketch properly so here the missing comment for the delay time of 168, just for completeness.

// Time between two measurements 
/* Max. temperature conversion time for 12 bit resolution: 750 ms 
 * Actual conversion time (empirically derived, see tab "displayed_elapsedTime"): 
 * 752 ms (average)
 * 
 * Actual needed time for execution of loop (empirically derived): 832 ms 
 * => Delay time: (1000 - 832 = 168) ms 
 */

I am using this library for the DS3231 and it works for me, it allows the expressions to get time and date.

It is surely a good Idea to write a seperate function to write SD and also you are right: I dont have a reason to run in parasite mode and it is definitely unnesecary :slight_smile:

No. The time of your loop is

168 + whatever time it takes to serial print and SD print = not very much and probably a lot less than 750.

I'm not sure what you mean by "empirically derived" but I'll be surprised if the loop takes 852 ms.

The temp sensing and conversion is outside the loop. In short, you are not securing enough time for the sensor to do its job. If you are getting a result, i.e. the print duties etc take more than 750ms, it is by good luck and not good management. Eventually, you will probably put the job in a timed window, rather than use delay.