Sharing SPI bus with more than one device -- MOSI signal

I want to use the SPI bus to control two devices, SD card and TC72 temperature sensor. Both work well separately, but when I include the control of the TC72 in the code with the SD card control the MOSI signal remains always HIGH and cannot control the TC72 that requires to keep this signal LOW. The MOSI signal goes HIGH only after read the into the SD card, then remains always HIGH Could somebody tell me how to control the SPI pins and force the MOSI signal LOW? Highly appreciate some sujestions.

Post your code. The MOSI line is controlled by the Arduino, not the slave. The SD card has/had a bug that did not release the MISO line unless you did another SPI.transfer call.

edit: This is the data sheet. http://ww1.microchip.com/downloads/en/DeviceDoc/21743a.pdf The CS pin is reverse polarity for the SPI bus. The slave select should be HIGH for disabled and LOW for enabled.

MOSI = Master Out ---> Slave In Transmission from Master to Slave MISO = Master In ---> Slave Out Transmission from Slave to Master.OO

Thanks for your answers
Here include below a short piece of code as example. Without the SD card code the TC72 works well, when I include them (remove comment) doen´t work, the reading out are wrong.
I saw the lines MOSI and MISO work different with SD and whitout.
/*
Circuit:
TC72 sensor attached to pins 49, 50, 51, 52
SD card reader in Ethernet shield

CS: pin 49
MOSI: pin 51
MISO: pin 50
SCK: pin 52
*/

// the sensor communicates using SPI, so include the library:
#include <SPI.h>
#include <SD.h>
#define pin_MOSI 51
#define pin_MISO 50
#define chipSelectPin 49

char leo_;

File saveFile;

void setup()
{
pinMode(chipSelectPin, OUTPUT);

Serial.begin(9600);
SPI.begin();
SPI.beginTransaction(SPISettings(16000000, MSBFIRST, SPI_MODE1));
if (!SD.begin(4))
{
Serial.println(“ERROR - SD card initialization failed!”);
return; // init failed
}
Serial.println(“SUCCESS - SD card initialized.”);
// check for index.htm file
if (!SD.exists(“index.htm”))
{
Serial.println(“ERROR - Can’t find pagina1.htm file!”);
return; // can’t find index file
}
Serial.println(“SUCCESS - Found index.htm file.”);

//************************************************************************************
// Only a simulation to access the SD memory is enough to leave the MOSI signal HIGH
saveFile = SD.open(“index.txt”, FILE_READ);
leo_= saveFile.read();
saveFile.close();
//If the lines above are removed the MOSI pin remains LOW***********************
}

void loop()
{
digitalWrite(chipSelectPin, HIGH);
SPI.transfer(0x80); //Address byte
SPI.transfer(0x04); //Control byte
digitalWrite(chipSelectPin, LOW);

digitalWrite(chipSelectPin, HIGH);
SPI.transfer(0x02); //address byte
// read the three data from TC72
int result1 = SPI.transfer(0);
int result2 = SPI.transfer(0);
int result3 = SPI.transfer(0);
digitalWrite(chipSelectPin, LOW);

Serial.print (result1);
Serial.print (" “);
Serial.print (result2);
Serial.print (” ");
Serial.println (result3);
delay(10);
}

It appears the SD and your temp sensor work with two different SPI modes. The SD card likes mode 0, and you temp sensor likes mode 1. I use SPI.setDataMode. Try this. I don’t have a temp sensor, but it compiles and runs.

#include <SPI.h>
#include <SD.h>
#define pin_MOSI 51
#define pin_MISO 50
#define chipSelectPin 49

char leo_;

File saveFile;

void setup()
{
  // I use an ethernet card, so this sets the w5100 SPI disabled
  digitalWrite(10,HIGH);
  
  // this disables the temp sensor SPI  
  pinMode(chipSelectPin, OUTPUT); 
 
  Serial.begin(9600);
   SPI.begin(); 
// I commented out this call.
//   SPI.beginTransaction(SPISettings(16000000, MSBFIRST, SPI_MODE1));

  if (!SD.begin(4))
        {
        Serial.println("ERROR - SD card initialization failed!");
        return;    // init failed
        }
    Serial.println("SUCCESS - SD card initialized.");
    // check for index.htm file
    if (!SD.exists("index.htm"))
        {
        Serial.println("ERROR - Can't find pagina1.htm file!");
        return;  // can't find index file
        }
    Serial.println("SUCCESS - Found index.htm file."); 
 
//************************************************************************************
// Only a simulation to access the SD memory is enough to leave the MOSI signal HIGH 
    saveFile = SD.open("index.txt", FILE_READ); 
    leo_= saveFile.read();
    saveFile.close();
  // I added this transfer to release the MISO line
    SPI.transfer(0x00);
//*****If the lines above are removed the MOSI pin remains LOW****************************

  delay(1000);
}

void loop()
{
  // set SPI mode to 1
  SPI.setDataMode(SPI_MODE1);

  digitalWrite(chipSelectPin, HIGH);
  SPI.transfer(0x80); //Address byte
  SPI.transfer(0x04);  //Control byte
  digitalWrite(chipSelectPin, LOW);
 
  digitalWrite(chipSelectPin, HIGH);
  SPI.transfer(0x02);  //address byte
  // read the three data from TC72
  int result1 = SPI.transfer(0);
  int result2 = SPI.transfer(0);
  int result3 = SPI.transfer(0);
  digitalWrite(chipSelectPin, LOW); 

  // set SPI mode back to 0
  SPI.setDataMode(SPI_MODE0);
  

  Serial.print (result1);
  Serial.print ("  ");
  Serial.print (result2);
  Serial.print ("  ");
  Serial.println (result3);
  delay(10);
}

Works¡¡¡
Many thanks SurferTim.
Changing this sntence before the use of the SPI by the TC72 works fine
I included in my main code and works fine as well.
Subject closed.

SurferTim: The SD card has/had a bug that did not release the MISO line unless you did another SPI.transfer call.

Thanks so much for this one. I have been stuck on this bug for about 25 hours now and had not come across any other reference to it on endless searches. I'd managed to work out that the SD card was holding MISO high by removing the card and watching the line come down but had no idea what was going on or how to fix it. It turns out one dummy call to SPI.transfer() after the last File.close() without any slaves selected was all it took. It looks as though this bug is alive and well in 2018! If you're getting an invalid or unexpected byte back on the first transfer from a different SPI slave after you think you've finished with the SD card then this is a highly likely candidate. Thanks again for your post - I can now get my life back!