Created
May 18, 2016 07:43
-
-
Save sleemanj/059fce7f1b8087edfe7d7ef845a5d881 to your computer and use it in GitHub Desktop.
MAX6675 Thermocouple Reader Example Arduino Code
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** 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; | |
} |
Correct not using SPI as noted in comment you can use with any pin. Been 5 years since publishing I don't recall details of implementation but as I'm sure I would have tested then and nobody has said otherwise I guess that reading when clock is high (already risen) is ok even if datasheet says read falling edge.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
**Edit with more detail.**
Indeed, if we look at the timing diagram on page 6 of the datasheet, we can see that each bit starting with D15 is correctly set throughout the period of the clock high. The device starts "1 bit ahead" by virtue of D15 (always 0) being already clocked in when you put CS low (well, tDV being 100ns afterwards).
My reading of the diagrams on page 6 is therefore that the code presented above, written 5 years previous, will produce correct results.
CS is set LOW, D15 is ready to be read 100ns later, at least a 1 milliscond delay passes before shiftin sets the clock high, it reads the D15 bit which the timing diagram assures is still available, before setting the clock low, at which time the device will prepare bit D14 to be read, the clock will go high, D14 will be read, and so forth.
You can see in the timing diagram clearly that the state of SO pin begins changing to the next bit on the falling edge (and may take up to 100nS being tCL), so indeed, read the pin right before you set it low, not right after you set it low, especially with the slow performance of the Arduino digitalWrite and digitalRead functions employed by shiftIn.
**In other words**, shiftIn doesn't read on the rising edge, the arduino functions are slow enough that the edge has surely already risen and then some, the arduino in this case could be measured in several microseconds between the digitalWrite to set SCK high and the digitalRead of the data, where the physical rise time itself would be usually measured in nanoseconds.
Can we measure negative Temperature
No, only from 0 degrees C and up. ---- On Sun, 14 Nov 2021 03:29:08 +1300 ***@***.*** wrote ***@***.*** commented on this gist.
Can we measure negative Temperature
—You are receiving this because you authored the thread.Reply to this email directly, view it on GitHub, or unsubscribe.Triage notifications on the go with GitHub Mobile for iOS or Android.
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...
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.