user32872: I want to connect 4 MAX6675 thermocouple to Arduino.
MAX 6675 description.  
It comes with its own Arduino library. library link.  
I tested the example code for one sensor and it works perfectly. Now I want to connect all the four sensors to Arduino using minimum GPIO pins possible.
I connected - (for 2 sensors only for initial testing):
D7 -> clk (slave 1) ->clk (slave 2)
D3 -> so (slave 1) ->so (slave 2)
D4 -> cs (slave 1)
D5 -> cs (slave 2)

My code (I modified the original example code a bit):
// this example is public domain. enjoy!
// www.ladyada.net/learn/sensors/thermocouple

#include "max6675.h"

int thermoCLK = 7;
int thermoDO = 3;
int thermoCS = 4;
int thermoCS2 = 5;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
MAX6675 thermocouple2(thermoCLK, thermoCS2, thermoDO);

void setup() {
  Serial.begin(9600);
  Serial.println("MAX6675 test");
  // wait for MAX chip to stabilize
  delay(500);
}

void loop() {
  // basic readout test, just print the current temp
  Serial.print("C = ");
  Serial.println(thermocouple.readCelsius());
  Serial.print("F = ");
  Serial.println(thermocouple.readFahrenheit());
  Serial.print("C 2= ");
  Serial.println(thermocouple2.readCelsius());
  Serial.print("F 2= ");
  Serial.println(thermocouple2.readFahrenheit());
  delay(1000);
}

I am able to read only one sensor information correctly. The other output is nan. Am I wiring the circuit wrong or is my code incorrect or both?

user4143: First off ditch that library. It's completely pointless and actually makes things harder.
Secondly connect your sensors to the SPI pins, selecting a new CS pin for each sensor.

Arduino SCK (pin 13) to all the SCK pins.
Arduino MISO (pin 12) to all the SO pins.
Individual GPIO pins to individual CS pins (use pin 10 for one of them).

Then just use a simple function to read the data using the SPI library (adapted from the library you link to). For instance with CS on 10 an 9:
#include <SPI.h>

double readCelsius(uint8_t cs) {
    uint16_t v;

    digitalWrite(cs, LOW);
    v = SPI.transfer(0x00);
    v <<= 8;
    v |= SPI.transfer(0x00);
    digitalWrite(cs, HIGH);

    if (v & 0x4) {
        // uh oh, no thermocouple attached!
        return NAN; 
    }

    v >>= 3;

    return v*0.25;
}

void setup() {
    SPI.begin();
    pinMode(10, OUTPUT);
    pinMode(9, OUTPUT);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH);

    Serial.begin(115200);
}

void loop() {
    Serial.print(readCelsius(10));
    Serial.print(" ");
    Serial.println(readCelsius(9));
    delay(1000);
}


32872: <p>I want to connect 4 MAX6675 thermocouple to Arduino.
<a href="https://www.maximintegrated.com/en/products/analog/sensors-and-sensor-interface/MAX6675.html" rel="nofollow noreferrer">MAX 6675 description</a>.  </p>

<p>It comes with its own Arduino library. <a href="https://github.com/adafruit/MAX6675-library" rel="nofollow noreferrer">library link</a>.  </p>

<p>I tested the example code for one sensor and it works perfectly. Now I want to connect all the four sensors to Arduino using minimum GPIO pins possible.</p>

<p>I connected - (for 2 sensors only for initial testing):</p>

<pre><code>D7 -&gt; clk (slave 1) -&gt;clk (slave 2)
D3 -&gt; so (slave 1) -&gt;so (slave 2)
D4 -&gt; cs (slave 1)
D5 -&gt; cs (slave 2)
</code></pre>

<p>My code (I modified the original example code a bit):</p>

<pre><code>// this example is public domain. enjoy!
// www.ladyada.net/learn/sensors/thermocouple

#include "max6675.h"

int thermoCLK = 7;
int thermoDO = 3;
int thermoCS = 4;
int thermoCS2 = 5;
MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);
MAX6675 thermocouple2(thermoCLK, thermoCS2, thermoDO);

void setup() {
  Serial.begin(9600);
  Serial.println("MAX6675 test");
  // wait for MAX chip to stabilize
  delay(500);
}

void loop() {
  // basic readout test, just print the current temp
  Serial.print("C = ");
  Serial.println(thermocouple.readCelsius());
  Serial.print("F = ");
  Serial.println(thermocouple.readFahrenheit());
  Serial.print("C 2= ");
  Serial.println(thermocouple2.readCelsius());
  Serial.print("F 2= ");
  Serial.println(thermocouple2.readFahrenheit());
  delay(1000);
}
</code></pre>

<p>I am able to read only one sensor information correctly. The other output is <em>nan</em>. Am I wiring the circuit wrong or is my code incorrect or both?</p>
 4143: <p>First off ditch that library. It's completely pointless and actually makes things harder.</p>

<p>Secondly connect your sensors to the SPI pins, selecting a new CS pin for each sensor.</p>

<ul>
<li>Arduino SCK (pin 13) to all the SCK pins.</li>
<li>Arduino MISO (pin 12) to all the SO pins.</li>
<li>Individual GPIO pins to individual CS pins (use pin 10 for one of them).</li>
</ul>

<p>Then just use a simple function to read the data using the SPI library (adapted from the library you link to). For instance with CS on 10 an 9:</p>

<pre><code>#include &lt;SPI.h&gt;

double readCelsius(uint8_t cs) {
    uint16_t v;

    digitalWrite(cs, LOW);
    v = SPI.transfer(0x00);
    v &lt;&lt;= 8;
    v |= SPI.transfer(0x00);
    digitalWrite(cs, HIGH);

    if (v &amp; 0x4) {
        // uh oh, no thermocouple attached!
        return NAN; 
    }

    v &gt;&gt;= 3;

    return v*0.25;
}

void setup() {
    SPI.begin();
    pinMode(10, OUTPUT);
    pinMode(9, OUTPUT);
    digitalWrite(10, HIGH);
    digitalWrite(9, HIGH);

    Serial.begin(115200);
}

void loop() {
    Serial.print(readCelsius(10));
    Serial.print(" ");
    Serial.println(readCelsius(9));
    delay(1000);
}
</code></pre>
