Arduino UNO, with a MAX31855 and SDcard reader from adafruit

Hi all,

Im working on a data logging project and I am using a Arduino UNO MAX31855 and SDcard reader from adafruit. I got it all working by having the SD card run on hardware SPI on pins 11(DI), 12(DO) and 13(CLK) and the MAX31855 on software SPI on pins i defined. I am trying to reduce the amount of digital pins im using so i want to put both devices on (share) pins 12 and 13 and assign each a CS. For some reason I cant get it to work.
I attached my working code at the top and on the bottom what i modified that wasnt working. Any help would be greatly appreciated. BTW I search high and low and I can find anything applicable to my problem.

/*
 SD card datalogger
 logging temperture from a max31855 and photoresistor
*/

#include <SPI.h>
#include <SD.h>
#include "Adafruit_MAX31855.h"

// Defining the pin outputs for the MAX Thermocouple
#define MAXDO   5
#define MAXCS   4
#define MAXCLK  3
#define sensorPin A0

// initialize the Thermocouple
Adafruit_MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);

//-----CS for microSD-------------
const uint8_t chipSelect = 10;

void setup() {

  pinMode(MAXCS, OUTPUT);
  pinMode(chipSelect, OUTPUT);
  sd_card_initialize();
}

void loop() {
// Activate MAX31855, Deactivate SD card
 digitalWrite(MAXCS, HIGH);
 digitalWrite(chipSelect, LOW);
 
 float thermo_reading = max31855_function();
 Serial.print("Current temp = ");
 Serial.print(thermo_reading);
 Serial.println( "C");

 float photo_reading = analogRead(sensorPin);
 Serial.print("Current light intensitiy = ");
 Serial.println(photo_reading);

 //Activate SD card, Deactivate MAX31855
 digitalWrite(MAXCS, LOW);
 digitalWrite(chipSelect, HIGH);

 File dataFile = SD.open("datalog.txt", FILE_WRITE);

 if (dataFile) {
  dataFile.print("Current temp = ");
  dataFile.print(thermo_reading);
  dataFile.println( "C");

  dataFile.print("Current light intensitiy = ");
  dataFile.println(photo_reading);
  dataFile.close();
 }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
delay(1000);
}
void sd_card_initialize(){
    // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print("Initializing SD card...");
  
  // see if the card is present and can be initialized:
  if (!SD.begin()) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");

}
//------------------------------------------

float max31855_function(){
  float x; 
  x = thermocouple.readCelsius();
  if (isnan(x)){
    Serial.println("Something wrong with thermocouple!");
    } 
  else {
      return x;
      }
}
/ Defining the pin outputs for the MAX Thermocouple
#define MAXCS   4

// initialize the Thermocouple
Adafruit_MAX31855 thermocouple( MAXCS);

//-----CS for microSD-------------
const uint8_t chipSelect = 10;

I attached my working code at the top and on the bottom what i modified that wasnt working.

What is the code in the middle?

What does "not working" mean? The code does something. You need to tell us EXACTLY what it does. You expect it to do something. Presumably, that is not the same as what it actually does. But, how those things differ is a complete mystery.

The code in the middle are functions that i use in the main code. The code overall is meant to sample temperature and luminosity and write it to the SD card every 1s.

The top section of code is my code that is working with the MAX31855 on software driven SPI and the SD card on hardware driven SPI. this method takes up alot of pins.

My issue is, I am trying to reduce the number of digital pins. When I try to share the pin13(CLK) and pin12(DO) between the MAX31855 and SD card, my SD card fails to initialize. The bottom section of code is where I am trying to do this.

The bottom section of code is where I am trying to do this.

The bottom snippet of code makes no attempt to initialize the SD card.

Post ALL of your real code in ONE piece.

let me go about this differently, here is my modified code where I want to share pins 12 and 13. I dont know why it doesn’t work. To my knowledge, I am forcing the MAX31855 to use hardware SPI (pin12 and 13) and telling it pin 4 for CS.

/*
 SD card datalogger
 logging themperture from a max31855 and photoresistor
*/

#include <SPI.h>
#include <SD.h>
#include "Adafruit_MAX31855.h"

// Defining the pin outputs for the MAX Thermocouple
//#define MAXDO   5
#define MAXCS   4
//#define MAXCLK  3
#define sensorPin A0

// initialize the Thermocouple
Adafruit_MAX31855 thermocouple(MAXCS);
//-----CS for microSD-------------
const uint8_t chipSelect = 10;

void setup() {

  pinMode(MAXCS, OUTPUT);
  pinMode(chipSelect, OUTPUT);
  sd_card_initialize();
}

void loop() {
// Activate MAX31855, Deactivate SD card
 digitalWrite(MAXCS, HIGH);
 digitalWrite(chipSelect, LOW);
 
 float thermo_reading = max31855_function();
 Serial.print("Current temp = ");
 Serial.print(thermo_reading);
 Serial.println( "C");

 float photo_reading = analogRead(sensorPin);
 Serial.print("Current light intensitiy = ");
 Serial.println(photo_reading);

 //Activate SD card, Deactivate MAX31855
 digitalWrite(MAXCS, LOW);
 digitalWrite(chipSelect, HIGH);

 File dataFile = SD.open("datalog.txt", FILE_WRITE);

 if (dataFile) {
  dataFile.print("Current temp = ");
  dataFile.print(thermo_reading);
  dataFile.println( "C");

  dataFile.print("Current light intensitiy = ");
  dataFile.println(photo_reading);
  dataFile.close();
 }
  // if the file isn't open, pop up an error:
  else {
    Serial.println("error opening datalog.txt");
  }
delay(1000);
}


void sd_card_initialize(){
    // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print("Initializing SD card...");
  
  // see if the card is present and can be initialized:
  if (!SD.begin()) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    while (1);
  }
  Serial.println("card initialized.");

}
//------------------------------------------

float max31855_function(){
  float x; 
  x = thermocouple.readCelsius();
  if (isnan(x)){
    Serial.println("Something wrong with thermocouple!");
    } 
  else {
      return x;
      }
}

Anyone? The real question here is does the libraries I’m including allow more then 1 device on the SPI protocol.

Some years ago, I tried to share the SPI bus between a W5100 Ethernet controller and the MAX31855. I could not get it to work so eventually I gave up and went back to software SPI on the MAX31855.

My understanding is that, although it's intended to be a shared bus, the SPI protocol is not well standardized so that doesn't always work as we would expect. I wasn't as experienced at that time and I didn't spend a ton of time on it since the use of software SPI didn't pose a significant problem for the project so it may have been there was a solution I didn't find.

I was looking at the .cpp files in the library and it seems if you set the sclk = miso = -1 it forces it on hardware spi, then I’m guess I just have to set a CS pin. I’ll have to try this and get back. I’m the mean time I’m hoping someone can’t chime in!

I'm the mean time I'm hoping someone can't chime in!

Can't?

It’s a typo, can chime in.

I am a little disappointed that the only comments I can get is calling me out on a typo.....

If i wiring the SD and Max31855 on the same DO and CLK pins I cannot get the SD card to initialize. I have set different CS pins, toggled them when using each device but I cannot get the SD card to initialize.

Anybody?!?