reading two sensors across shared MISO line?

Hello, I am currently doing a school project. I have a L293D chip controlling a motor. I have tested the IC and my code in real life with just the L293D controlling the motor, and it is fine.

But I want to have two temperature sensors, connected to the arduino where the difference between the two temperatures is above a certain degree celsius, the motor will start up.

Using Proteus for simulation so I don’t burn anything. So far I’ve been unsuccessful, and the motor always sits still and cannot start no matter what I change the two temperatures at the two thermocouples to. At first I thought it was because I needed the the two max31855 ics to be read at different times, so set cs low and high at different times, but it seems to have no effect…

So I need help.

My code is below:

#include <MAX31855.h>   // MAX31855 library       //                                                     
#include <SPI.h>             // Standard SPI library    //                                                      
//-------------------------------------------------------------------------------------------------------------
//PIN OUTS & GLOBAL VARIABLES SECTION                                                                               
//-------------------------------------------------------------------------------------------------------------
//L293D, controls motor
const int EnablePin = 3; // enable pin controls current supply to output
const int pwmA  = 5;  // Pin no. 2, name 1A of L293--> arduino PWM pin 5
const int pwmB  = 6;  // Pin no. 7, name 2A of L293--> arduino PWM pin 6

//-------------------------------------------------------------------------------------------------------------
//MAX31855 pinout connections                                                                                       
//-------------------------------------------------------------------------------------------------------------
// Map SPI port to 'new' Pin connections.  
const uint8_t  CHIP_SELECTa = A1;                        // Chip-Select PIN for sensor B  //
const uint8_t  CHIP_SELECTb = A2;                        // Chip-Select PIN for sensor A  //
class MAX31855 tca(MISO, CHIP_SELECTa, SCK);
class MAX31855 tcb(MISO, CHIP_SELECTb, SCK);

const unsigned long SECONDS = 1000;        //one second
const unsigned long HOUR = 3600*SECONDS;   //one hour

   int pwmValue;      // sets speed


void setup() 
{
//set motor IC pins
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  pinMode(EnablePin,OUTPUT);
//set direction to counterclockwise
  digitalWrite(pwmA, LOW);
  digitalWrite(pwmA, HIGH);


  loop();                         //go to loop function
}

void loop() 
{

  //set CS to low for reading chip A
  digitalWrite(CHIP_SELECTa, LOW);            //can now read chip A
  tca.readThermocouple(CELSIUS);              // get the temp @ tca
  float temptca =  tca.readThermocouple(CELSIUS); //write to var
  delay(1000);
  digitalWrite(CHIP_SELECTa, HIGH);           //turn off
  
  //set CS to low for reading chip B
  digitalWrite(CHIP_SELECTb, LOW);            // get the temp @ tcb
  tcb.readThermocouple(CELSIUS);              // get the temp @ tcb
  float temptcb =  tcb.readThermocouple(CELSIUS); //write to var
  delay(1000);
  digitalWrite(CHIP_SELECTb, HIGH);           //turn off

  //initiate delta T variable, delta T == tca - tcb
  int deltaT    =  temptca - temptcb;

  if ( deltaT > 2)     // 2c is the theshold temperature difference
                            // @ minimum threshold enter into while loop
  {
      while ( (temptca - temptcb) > 10) //@ peak deltaT, run at full speed
      {
        digitalWrite(EnablePin,HIGH); //ENABLE signal --> motor driver
       }
   else
   {
      digitalWrite(EnablePin,LOW); //DISABLE motor driver
      return;
   }
}

temp only.ino (3.1 KB)

From your schematic drawing you have the sensors connected to I2C instead of SPI.

On the UNO pin 13 is SLK and pin 12 is SO. You must ensure that pin 10 is also set as output (use it as one of the CS pins) for the SPI to work as master mode else it will be in slave mode.

That's an interesting one. More info on Adafruit's site as usual.

SPI but only one way, MOSI is not connected. Your wiring is indeed wrong, that's the I2C pins.

Basically: - sensor's SCK connects to Arduino SCK, by default pin 13 on the Uno (all sensors to the same pin - SPI bus) - sensor's SO or DO connects to Arduino MISO, by default pin 12 on the Uno (all sensors to the same pin - SPI bus) - sensor's CS connects to any free pin - this must be a separate pin for each sensor.

OK, so I’ve gone and changed the pins.

I’ve also decided to take a step back and use only one max31855 just to test if I have the code right.

So the program goes as follows basically:

  • CS is normally high
  • Every two seconds, CS is set low and a reading is taken
  • the read value is written to a variable
  • then CS is set high again.

After this, an IF statement, goes,
IF temperature of the thermocouple hits 30C the motor is enabled, etc,
ELSE motor is disabled.

The problem so far is that the CS pin seems to stay high no matter what.

Code is below:

#include <TimeLib.h>    // time and clock library //                                                     
#include <MAX31855.h>   // MAX31855 library       //                                          
#include <SPI.h>        // Standard SPI library   //                                                     
//--------------------------------------------------
//PIN OUTS & GLOBAL VARIABLES SECTION                                                                               
//--------------------------------------------------
//L293D, controls motor
const int EnablePin = 3; // enable pin controls current supply to output
const int pwmA  = 5;  // Pin no. 2, name 1A of L293--> arduino PWM pin 5
const int pwmB  = 6;  // Pin no. 7, name 2A of L293--> arduino PWM pin 6

//--------------------------------------------------
//MAX31855 pinout connections                                                                                       
//--------------------------------------------------
// Map SPI port to pin connections.  
#define CHIP_SELECTa 11             // Chip-Select PIN for sensor B      //
#define MISO         12             // Master-in-Slave Output --> pin 12 //
#define SCK          13             // SPI Clock common                  //
class MAX31855 tca(MISO, CHIP_SELECTa, SCK);

const unsigned long SECONDS = 1000;        //one second
const unsigned long HOUR = 3600*SECONDS;   //one hour


void setup() {
//set motor IC pins
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  pinMode(EnablePin,OUTPUT);
//set direction to counterclockwise
  digitalWrite(pwmA, LOW);
  digitalWrite(pwmA, HIGH);
//serial bus
  Serial.begin(9600);
//initiate SPI bus
  SPI.begin();
  pinMode(CHIP_SELECTa, OUTPUT);
//jump to loop function
  loop();
}

void loop() {
  //CS is normally high
  digitalWrite(CHIP_SELECTa, HIGH);            //can now read chip A
  delay(2*SECONDS);                            //takes a reading every 2 seconds
  //set CS to low for reading chip A
  digitalWrite(CHIP_SELECTa, LOW);            //can now read chip A
  delay(200);                                 //give it 1/5 second
  tca.readThermocouple(CELSIUS);              // get the temp @ tca
  float temptca =  tca.readThermocouple(CELSIUS); //write to var
  digitalWrite(CHIP_SELECTa, HIGH);            //disable read chip
  
  if ( temptca >= 30) //@ peak temptca, run at full speed
      {
        digitalWrite(EnablePin,HIGH); //ENABLE signal --> motor driver
      }
  if ( temptca > 25 && temptca < 30) //@medium temptca, run at half speed
      {
        analogWrite(EnablePin,128); //ENABLE signal --> motor driver
      }
  else
      {
        digitalWrite(EnablePin,LOW); //DISABLE motor driver
      }
  loop();
}

You cannot use pin 11 for a CS line, it’s a dedicated MOSI pin. Use any pin in the 2 to 10 range for your two CS pins.

WattsThat: You cannot use pin 11 for a CS line, it’s a dedicated MOSI pin.

Not sure about that! It's designated as MOSI but can be used for other things normally... Any reference about this?

OK took a second stab at it. I think I’ve figured out the problem. My code is (or was) fine, the problem with exiting the while loop was the data itself.

I suspect that the “readThermocouple(CELSIUS)” function returns a serial or some other string type data to the Arduino, rather than integer type, which I will have to convert the data back to an integer type somehow… :o

#include <MAX31855.h>   // MAX31855 library       // 
#include <TimeLib.h>             // time and clock library // 
#include <SPI.h>                 // Standard SPI librarys  //

const int EnablePin = 3; // enable pin controls current supply to output
const int pwmA  = 5;  // Pin no. 2, name 1A of L293--> arduino PWM pin 5
const int pwmB  = 6;  // Pin no. 7, name 2A of L293--> arduino PWM pin 6

const int CHIP_SELECTa = 10;        // Chip-Select PIN for sensor B      //
const int MISOa        = 12;        // Master-in-Slave Output --> pin 12 //
const int SCKa         = 13;        // SPI Clock common                  //

class MAX31855 tca(SCKa, CHIP_SELECTa, MISOa );


volatile int temptca = (float) tca.readThermocouple(CELSIUS); 
//variable temperature temptca is double, 

const unsigned long SECONDS = 1000;        //one second
const unsigned long HOUR = 3600*SECONDS;   //one hour

void setup();
void loop();

void setup() {
  Serial.begin(9600);
//set motor IC pins
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  pinMode(EnablePin,OUTPUT);
  digitalWrite(EnablePin,LOW);
//set direction to counterclockwise
  digitalWrite(pwmA, LOW);
  digitalWrite(pwmA, HIGH);
//MAX31855
  pinMode(CHIP_SELECTa,OUTPUT);

  loop();
}

void loop() 
{
  digitalWrite(CHIP_SELECTa, LOW);
  delay(100);
  digitalWrite(CHIP_SELECTa, HIGH);
  temptca = (float) tca.readThermocouple(CELSIUS);  
  delay(100);

  
  if ( temptca >= 30 ) //@ peak temptca, run at full speed
      {
        digitalWrite(EnablePin,HIGH);
        

        digitalWrite(CHIP_SELECTa, LOW);
        delay(100);
        digitalWrite(CHIP_SELECTa, HIGH);
        temptca = (int) tca.readThermocouple(CELSIUS); 
        delay(100);

      }

  else //@low temptca, disable motor driver
      {
        digitalWrite(EnablePin,LOW);
        

        digitalWrite(CHIP_SELECTa, LOW);
        delay(100);
        digitalWrite(CHIP_SELECTa, HIGH);
        temptca = (float) tca.readThermocouple(CELSIUS); 
        delay(100);

      }
}

EDIT also switched the cs from pin 11 to pin 10, still no dice. I’m starting to think its a data type issue.

What MAX31855 library are you using? Is it the one from Adafruit or from somewhere else.

There are several problems with your code but if you let us know what library your using we can point you in the right direction.
Some of the code problems are…
Wrong variable types & casting.
Spurious setup/loop definitions and calls.
Chip select wrong (library call should handle that for you)

I would also suggest you have some hysteresis in the temperature high/low values so the motor wont be pulsing on/off at exactly 30 degrees.

EDIT:
Try using the example sketch from the library code to be sure your reading the temperature okay first, then incorporate it into your motor sketch.

#include <MAX31855.h>   // MAX31855 library       //
//#include <TimeLib.h>             // time and clock library //
#include <SPI.h>                 // Standard SPI librarys  //

const int EnablePin = 3; // enable pin controls current supply to output
const int pwmA  = 5;  // Pin no. 2, name 1A of L293--> arduino PWM pin 5
const int pwmB  = 6;  // Pin no. 7, name 2A of L293--> arduino PWM pin 6

const int CHIP_SELECTa = 10;        // Chip-Select PIN for sensor B      //
const int MISOa        = 12;        // Master-in-Slave Output --> pin 12 //
const int SCKa         = 13;        // SPI Clock common                  //

MAX31855 tca(SCKa, CHIP_SELECTa, MISOa );


double temptca = 0;
//variable temperature temptca is double,

//const unsigned long SECONDS = 1000;        //one second
//const unsigned long HOUR = 3600*SECONDS;   //one hour

//void setup();
//void loop();

void setup() {
  Serial.begin(9600);
  //set motor IC pins
  pinMode(pwmA, OUTPUT);
  pinMode(pwmB, OUTPUT);
  pinMode(EnablePin,OUTPUT);
  digitalWrite(EnablePin,LOW);
  //set direction to counterclockwise
  digitalWrite(pwmA, LOW);
  digitalWrite(pwmA, HIGH);
  //MAX31855
  //pinMode(CHIP_SELECTa,OUTPUT);
  
  //loop();
}

void loop()
{
  //digitalWrite(CHIP_SELECTa, LOW);
  delay(100);
  //digitalWrite(CHIP_SELECTa, HIGH);
  temptca = tca.readThermocouple(CELSIUS); 
  //delay(100);
  
  
  if ( temptca >= 30.0 ) //@ peak temptca, run at full speed
  {
    digitalWrite(EnablePin,HIGH);
    
    //digitalWrite(CHIP_SELECTa, LOW);
    //delay(100);
    //digitalWrite(CHIP_SELECTa, HIGH);
    //temptca = (int) tca.readThermocouple(CELSIUS);
    //delay(100);
    
  }
  
  else //@low temptca, disable motor driver
  {
    digitalWrite(EnablePin,LOW);
    
    
    //digitalWrite(CHIP_SELECTa, LOW);
    //delay(100);
    //digitalWrite(CHIP_SELECTa, HIGH);
    //temptca = (float) tca.readThermocouple(CELSIUS);
    //delay(100);
    
  }
}