Skip to content

Instantly share code, notes, and snippets.

@sleemanj
Created May 18, 2016 07:43
Show Gist options
  • Save sleemanj/059fce7f1b8087edfe7d7ef845a5d881 to your computer and use it in GitHub Desktop.
Save sleemanj/059fce7f1b8087edfe7d7ef845a5d881 to your computer and use it in GitHub Desktop.
MAX6675 Thermocouple Reader Example Arduino Code
/** 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;
}
@riyyappan
Copy link

Refer code column number 63 to 65, what manipulation is being done over here?. This information is available at where?

@Tommy-LSA
Copy link

Refer code column number 63 to 65, what manipulation is being done over here?. This information is available at where?

https://www.arduino.cc/reference/en/language/functions/advanced-io/shiftin/

@Vasek-R
Copy link

Vasek-R commented Jan 31, 2019

Works great tested with NodeMCU 8266 just renamed spi pins to D6-D8
Thanks
Vasek

@ADI8421
Copy link

ADI8421 commented Dec 23, 2020

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.

@sleemanj
Copy link
Author

sleemanj commented Dec 23, 2020 via email

@nilu0007
Copy link

Can we measure negative Temperature

@sleemanj
Copy link
Author

sleemanj commented Nov 13, 2021 via email

@Refmech
Copy link

Refmech commented Feb 7, 2022

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.

@sleemanj
Copy link
Author

sleemanj commented Feb 7, 2022

double myTemperature;

...

 myTemperature = readThermocouple();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment