Hello, I'm working on a project utilizing 6, T-type thermocouples for ground temperature measurements over time. My interface is the Ocean Controls Thermocouple Multiplexer Shield (KTA-259T) mounted atop an arduino Metro from Adafruit.
The issue is I need to write the thermocouple temperatures to an Adafruit SD Card Shield. I've reviewed some of the sample codes for SD cards and am currently able to get the card to initialize in my sketch and thats the extent. From what I've read, the code needs to turn off the multiplexer CS pin to write to the SC Card, but I'm not sure what/how to integrate the SD Card write code into my sketch. Once I can get this working I'd also like to encorporate the DateTime stamps as well. The code I'm using is a sample code from the Ocean Controls website.
Thank you for any help you may provide.
// Assumes 20 MHz or less clock. Insert NOPs for faster operation.
// Pinout for Ocean Design's Thermocouple Multiplexer Shield
#include "SD.h" //talks to SD Card
#include <Wire.h> //helps Arduino with i2c
#include "RTClib.h" //talks with real time clock
#include <SPI.h> // include the SD library:
#define PINEN 7 // TC Mux Enable pin
#define PINA0 4 // TC Mux Address 0 pin
#define PINA1 5 // TC Mux Address 1 pin
#define PINA2 6 // TC Mux Address 2 pin
#define PINSO 12 //TC ADC Slave Out pin (MISO)
#define PINSC 13 //TC ADC Serial Clock (SCK)
#define PINCS 9 //TC ADC Chip Select
#define SHORT -1001
#define OPEN -1000
const int chipSelect = 10;
volatile int tcTemp[9]; // in quarter deg. C, tcTemp[8] is the interal reference temp, disable IRQ's to access these
int readSPI() {
word v = 0;
for (byte i = 16; i != 0; i--) {
v <<= 1;
digitalWrite(PINSC, HIGH);
// 100nS min. delay implied
v |= digitalRead(PINSO);
digitalWrite(PINSC, LOW); // request next serial bit
// 100nS min. delay implied
}
return v;
}
void tcTempSetup() {
pinMode(PINEN, OUTPUT);
pinMode(PINA0, OUTPUT);
pinMode(PINA1, OUTPUT);
pinMode(PINA2, OUTPUT);
pinMode(PINSO, INPUT);
pinMode(PINCS, OUTPUT);
pinMode(PINSC, OUTPUT);
digitalWrite(PINEN, HIGH); // enable the mux all the time
digitalWrite(PINSC, LOW); // put clock in low
digitalWrite(PINCS, LOW); // stop conversion, start serial interface
// Timer0's overflow is used for millis() - setup to interrupt
// in the middle and call the "Compare A" function below
OCR0A = 0x80;
TIMSK0 |= _BV(OCIE0A);
}
void setup() {
Serial.begin(9600);
tcTempSetup();{
}
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:
while (1);
}
Serial.println("card initialized.");
}
// Interrupt is called every millisecond
SIGNAL(TIMER0_COMPA_vect) {
static byte ms = 0;
static byte ch = 0;
if (ms == 0) {
// select the thermocouple channel on the mux
digitalWrite(PINA0, ch & 1);
digitalWrite(PINA1, ch & 2);
digitalWrite(PINA2, ch & 4);
// ... wait a while for the capacitor on the ADC input to charge (< .1 mS actually needed)
} else if (ms == 5) {
// begin conversion
digitalWrite(PINCS, HIGH);
// ... wait 100 mS for conversion to complete
} else if (ms == 105) {
// stop conversion, start serial interface
digitalWrite(PINCS, LOW);
// 100nS min. delay implied
int rawTC = readSPI();
int rawIT = readSPI();
int tempC = rawTC / 4;
if (rawTC & 1) {
if (rawIT & 1) {
tempC = OPEN;
}
if (rawIT & 6) {
tempC = SHORT;
}
}
tcTemp[ch] = tempC;
if (++ch == 8) {
tcTemp[8] = rawIT / 64; // internal temperature reduced to quarter degree C
ch = 0;
}
ms = 255; // ++ will make this 0
}
ms++;
}
void loop() {
for (byte j = 0; j < 9; j++) {
//Serial.print((word)j);
//Serial.print('=');
// access thermocouple readings with interrupts disabled to prevent 'shearing'
noInterrupts();
int t = tcTemp[j];
interrupts();
if (t == OPEN) {
Serial.print(" , "); //Change , to Open to troubleshoot thermocouple
} else if (t == SHORT) {
Serial.print(" , "); //Change , to Short troubleshoot thermocouple
} else {
Serial.print(t / 4);
}
Serial.print(' ');
}
Serial.println("");
delay(1000); // delay(1000) milleseconds, set this for print time;
}