Data Logging Program Question

Hi all,

I have a question regarding an issue that I am running into. I am using an Arduino Uno, with the Adafruit Data logging shield (on board SD and RTC clock) and the AD595 thermocouple interface. The issue is with the combination of the two. Alone both of these are working correctly however when I try to combine the two, the data that is read is incorrect and not even a value that I can determine its origin. Basically I am using the Adafruit open source fridge temp and light code to do the data logging and want to incorporate the AD595 as the temperature source. I realize this is large and open question so I will continue to debug this project on my end, but any suggestions as to what to look at would be helpful. I suspect that for some reason I do not have permission to access the AD595 library source so the output I am seeing is wrong.

TOTAL PROJECT

#include <SD.h>
#include <Wire.h>
#include "RTClib.h"
#include <AD595.h> //Included to add 595 library for use in temp logging

// A simple data logger for the Arduino analog pins

// how many milliseconds between grabbing data and logging it. 1000 ms is once a second
#define LOG_INTERVAL  5000 // mills between entries (reduce to take more/faster data)

// how many milliseconds before writing the logged data permanently to disk
// set it to the LOG_INTERVAL to write each time (safest)
// set it to 10*LOG_INTERVAL to write all data every 10 datareads, you could lose up to 
// the last 10 reads if power is lost but it uses less power and is much faster!
#define SYNC_INTERVAL 5000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()

#define ECHO_TO_SERIAL   1 // echo data to serial port
#define WAIT_TO_START    0 // Wait for serial input in setup()

// the digital pins that connect to the LEDs
#define redLEDpin 2
#define greenLEDpin 3

// The analog pins that connect to the sensors
#define tempPin 0                // analog 0
#define BANDGAPREF 14            // special indicator that we want to measure the bandgap

#define aref_voltage 3.3         // we tie 3.3V to ARef and measure it with a multimeter!
#define bandgap_voltage 1.1      // this is not super guaranteed but its not -too- off

RTC_DS1307 RTC; // define the Real Time Clock object

// for the data logging shield, we use digital pin 10 for the SD cs line
const int chipSelect = 10; //THis is the same as setting the pin=SS

// the logging file
File logfile;
AD595 thermocouple; //Define thermocouple

void error(char *str)
{
  Serial.print("error: ");
  Serial.println(str);
  
  // red LED indicates error, missing MEDIA
  digitalWrite(redLEDpin, HIGH);

  while(1);
}

void setup(void)
{
  Serial.begin(9600);
  Serial.println();
  thermocouple.init(0); // Define that thermocuple input is A3
  
  // use debugging LEDs
  pinMode(redLEDpin, OUTPUT);
  pinMode(greenLEDpin, OUTPUT);
  
#if WAIT_TO_START
  Serial.println("Type any character to start");
  while (!Serial.available());
#endif //WAIT_TO_START

  // initialize the SD card
  Serial.print("ADA595 Initialize Success");
  Serial.print('\n');
  Serial.print("Initializing SD card...");
  // make sure that the default chip select pin is set to
  // output, even if you don't use it:
  pinMode(10, OUTPUT);
  
  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    error("Card failed, or not present");
  }
  Serial.println("card initialized.");
  
  // create a new file
  char filename[] = "LOGGER00.CSV";
  for (uint8_t i = 0; i < 100; i++) {
    filename[6] = i/10 + '0';
    filename[7] = i%10 + '0';
    if (! SD.exists(filename)) {
      // only open a new file if it doesn't exist
      logfile = SD.open(filename, FILE_WRITE); 
      break;  // leave the loop!
    }
  }
  
  if (! logfile) {
    error("couldnt create file");
  }
  
  Serial.print("Logging to: ");
  Serial.println(filename);

  // connect to RTC
  Wire.begin();  
  if (!RTC.begin()) {
    logfile.println("RTC failed");
  }
  

  logfile.println("seconds,stamp,datetime,temp,vcc");    
 
  // If you want to set the aref to something other than 5v
  analogReference(EXTERNAL);
}

void loop(void)
{
  DateTime now;

  // delay for the amount of time we want between readings
  delay((LOG_INTERVAL -1) - (millis() % LOG_INTERVAL));
  
  digitalWrite(greenLEDpin, HIGH);
  
  // log milliseconds since starting
  uint32_t m = millis();
  m = m/1000;
  int i = 0;
  i++;
  if (i > 0) {
    m=m+1;
  }
  logfile.print(m);           // milliseconds since start
  logfile.print(", ");    

  // fetch the time
  now = RTC.now();
  // log time
  logfile.print(now.unixtime()); // seconds since 1/1/1970
  logfile.print(", ");
  logfile.print('"');
  logfile.print(now.year(), DEC);
  logfile.print("/");
  logfile.print(now.month(), DEC);
  logfile.print("/");
  logfile.print(now.day(), DEC);
  logfile.print(" ");
  logfile.print(now.hour(), DEC);
  logfile.print(":");
  if (now.minute() <= 9){ //used to pad single digit with 0
    logfile.print("0");
  }
  logfile.print(now.minute(), DEC);
  logfile.print(":");
  if (now.minute() <= 9){
	logfile.print("0");
  }
  logfile.print(now.second(), DEC);
  logfile.print('"');
  //Temperature READ SET
  //analogRead(tempPin); 
  delay(10);
  
  //int tempReading = analogRead(tempPin);    
  //converting that reading to voltage, for 3.3v arduino use 3.3, for 5.0, use 5.0
  //float temperatureC = tempReading * 100 / 1024;  
  //float temperatureF = (temperatureC * 9 / 5) + 32;
  float temperatureF = thermocouple.measure(TEMPF); // Re-defined temperatureF as a 
  					// response from ADA595
  logfile.print(", ");    
  logfile.print(temperatureF);

  // Log the estimated 'VCC' voltage by measuring the internal 1.1v ref
  analogRead(BANDGAPREF); 
  delay(10);
  int refReading = analogRead(BANDGAPREF); 
  float supplyvoltage = (bandgap_voltage * 1024) / refReading; 
  
  logfile.print(", ");
  logfile.print(supplyvoltage);

  logfile.println();

  digitalWrite(greenLEDpin, LOW);

  // Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
  // which uses a bunch of power and takes time
  if ((millis() - syncTime) < SYNC_INTERVAL) return;
  syncTime = millis();
  
  // blink LED to show we are syncing data to the card & updating FAT!
  digitalWrite(redLEDpin, HIGH);
  logfile.flush();
  digitalWrite(redLEDpin, LOW);
  
}

AD595 DEMO

/*
 Demonstration sketch for Hobbybotics AD595 Thermocouple breakout board.
 
 Reads temperature from AD595 in celsius and fahrenheit.  Prints results to serial monitor.
*/

#include <AD595.h>

AD595 thermocouple;
  
void setup() {
  Serial.begin(9600);
  thermocouple.init(0);
  
  Serial.println("AD595 test");
  // wait for AD595 chip to stabilize
  delay(500);
}

void loop() {
  // basic readout test, just print the current temp
  
  // Serial.print("C = "); 
   //Serial.println(float(thermocouple.measure(TEMPC)));
   Serial.print('\n');
   Serial.print("F = ");
   Serial.println(float(thermocouple.measure(TEMPF)));
 
   delay(1000);
}

AD595 Lib File

/*-----------------------------------------------------------------------------------------------
 * File: AD595.cpp
 * Function: AD595 Thermocouple library
 * Description: AD595 Type-K Thermocouple Library for the Hobbybotics AD595 breakout board.
 * Created by Curtis Brooks, May 13, 2012.
 * Updated: N/A
 * Released into the public domain.
 * 
 * ----------------------------------------------------------------------------------------------*/
// Add necessary include files
#include "AD595.h" 
/*-----------------------------------------------------------------------------------------------
 * Public Methods
 * ----------------------------------------------------------------------------------------------*/
void AD595::init(uint8_t DO) {
  _DO = DO;
  
  //define pin modes
  pinMode(_DO, INPUT);
}
double AD595::measure(uint8_t type) {
  double value;
  
  switch(type) {
    case TEMPC : value = tempC(); break;
    case TEMPF : value = tempF(); break;
  }
  return value;
}

/*-----------------------------------------------------------------------------------------------
 * Private Methods
 * ----------------------------------------------------------------------------------------------*/
double AD595::tempC() {
  return (5.0 * analogRead(_DO) * 100.0) / 1024.0;
}
double AD595::tempF() {
  return ((tempC() * 9.0/5.0) + 32);
}

so the output I am seeing

That you didn't share...

I suspect that for some reason I do not have permission to access the AD595 library source

There is no reason to simply suspect something that you can easily test.

Your "total project" file uses the analogReference() function. The working code appears not to. Explain.

The output that I am seeing: A static 931.00 in the temperature slot of the CSV file

  • when I run the project without my additions or thermocouple hooked up, I see a fluctuating value, which I think is the correct idea that it is reading a voltage at that pin and attempting to display the temperature. However without the thermister used in the project origionally, I cannot be sure.
  • when I run the AD595 test project included above, I get the correct temperature values

How can I test if I have access to that library?

  • I am familiar with C programming however, I am very new to the Arduino

As far as the analogReference(), it was there when I began with the code so I am unaware of its functionality.

Thanks,
Tim

when I run the AD595 test project included above, I get the correct temperature values

The demo code doesn't use analogReference().

As far as the analogReference(), it was there when I began with the code so I am unaware of its functionality.

It was where? It was not in the demo code. Why did you put it in the real code?

How can I test if I have access to that library?

Go to where you installed the library. Check the properties on the cpp file in that directory.

In reference to the analogReference(), when I downloaded the fridge light and temperature code it was already in there. Now that you have brought it to my attention I will investigate what it actually does. And access to the library is available through the file system. I was more referring to the possibility that the program structure is not allowing it to access the AD595 library for some reason?

Thanks,

Tim