Problems with Arduino Uno, SPI microSD module and SPI MAX6675

I am trying to measure temperature with a termocouple and MAX6675 and record these measurements on a microSD. I have had problems with this and I found a common answer that was that the microSD module does not release the SPI bus so other devices can't work. So I put a tri-state buffer liked to the CS of the microSD module to isolate it from the SPI bus when not in use. But this did not work either.

I later wrote a code just to read the thermocouple temp and all works fine. I later added a line that will start the SD module, but I do not connect anything, and the thermocouple reading stops. (Arduino just has the max6675 connected to it and nothing else). This is the code I am using when this happens.

/*
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 10 
*/

unsigned long temp;
int i=0;

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

int thermoDO = 12;
int thermoCS = 9;
int thermoCLK = 13;

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

File myFile;

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect.
  }


  Serial.println("MAX6675 test");
  // wait for MAX chip to stabilize
  delay(500);


}

void loop() {
  i=i+1;
  temp=thermocouple.readCelsius();
  Serial.println(temp);
  delay(1000);

//After 5 reads initialize SD
  if (i==5){
    SD.begin(10);
  }
}

This is my code, and if the microSD module is connected or not, temp is always 0 after initializing SD. What could be the problem with this? I would really need to record this information on the microSD card.

Thanks

The problem is the MISO pin, not the CS pin. Can you post a schematic?

Where did you download this library? When I searched for that library, I found one (Adafruit) that does not use hardware SPI, so could easily be connected to any other 3 pins on the Arduino, and may not work if it uses the hardware SPI pins and another library is using the hardware SPI port.

Hi, @mafulynch
I use this module with the MAX6675 to measure the oven temperature.
I don't use Arduino's SPI pins.
I use the analog pins as digital defined like this:


int miso = A2; // DIGITAL PIN (SO) Orange
int cs =   A1; // DIGITAL PIN (CS) Yellow
int sclk = A0; // DIGITAL PIN (CLK / SCK) Brown

and it works very well.

What I did was connect the CS from the SD module also to the OE of the tri-state buffer. That way it will isolate the MISO pin from the SPI bus when not using the SD module.
The problem is that even if the micro sd module is not connected, as soon as the SD.begin() command is run in the code the thermocouple stops working (this is with just the thermocouple connected to the arduino).

Using different pins for the MAX6675 did solve the problem. I just find it qurious why is it that the MAX6675 on the arduino SPI pins will stop working after the SD.begin(). Maybe it is a problem with the SD.h library or the max6675.h library.

Anyway with your help and changint the max6675 pins it is working now. Thank you

I suspect that SD.begin() enables the hardware SPI to use the MISO & MOSI pins, which stops digitalWrite() from working with those pins. The MAX6675 library uses software SPI and digitalWrite(), so it stops working at that point.

If you could find a MAX6675 library which also uses hardware SPI, and that library was written correctly using SPI.beginTransaction() and SPI.endTransaction() so that it supports multiple devices on the SPI bus, you could continue to use the same pins for both devices. I did not find such a library, but I did not look for long.

See this: MAX6675 with hardware SPI - Arduino Reference

1 Like

I am having a similar problem with a microSD and an SSD1306 OLED display with an Arduino nano. The OLED is using the SCL and SDA pins on the Arduino and the microSD is using the MISO, MOSI, SCK and Digial 4 or 8 pin for the CS. The program crashes at the SD.begin() line of the code. Is there an small OLED that doesn't use the SPI or a microSD that is not SPI?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.