Skip to content

Instantly share code, notes, and snippets.

@geekman
Created May 12, 2016 15:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geekman/bb0d9339fbdebf1b0d4d3379c10a3b68 to your computer and use it in GitHub Desktop.
Save geekman/bb0d9339fbdebf1b0d4d3379c10a3b68 to your computer and use it in GitHub Desktop.
MAX6675 thermocouple converter Arduino code sample
//
// MAX6675 thermocouple converter Arduino code sample
//
// 2016.05.12 darell tan
//
#include <SPI.h>
static const int CS_PIN = 13;
void setup() {
Serial.begin(115200);
SPI.begin();
pinMode(CS_PIN, OUTPUT);
digitalWrite(CS_PIN, HIGH);
}
void loop() {
SPI.beginTransaction(SPISettings(14000000, MSBFIRST, SPI_MODE0));
digitalWrite(CS_PIN, LOW);
// read 16 bits, MSB first
uint16_t data = 0;
data |= SPI.transfer(0) << 8;
data |= SPI.transfer(0) << 0;
SPI.endTransaction();
digitalWrite(CS_PIN, HIGH);
// interpret value
if (data & 0x8000) {
Serial.println("sign not zero!");
} else if (data & 0b100) {
Serial.println("not conn");
} else {
uint16_t temp = (data & 0x7FF8) >> 3;
#if DEBUG
Serial.print("0x");
Serial.print(data >> 8, HEX);
Serial.print(data & 0xFF, HEX);
Serial.print(" -- ");
#endif
// ADC is 12 bits (0-4095)
// temp is 0deg to 1023.75deg
// therefore each increment is 0.25 deg, or 1/4 deg
Serial.print(temp / 4);
Serial.print('.');
Serial.print(25 * (temp % 4));
Serial.println();
}
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment