-
-
Save sleemanj/059fce7f1b8087edfe7d7ef845a5d881 to your computer and use it in GitHub Desktop.
/** MAX6675 Thermocouple Reader Example Code | |
* | |
* This is so simple a library would be silly! | |
* | |
* 6675 Module ==> Arduino | |
* CS ==> D10 | |
* SO ==> D12 | |
* SCK ==> D13 | |
* Vcc ==> Vcc (5v OK) | |
* Gnd ==> Gnd | |
* | |
* You can change the pin assignments below, any pins you want are fine. | |
* | |
* Upload coade and open your Serial terminal at 9600 to see the temperature | |
* printed every 1.5 seconds. That's all! | |
* | |
*/ | |
#include <SPI.h> | |
#define MAX6675_CS 10 | |
#define MAX6675_SO 12 | |
#define MAX6675_SCK 13 | |
void setup() { | |
Serial.begin(9600); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
Serial.print(readThermocouple()); | |
Serial.println('c'); | |
// Just for completeness | |
if(readThermocouple() > 33) | |
{ | |
Serial.println("Wow it's hot today!"); | |
} | |
else if(readThermocouple() < 10) | |
{ | |
Serial.println("I hope you have your warm clothes on!"); | |
} | |
delay(1500); | |
} | |
double readThermocouple() { | |
uint16_t v; | |
pinMode(MAX6675_CS, OUTPUT); | |
pinMode(MAX6675_SO, INPUT); | |
pinMode(MAX6675_SCK, OUTPUT); | |
digitalWrite(MAX6675_CS, LOW); | |
delay(1); | |
// Read in 16 bits, | |
// 15 = 0 always | |
// 14..2 = 0.25 degree counts MSB First | |
// 2 = 1 if thermocouple is open circuit | |
// 1..0 = uninteresting status | |
v = shiftIn(MAX6675_SO, MAX6675_SCK, MSBFIRST); | |
v <<= 8; | |
v |= shiftIn(MAX6675_SO, MAX6675_SCK, MSBFIRST); | |
digitalWrite(MAX6675_CS, HIGH); | |
if (v & 0x4) | |
{ | |
// Bit 2 indicates if the thermocouple is disconnected | |
return NAN; | |
} | |
// The lower three bits (0,1,2) are discarded status bits | |
v >>= 3; | |
// The remaining bits are the number of 0.25 degree (C) counts | |
return v*0.25; | |
} |
Works great tested with NodeMCU 8266 just renamed spi pins to D6-D8
Thanks
Vasek
So you're not actually using the SPI library at all ? shiftIn() is a software implementation. It's not part of the SPI library. shiftIn() reads the data on the rising edge of the clock, but MAX6675 requires a read on the falling edge of the clock. Nice try.
Can we measure negative Temperature
This works great! Uploaded to Arduino Nano 33 Iot.
I am trying to link the temperature to a widget in the Arduino cloud but I can't figure out the proper statement. I generated Variable CloudTemperatureSensor temperature and tried temperature = readTemperature; but it was not successful.
double myTemperature;
...
myTemperature = readThermocouple();
Works very well! Got a max31855 that's showing half the real value so v*0.5 it will be...
https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftin/