Another MAX31855 - SD card issue.

Hello,

I made my own shield for 6 MAX31855 devices where I want to measure temperature of six points via thermocouple at centrifugal casting machine. So I decided to record my measurments on SD card. First I made and tested my code on the Serial Monitor and it works great. But when I want to cooperate MAX31855 with SD card it returns not errors but zeroes.

I read this two topics: FIRST and SECOND

I made everything like fat16lib said in post #15 including his SdFat.h library but it doesn't resolved my problem.

Here's my first code:

#include <SPI.h>
#include <SdFat.h>
SdFat SD;
#include <Wire.h>
#include <Adafruit_MAX31855.h>

unsigned long czas;
const int LED = 2;
const int sdPIN = 4;
File datalog;

#define SO  13
#define SCK 12
#define CS1 11
#define CS2 10
#define CS3 9
#define CS4 8
#define CS5 7
#define CS6 6

Adafruit_MAX31855 termopara1(SCK, CS1, SO);
Adafruit_MAX31855 termopara2(SCK, CS2, SO);
Adafruit_MAX31855 termopara3(SCK, CS3, SO);
Adafruit_MAX31855 termopara4(SCK, CS4, SO);
Adafruit_MAX31855 termopara5(SCK, CS5, SO);
Adafruit_MAX31855 termopara6(SCK, CS6, SO);

void setup() {
  Serial.begin(9600);
  pinMode (LED, OUTPUT);
  Serial.println("Stabilizowanie MAX31855");
  delay(500);

  //wlaczenie karty sd
  Serial.println("Wlaczanie karty SD...");
  if (!SD.begin(sdPIN)){
    Serial.println("Blad karty lub karta nieobecna");
    return;
  }
  Serial.println("Karta wlaczona pomyslnie");

  //zarzadzanie plikami
  if (SD.exists("datalog.txt")) {
    Serial.println("Plik datalog.txt istnieje. Usuwanie...");
    SD.remove("datalog.txt");
    Serial.println("Tworzenie pustego pliku datalog.txt.");
    datalog = SD.open("datalog.txt", FILE_WRITE);
    datalog.print("Czas \t t1 \t t2 \t t3 \t t4 \t t5 \t t6");
    datalog.println(); 
    datalog.close();
  }
  else {
    Serial.println("Plik datalog.txt nie istnieje. Tworzenie pustego pliku...");
    datalog = SD.open("datalog.txt", FILE_WRITE);    
    datalog.print("Czas \t t1 \t t2 \t t3 \t t4 \t t5 \t t6");
    datalog.println(); 
    datalog.close();
  }
}

void loop() {
  digitalWrite (LED, HIGH);
  czas = millis();
 
  double temp1 = termopara1.readCelsius();
  double temp2 = termopara2.readCelsius();
  double temp3 = termopara3.readCelsius();
  double temp4 = termopara4.readCelsius();
  double temp5 = termopara5.readCelsius();
  double temp6 = termopara6.readCelsius();

  datalog = SD.open("datalog.txt", FILE_WRITE);
  if (datalog) {

    datalog.print(czas);
    datalog.print("\t");
      
    if (isnan(temp1)) {
      Serial.print("ERR");
      Serial.print("\t");
    } else {
      Serial.print(temp1);
      Serial.print("\t");
    }

    if (isnan(temp2)) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp2);
      datalog.print("\t");
    }

    if (isnan(temp3)) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp3);
      datalog.print("\t");
    }

    if (isnan(temp4)) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp4);
      datalog.print("\t");
    }

    if (isnan(temp5)) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp5);
      datalog.print("\t");
    }

    if (isnan(temp6)) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp6);
      datalog.print("\t");
    }
  
  datalog.println();
  }
  else {
    Serial.println("Nie mozna otworzyc datalog.txt");
  }
  datalog.close();
  digitalWrite (LED, LOW);
  delay (1000);

}

Can someone help me? At end I want to apologize for my poor english.

elaveiro:
Hello,

I made my own shield for 6 MAX31855 devices where I want to measure temperature of six points via thermocouple at centrifugal casting machine. So I decided to record my measurments on SD card. First I made and tested my code on the Serial Monitor and it works great. But when I want to cooperate MAX31855 with SD card it returns not errors but zeroes.

I read this two topics: FIRST and SECOND

I made everything like fat16lib said in post #15 including his SdFat.h library but it doesn't resolved my problem.

Here's my first code:

#include <SPI.h>

#include <SdFat.h>
SdFat SD;
#include <Wire.h>
#include <Adafruit_MAX31855.h>

#define SO  13
#define SCK 12
#define CS1 11
#define CS2 10
#define CS3 9
#define CS4 8
#define CS5 7
#define CS6 6

The SD library uses the SPI interface which uses pins (13,12,11)

pin13 is SCK (CLOCK) OUTPUT
pin12 is MISO (MasterInSlaveOut) INPUT
pin11 is MOSI (MasterOutSlaveIn) OUTPUT

you are trying to use MISO (pin12) as an output with the ADAFRUIT_MAX31855 library
and as an input with the SPI(SD) library.

won't work.

don't use pins (11,12,13) for the adafruit library, use (5,3,2) or some of the Analog Pins A0..A3

chuck.

Thank you for your answer,

problem is that I've made pcb already, I thought that connecting via ICSP won't be troubleshooting. Is any way to switch on and off these pins in case when im using MAX or SD card? I'm opening it in every loop anyway, so is it way to solve the problem too?

EDIT: I made test, and when I make measurments before SD begin working I recive first record, now I have to find way to disable SD card after every use. If i"m correct I will loose ca. 20 ms for every begining of SD card.

elaveiro:
Thank you for your answer,

problem is that I've made pcb already, I thought that connecting via ICSP won't be troubleshooting. Is any way to switch on and off these pins in case when im using MAX or SD card? I'm opening it in every loop anyway, so is it way to solve the problem too?

you can try:

// use a different name for your SCK, SCK is defined to pin 13 on UNO's your redefining it to pin
// 12 could also be messing up SD
#define mySCK 12

// and you are going to have to not use Pin11 for CS1, Pin11 is MISO and is required by the SPI library.
// it is the pin that all data is sent out when using SPI (SD library).  so every low bit would cause the MAX31855 on CS1 try to write data out on SO.

#define CS1 A0  // rewire the MAX31855 to A0

Adafruit_MAX31855 termopara1(mySCK, CS1, SO);

//before each call to any of the SD functions
SPI.begin(); // this is initialize the SPI hardware, Pin11,12,13

SD.whatever();

//before your calls to AdaFruit's Max31855 library
SPI.end();

pinMode(SO,INPUT);
pinMode(mySCK,OUTPUT);

so, to modify your loop()

void loop() {
  digitalWrite (LED, HIGH);
  czas = millis();
 
 
  SPI.end();
  pinMode(SO,INPUT);  
  pinMode(mySCK,OUTPUT);

  
  double temp1 = termopara1.readCelsius();
  double temp2 = termopara2.readCelsius();
  double temp3 = termopara3.readCelsius();
  double temp4 = termopara4.readCelsius();
  double temp5 = termopara5.readCelsius();
  double temp6 = termopara6.readCelsius();

  SPI.begin();

  datalog = SD.open("datalog.txt", FILE_WRITE);
  if (datalog) {

    datalog.print(czas);
    datalog.print("\t");
     
    if (isnan(temp1)) {
      Serial.print("ERR");
      Serial.print("\t");
    } else {
      Serial.print(temp1);
      Serial.print("\t");
    }

    if (isnan(temp2)) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp2);
      datalog.print("\t");
    }

    if (isnan(temp3)) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp3);
      datalog.print("\t");
    }

    if (isnan(temp4)) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp4);
      datalog.print("\t");
    }

    if (isnan(temp5)) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp5);
      datalog.print("\t");
    }

    if (isnan(temp6)) {
      datalog.print("ERR");
      datalog.print("\t");
    } else {
      datalog.print(temp6);
      datalog.print("\t");
    }
 
  datalog.println();
  }
  else {
    Serial.println("Nie mozna otworzyc datalog.txt");
  }
  datalog.close();
  digitalWrite (LED, LOW);
  delay (1000);

}

Rewire CS1 to A0,

No Warrantee! :confused:

Chuck.

elaveiro:
Thank you for your answer,

problem is that I've made pcb already, I thought that connecting via ICSP won't be troubleshooting.

The ISP header is a standard pinout, all of the Arduinos use the same 6 pin header for In System Programming, but each different model of Arduino can use different pins for MISO, MOSI, SCK, Reset. The six pit header is standardized. On the Uno pins 11..13 go to the ISP, on the MEGA2560 50..52 go to the ISP header.

Chuck.

Dear Chuck,

it worked! Thank you very much!

Now I have to do two things, reconnect PIN 11 on my PC to eg. pin 4

Now one loop take a lot of time 400 - 500 ms, is ant way to make it faster? Without SD card my shield worked in ca. 70ms loop. (I turned off delay)

EDIT: my bad, without SD card saving measurment take 400 - 500 ms too
EDIT2: 32 bit value causes that it take so long, I dont know if is way to make it faster ;/

Hello again. As I said your idea works, but I have problem - I suspect that it is caused while SPI ending and beginning. Datalogging sometimes suspend or not log time value. I descriped it here: millies() doesn't work on sdcard datalogger - Storage - Arduino Forum

@chucktodd Do you know how to repair it? I tried using delays but it didn't help.

Cheers,
Elav