Go Down

Topic: [Still having issues] SD - MAX31855 Conflict (Read 2805 times) previous topic - next topic

Cowboy303

Feb 02, 2015, 02:52 am Last Edit: Feb 22, 2015, 07:27 pm by Cowboy303
Howdy Guys.

I've got some issues with a project I'm working on.  I'm using a Due with a MAX31855 thermocouple reader, along with an SD card (both SPI).  The issue I'm having is that I can't get both to work at the same time.  By changing the code a little, I can get the SD to work, but then the MAX31855 doesn't seem to be sending any info.  Then if I change it a bit to get the MAX31855 to work as it should, the SD card stops working.  My code is pretty long and split up between a lot of functions, but I was able to replicate the problem with this simpler sketch.

(MAX31855 Library http://forum.arduino.cc/index.php?topic=208061.0)

Code: [Select]

/*
  SD card datalogger

 This example shows how to log data from three analog sensors
 to an SD card using the SD library.

 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:

SD Chip select pin is 53
MAX31855 Chip select pin is 22

 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */
#include "MAX31855.h"
#include <SPI.h>
#include <SD.h>

//for the CD4051
#define CDA 14
#define CDB 15
#define CDC 16

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
MAX31855 tc(SCK, 22, MISO);


const int SDchipSelect = 53;

void setup()
{
pinMode(CDA, OUTPUT);
pinMode(CDB, OUTPUT);
pinMode(CDC, OUTPUT);
 

  // Open serial communications and wait for port to open:
    Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
delay(50);

  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(53, OUTPUT);
delay(50);
// see if the card is present and can be initialized:
  if (!SD.begin(SDchipSelect)) {
   Serial.println("Card failed, or not present");
   // don't do anything more:
  return;
 }
  Serial.println("card initialized.");
delay(50);
tc.begin();
delay(50);
}

void loop()
{
  // make a string for assembling the data to log:
  String dataString = "";
delay(50);
  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }
delay(50);
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
delay(50);
  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
delay(50);

digitalWrite(CDA, HIGH);
digitalWrite(CDB, LOW);
digitalWrite(CDC, HIGH);

delay(50);
  Serial.print("Start max31855_demo4: ");
  Serial.println(MAX31855_VERSION);
  Serial.println();
delay(50);
 

  uint32_t start = micros();
  for (int i=0; i< 10; i++) tc.read();
  uint32_t stop = micros();
  Serial.print("10x read:\t");
  Serial.println(stop - start);
delay(50);

  start = micros();
  float t1 = tc.getTemperature();
  stop = micros();
  Serial.print("getTemperature:\t");
  Serial.println(stop - start);
  Serial.println(t1, 2);
  Serial.println();

delay(50);
  start = micros();
  tc.setOffset(2.0);
  stop = micros();
  Serial.print("setOffset:\t");
  Serial.println(stop - start);
delay(50);
  tc.read();
  start = micros();
  t1 = tc.getTemperature();
  stop = micros();
  Serial.print("getTemperature:\t");
  Serial.println(stop - start);
  Serial.println(t1, 2);
  Serial.println();
delay(50);

  start = micros();
  float t2 = tc.getInternal();
  stop = micros();
  Serial.print("getInternal:\t");
  Serial.println(stop - start);
  Serial.println(t2, 4);
  Serial.println();

delay(10000);
}


I get this in the serial monitor (SD card not working)



But, if you put the "tc.begin();" from the bottom of the "void setup()" to the top, then you get this (MAX31855 not working).


the delays in the code were just thrown in there to see if that would fix it.
I don't think it's my powersupply, I've got two separate regulators powering each item.

Any help would be great, thanks!

SurferTim

You shouldn't have any trouble with the SPI bus settings. They seem to be the same.

I recommend disabling both SPI slave selects first thing in setup. Something like this.
Code: [Select]
void setup() {
  pinMode(53,OUTPUT);
  digitalWrite(53,HIGH);

  pinMode(22,OUTPUT);
  digitalWrite(22,HIGH);

// rest of your setup

How does that do?

Cowboy303

Thank you for your help!  I added that to the code and no change.  I also set the Slave select pin HIGH after each time I use it, and still nothing.

Cowboy303

#3
Feb 02, 2015, 05:34 am Last Edit: Feb 02, 2015, 05:56 am by Cowboy303
OK well, a little bit of an update on this.  After monkeying with my code, I didn't find the issue, but may have found a bandaid.

First I setup the SD card "sd.begin()" and then opened and closed the datalogfile without writing anything to it.  Then I did the tc.begin(), and then the rest of "void setup()" and so far no issues.  So until I find the actual fix, or it starts being a problem again, I just use that.

Code: [Select]

/*
  SD card datalogger

 This example shows how to log data from three analog sensors
 to an SD card using the SD library.

 The circuit:
 * analog sensors on analog ins 0, 1, and 2
 * SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4

 created  24 Nov 2010
 modified 9 Apr 2012
 by Tom Igoe

 This example code is in the public domain.

 */
#include "MAX31855.h"
#include <SPI.h>
#include <SD.h>

//for the CD4051
#define CDA 14
#define CDB 15
#define CDC 16

// On the Ethernet Shield, CS is pin 4. Note that even if it's not
// used as the CS pin, the hardware CS pin (10 on most Arduino boards,
// 53 on the Mega) must be left as an output or the SD library
// functions will not work.
MAX31855 tc(SCK, 22, MISO);


const int SDchipSelect = 53;

void setup()
{
    pinMode(53,OUTPUT);
  digitalWrite(53,HIGH);

  pinMode(22,OUTPUT);
  digitalWrite(22,HIGH);

    Serial.begin(115200);
File dataFile = SD.open("datalog.txt", FILE_WRITE);// Open and close the file before "tc.begin" as a bandaid.
  if (dataFile) {
    dataFile.close();
  }
tc.begin();

delay(50);
 
pinMode(CDA, OUTPUT);
pinMode(CDB, OUTPUT);
pinMode(CDC, OUTPUT);
 

  // Open serial communications and wait for port to open:
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }
delay(50);

  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(53, OUTPUT);
delay(50);
// see if the card is present and can be initialized:
  if (!SD.begin(SDchipSelect)) {
   Serial.println("Card failed, or not present");
   // don't do anything more:
  return;
 }
  Serial.println("card initialized.");
delay(50);

}

void loop()
{
  // make a string for assembling the data to log:
  String dataString = "";
delay(50);
  // read three sensors and append to the string:
  for (int analogPin = 0; analogPin < 3; analogPin++) {
    int sensor = analogRead(analogPin);
    dataString += String(sensor);
    if (analogPin < 2) {
      dataString += ",";
    }
  }
delay(50);
  // open the file. note that only one file can be open at a time,
  // so you have to close this one before opening another.
  File dataFile = SD.open("datalog.txt", FILE_WRITE);
delay(50);
  // if the file is available, write to it:
  if (dataFile) {
    dataFile.println(dataString);
    dataFile.close();
    // print to the serial port too:
    Serial.println(dataString);
  }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
delay(50);

digitalWrite(CDA, HIGH);
digitalWrite(CDB, LOW);
digitalWrite(CDC, HIGH);

delay(50);
  Serial.print("Start max31855_demo4: ");
  Serial.println(MAX31855_VERSION);
  Serial.println();
delay(50);
 

  uint32_t start = micros();
  for (int i=0; i< 10; i++) tc.read();
  uint32_t stop = micros();
  Serial.print("10x read:\t");
  Serial.println(stop - start);
delay(50);

  start = micros();
  float t1 = tc.getTemperature();
  stop = micros();
  Serial.print("getTemperature:\t");
  Serial.println(stop - start);
  Serial.println(t1, 2);
  Serial.println();

delay(50);
  start = micros();
  tc.setOffset(2.0);
  stop = micros();
  Serial.print("setOffset:\t");
  Serial.println(stop - start);
delay(50);
  tc.read();
  start = micros();
  t1 = tc.getTemperature();
  stop = micros();
  Serial.print("getTemperature:\t");
  Serial.println(stop - start);
  Serial.println(t1, 2);
  Serial.println();
delay(50);

  start = micros();
  float t2 = tc.getInternal();
  stop = micros();
  Serial.print("getInternal:\t");
  Serial.println(stop - start);
  Serial.println(t2, 4);
  Serial.println();

delay(10000);
}












Thanks again SurferTim

EDIT:  Well I spoke to soon, yes it doesn't throw error codes anymore, and it can even put a file on the SD card, but I can't seem to get it to write anything to the SD card...  I'll look at it more in the morning.

SurferTim

It may be a small bug in the SD library. Read this thread, especially reply #15 by fat16lib.


Cowboy303

Thank you for the link, I tried making the changes in the library, and still nothing.  So I tried the SdFat library.  Still not working!  I'm stumped.

Cowboy303

#6
Feb 15, 2015, 07:40 pm Last Edit: Feb 15, 2015, 07:41 pm by Cowboy303
Well I found the issue, and a bandaid, but would like to solve it for good.

If I'm understanding everything correctly,  the issue is that the MAX31855 library I'm using doesn't use the 'hardware spi' and needs the pins as regular input/outputs.  While the SD card does use the 'hardware spi' and so once you call to "sd.begin" it locks down the pins so the TC library can no longer control them with "digitalWrite(MISO, HIGH);"

There are 3 fixes.

Change the MISO and SCK lines to something other then the hardware SPI pins. (I'd rather not, cause the PCB is already made, and I'm a little tight for pins).

Change the Library to use hardware SPI.  (This is the route I'm likely to take,  if anyone knows of a premade library that is, that would great).

Or the Bandaid.  Call sd.begin(); before every time you use the SD card, and SPI.end(); every time when your done.  That unlocks the pins and allows the TC library to use them.  You also need to call tc.begin every time before using the MAX31855.

The issue with the bandaid for me, is that it take 20 millis for the "sd.begin()" and I'd like this to go as fast as possible.  Otherwise it works flawlessly.

Cowboy303

#7
Feb 16, 2015, 08:12 am Last Edit: Feb 22, 2015, 07:38 pm by Cowboy303
Well I got it to work, I'll post up the library when I get a chance to clean it up a little.

EDIT:  Cleaned it up, but found an issue between it and the SDFat library.  The SD card was screwing up the bits read from the thermocouple.  Removing the SD card would fix the issue.  I did a bit of testing and saved the data.  So far switching back to the SD.h library seems to have fixed it.  Using the SDFat library took my datalogging times from 20 milliseconds to 11ms.  So I'd really like to use it.

I'll put the Data I collected in a new post, as editing this one doesn't have the picture posting options.

Cowboy303

Here's the Data I've collected;

This is a Serial.print of the sensor working correctly, those are the 4 bytes being sent correctly, you can see the 1st byte incrementing and the second going from 0-255 like they should.



Here are the bytes received when I'm datalogging.  You can see the first byte is stuck at 7, though the second byte still goes from 0-255 as it should, so the temp it's sending hovers around 244*F and then jumps to 364*F.
Also, Sometimes the internal temp reads about 16*C higher than it should (e.g 48 instead of 32).



This is how I read the bytes.
Code: [Select]
uint32_t MAX31855::_read(void)
{
    uint32_t value = 0;

    digitalWrite(_tccs, LOW);
_byte1 = (SPI.transfer(0x00));
    _byte2 = (SPI.transfer(0x00));
    _byte3 = (SPI.transfer(0x00));
    _byte4 = SPI.transfer(0x00);
    digitalWrite(_tccs, HIGH);
   

value = ((_byte1 << 24) | (_byte2 << 16) | (_byte3 << 8) | _byte4);
return value;
}

elaveiro

#9
Sep 17, 2015, 01:26 pm Last Edit: Sep 17, 2015, 01:26 pm by elaveiro
How directly you solved this problem? I got this same, my MAX31855 shield works grat via Serial Monitor but when I want to use recording to SD card I've got zeros on return. Here's topic I created: link

Cowboy303

Thank you for bringing this back up.  Yes I ended up getting it fixed. I believe I had to reworked the library.  The problem was what I mentioned above, the SD card uses hard spi, and once you run "sd.begin" it locks down those pins, but with the MAX we were doing everything manually.  And so that was an issue.

I'll post up the library I made later.

Go Up