Multiple 3-Wire SPI Sensor Interfacing With Arduino - Arduino Stack Exchange
Multiple 3-Wire SPI Sensor Interfacing With Arduino - Arduino Stack Exchange
Arduino
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):
D4 -> cs (slave 1)
D5 -> cs (slave 2)
// www.ladyada.net/learn/sensors/thermocouple
#include "max6675.h"
int thermoCLK = 7;
int thermoDO = 3;
int thermoCS = 4;
int thermoCS2 = 5;
void setup() {
Serial.begin(9600);
Serial.println("MAX6675 test");
delay(500);
void loop() {
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
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.
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>
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) {
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