You are on page 1of 3

3/14/22, 3:29 AM Multiple 3-wire SPI sensor interfacing with Arduino - Arduino Stack Exchange

Arduino

Multiple 3-wire SPI sensor interfacing with Arduino


Asked
4 years, 11 months ago Modified
3 months ago Viewed
7k times

I want to connect 4 MAX6675 thermocouple to Arduino.


MAX 6675 description.

0 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.
2
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());

https://arduino.stackexchange.com/questions/37193/multiple-3-wire-spi-sensor-interfacing-with-arduino 1/3
3/14/22, 3:29 AM Multiple 3-wire SPI sensor interfacing with Arduino - Arduino Stack Exchange
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?

sensors spi

Share Improve this question Follow edited Apr 18, 2017 at 6:14 asked Apr 17, 2017 at 14:27
SDsolar Piyush Verma
1,141 1 10 33 3 1 3

1 I'd not use that library. Instead I'd one that uses SPI properly. Then you use just N+2 pins. Or since it's so
simple to use anyway, just use SPI directly.
– Majenko ♦
Apr 17, 2017 at 14:54

The above program will not run as it is as there is a syntax error. You have readCelsius in the beginning and
readCelcius later on, all references should be readCelsius. Best regards, Phil.
– Bonzo
Oct 6, 2018 at 21:49

Why would you not use pin 19 for SCK and pin 18 for MISO to keep the digital pins free? Could you please
comment on what each line does in that code, just trying to learn and understand as I want to use 4
thermocouple and then write to 6 relay outputs.
– Daniel Collins
Oct 10, 2018 at 6:49

1 Answer Active Oldest Votes

First off ditch that library. It's completely pointless and actually makes things harder.

2 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);

https://arduino.stackexchange.com/questions/37193/multiple-3-wire-spi-sensor-interfacing-with-arduino 2/3
3/14/22, 3:29 AM Multiple 3-wire SPI sensor interfacing with Arduino - Arduino Stack Exchange
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);

Share Improve this answer Follow edited Oct 7, 2018 at 8:08 answered Apr 17, 2017 at 15:04
Greenonline Majenko ♦
2,773 7 30 44 100k 5 69 125

Shouldn't the above code also refer to the SCK and MISO pins?
– uzadude
Dec 12, 2021 at 17:27

https://arduino.stackexchange.com/questions/37193/multiple-3-wire-spi-sensor-interfacing-with-arduino 3/3

You might also like