├── MAX31855-1-schematic.png ├── MAX31855-2-schematic.png ├── MAX31855-4-schematic.png ├── MAX31855 ├── keywords.txt ├── MAX31855.h ├── examples │ ├── MAX31855 │ │ └── MAX31855.ino │ ├── MAX31855_Quad │ │ └── MAX31855_Quad.ino │ ├── MAX31855x8_TimerISR │ │ ├── Timer.ino │ │ └── MAX31855x8_TimerISR.ino │ └── MAX31855x8 │ │ └── MAX31855x8.ino └── MAX31855.cpp ├── MAX31855_RPi.txt └── README /MAX31855-1-schematic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/engineertype/MAX31855/HEAD/MAX31855-1-schematic.png -------------------------------------------------------------------------------- /MAX31855-2-schematic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/engineertype/MAX31855/HEAD/MAX31855-2-schematic.png -------------------------------------------------------------------------------- /MAX31855-4-schematic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/engineertype/MAX31855/HEAD/MAX31855-4-schematic.png -------------------------------------------------------------------------------- /MAX31855/keywords.txt: -------------------------------------------------------------------------------- 1 | ####################################### 2 | # Syntax Coloring Map For MAX31855 3 | ####################################### 4 | 5 | ####################################### 6 | # Datatypes (KEYWORD1) 7 | ####################################### 8 | 9 | MAX31855 KEYWORD1 10 | 11 | ####################################### 12 | # Methods and Functions (KEYWORD2) 13 | ####################################### 14 | 15 | readThermocouple KEYWORD2 16 | readJunction KEYWORD2 17 | 18 | 19 | ####################################### 20 | # Constants (LITERAL1) 21 | ####################################### 22 | FAULT_OPEN LITERAL1 23 | FAULT_SHORT_GND LITERAL1 24 | FAULT_SHORT_VCC LITERAL1 25 | NO_MAX31855 LITERAL1 26 | CELSIUS LITERAL1 27 | FAHRENHEIT LITERAL1 28 | -------------------------------------------------------------------------------- /MAX31855/MAX31855.h: -------------------------------------------------------------------------------- 1 | // Written by Peter Easton (www.whizoo.com) 2 | // Released under WTFPL license 3 | // 4 | // Look for the MAX31855 breakout boards on www.whizoo.com. 5 | // 6 | // Change History: 7 | // 13 May 2014 Initial Version 8 | 9 | #ifndef MAX31855_H 10 | #define MAX31855_H 11 | 12 | #include "Arduino.h" 13 | 14 | #define FAULT_OPEN 10000 // No thermocouple 15 | #define FAULT_SHORT_GND 10001 // Thermocouple short to ground 16 | #define FAULT_SHORT_VCC 10002 // Thermocouple short to VCC 17 | #define NO_MAX31855 10003 // MAX31855 not communicating 18 | 19 | enum unit_t 20 | { 21 | CELSIUS, 22 | FAHRENHEIT 23 | }; 24 | 25 | class MAX31855 26 | { 27 | public: 28 | MAX31855(int, int, int); 29 | 30 | double readThermocouple(unit_t unit); 31 | double readJunction(unit_t unit); 32 | 33 | private: 34 | unsigned long readData(); 35 | int _miso, _cs, _clk; 36 | }; 37 | #endif // MAX31855_H -------------------------------------------------------------------------------- /MAX31855_RPi.txt: -------------------------------------------------------------------------------- 1 | Provided by a helpful customer - thanks! 2 | 3 | Here is the code written in python tested and working with the device on raspberry pi. You can adjust the sleep time as you want but you cannot go any lower than .125 ms from my experience. 4 | 5 | import time 6 | 7 | import Adafruit_GPIO.SPI as SPI 8 | import Adafruit_MAX31855.MAX31855 as MAX31855 9 | import RPi.GPIO as GPIO 10 | 11 | # Define a function to convert celsius to fahrenheit. 12 | def c_to_f(c): 13 | return c * 9.0 / 5.0 + 32.0 14 | 15 | # Raspberry Pi software SPI configuration. 16 | CLK = 25 17 | CS = 24 18 | DO = 18 19 | sensor = MAX31855.MAX31855(CLK, CS, DO) 20 | T0 = 17 21 | T1 = 27 22 | T2 = 22 23 | GPIO.setmode(GPIO.BCM) 24 | GPIO.setup(T0, GPIO.OUT) 25 | GPIO.setup(T1, GPIO.OUT) 26 | GPIO.setup(T2, GPIO.OUT) 27 | tc = 0 28 | # Loop printing measurements every second. 29 | print('Press Ctrl-C to quit.') 30 | while True: 31 | GPIO.output(T0, tc & 1<<0) 32 | GPIO.output(T1, tc & 1<<1) 33 | GPIO.output(T2, tc & 1<<2) 34 | time.sleep(0.125) 35 | temp = sensor.readTempC() 36 | internal = sensor.readInternalC() 37 | print('Sensor', tc) 38 | print('{1:0.3F}'.format(temp, c_to_f(temp))) 39 | #print('Thermocouple Temperature: {0:0.3F}*C / {1:0.3F}*F'.format(temp, c_to_f(temp))) 40 | #print(' Internal Temperature: {0:0.3F}*C / {1:0.3F}*F'.format(internal, c_to_f(internal))) 41 | tc = tc+1 42 | if tc == 4: 43 | tc=0 -------------------------------------------------------------------------------- /README: -------------------------------------------------------------------------------- 1 | This is the GitHub source code repository for the Arduino MAX31855 library and example code for Arduino. 2 | 3 | Maxim has released a newer version of this IC called MAX31856. You can find the library here: 4 | https://github.com/engineertype/MAX31856 5 | 6 | This library offers advantages over other MAX31855 libraries: 7 | a. Returns both the junction (IC) and thermocouple temperatures 8 | b. Handles negative temperatures 9 | c. Returns thermocouple error codes, like open, short-to-ground and short-to-vcc 10 | d. Returns an error if the MAX31855 isn’t communicating (wiring issue?) 11 | 12 | In this folder are: 13 | 1. README - this file 14 | 2. MAX31855—1-schematic.png - Hardware schematic for the MAX31855 board 15 | 3. MAX31855—2-schematic.png - Hardware schematic for the dual MAX31855 board 16 | 4. MAX31855—4-schematic.png - Hardware schematic for the quad MAX31855 board 17 | 5. MAX31855 folder - the Arduino library and example code for driving the MAX31855 18 | 6. MAX31855_RPi - Sample code for the Raspberry Pi (Octo boards) 19 | 20 | To install the MAX31855 library, please refer to: 21 | http://arduino.cc/en/Guide/Libraries 22 | 23 | Breakout boards are available on eBay. Please buy them and support my open-source efforts! 24 | 5V Single http://www.ebay.com/itm/301193478531 (sold out) 25 | 5V Dual http://www.ebay.com/itm/301214589468 26 | 5V Quad http://www.ebay.com/itm/301200852126 (sold out) 27 | 5V Octo http://www.ebay.com/itm/251802776832 28 | 3.3V Single http://www.ebay.com/itm/301208636998 (sold out) 29 | 3.3V Dual http://www.ebay.com/itm/301208677221 (sold out) 30 | 3.3V Quad http://www.ebay.com/itm/251704050290 (sold out) 31 | 3.3V Octo http://www.ebay.com/itm/301498690366 32 | 33 | Take a look at the new MAX31856 boards. Much better accuracy and flexibility! 34 | 35 | Looking to build a reflow oven? Check out the build guide: 36 | http://whizoo.com/reflowoven 37 | 38 | Peter Easton 2017 39 | Whizoo.com 40 | -------------------------------------------------------------------------------- /MAX31855/examples/MAX31855/MAX31855.ino: -------------------------------------------------------------------------------- 1 | /* 2 | MAX31855 library example sketch 3 | 4 | The MAX31855 uses SPI to communicate, and requires 3 pins. This example displays the junction 5 | (chip) temperature and the thermocouple temperature. 6 | 7 | Look for the MAX31855 breakout boards on www.whizoo.com. 8 | 9 | readJunction() and readThermocouple() will return the temperature, or one of these errors: 10 | #define FAULT_OPEN 10000 // No thermocouple 11 | #define FAULT_SHORT_GND 10001 // Thermocouple short to ground 12 | #define FAULT_SHORT_VCC 10002 // Thermocouple short to VCC 13 | #define NO_MAX31855 10003 // MAX31855 not communicating 14 | 15 | 16 | Note: If you connect the thermocouple probe the wrong way around, the temperature will go up 17 | instead of down (and vice versa). No problem, just reverse the terminals. 18 | 19 | Released under WTFPL license. 20 | 21 | 13 May 2014 by Peter Easton 22 | 23 | */ 24 | #include 25 | 26 | 27 | // Pin connections. 28 | #define MISO 2 29 | #define SCK 3 30 | #define CS 4 31 | 32 | // Create the temperature object, defining the pins used for communication 33 | MAX31855 temp(MISO, CS, SCK); 34 | 35 | void setup() { 36 | Serial.begin(9600); 37 | } 38 | 39 | 40 | void loop (){ 41 | Serial.print("Junction="); 42 | printTemperature(temp.readJunction(CELSIUS)); 43 | Serial.print("Thermocouple="); 44 | printTemperature(temp.readThermocouple(CELSIUS)); 45 | Serial.println(); 46 | delay(1000); 47 | } 48 | 49 | 50 | // Print the temperature, or the type or fault 51 | void printTemperature(double temperature) { 52 | switch ((int) temperature) { 53 | case FAULT_OPEN: 54 | Serial.print("FAULT_OPEN"); 55 | break; 56 | case FAULT_SHORT_GND: 57 | Serial.print("FAULT_SHORT_GND"); 58 | break; 59 | case FAULT_SHORT_VCC: 60 | Serial.print("FAULT_SHORT_VCC"); 61 | break; 62 | case NO_MAX31855: 63 | Serial.print("NO_MAX31855"); 64 | break; 65 | default: 66 | Serial.print(temperature); 67 | break; 68 | } 69 | Serial.print(" "); 70 | } 71 | 72 | 73 | 74 | 75 | -------------------------------------------------------------------------------- /MAX31855/examples/MAX31855_Quad/MAX31855_Quad.ino: -------------------------------------------------------------------------------- 1 | /* 2 | MAX31855 library example sketch 3 | 4 | The MAX31855 uses SPI to communicate, and requires 3 pins. This example displays the junction 5 | (chip) temperature and the thermocouple temperature. 6 | 7 | Look for the MAX31855 breakout boards on www.whizoo.com. 8 | 9 | readJunction() and readThermocouple() will return the temperature, or one of these errors: 10 | #define FAULT_OPEN 10000 // No thermocouple 11 | #define FAULT_SHORT_GND 10001 // Thermocouple short to ground 12 | #define FAULT_SHORT_VCC 10002 // Thermocouple short to VCC 13 | #define NO_MAX31855 10003 // MAX31855 not communicating 14 | 15 | 16 | Note: If you connect the thermocouple probe the wrong way around, the temperature will go up 17 | instead of down (and vice versa). No problem, just reverse the terminals. 18 | 19 | Released under WTFPL license. 20 | 21 | 13 May 2014 by Peter Easton 22 | 23 | */ 24 | #include 25 | 26 | 27 | // Pin connections to the Quad MAX31855 board 28 | #define MISO 2 29 | #define SCK 3 30 | #define CS0 4 31 | #define CS1 5 32 | #define CS2 6 33 | #define CS3 7 34 | 35 | // Create the temperature object, defining the pins used for communication 36 | MAX31855 *temp[4] = {new MAX31855(MISO, CS0, SCK), new MAX31855(MISO, CS1, SCK), new MAX31855(MISO, CS2, SCK), new MAX31855(MISO, CS3, SCK)}; 37 | 38 | void setup() { 39 | // Display temperatures using the serial port 40 | Serial.begin(9600); 41 | } 42 | 43 | 44 | void loop (){ 45 | for (int i=0; i<4; i++) { 46 | double temperature = (*temp[i]).readJunction(CELSIUS); 47 | if (temperature == NO_MAX31855) 48 | continue; 49 | Serial.print("J"); 50 | Serial.print(i); 51 | Serial.print("="); 52 | printTemperature(temperature); 53 | 54 | temperature = (*temp[i]).readThermocouple(CELSIUS); 55 | if (temperature == NO_MAX31855) 56 | continue; 57 | Serial.print("T"); 58 | Serial.print(i); 59 | Serial.print("="); 60 | printTemperature(temperature); 61 | } 62 | Serial.println(); 63 | delay(1000); 64 | } 65 | 66 | 67 | // Print the temperature, or the type of fault 68 | void printTemperature(double temperature) { 69 | switch ((int) temperature) { 70 | case FAULT_OPEN: 71 | Serial.print("FAULT_OPEN"); 72 | break; 73 | case FAULT_SHORT_GND: 74 | Serial.print("FAULT_SHORT_GND"); 75 | break; 76 | case FAULT_SHORT_VCC: 77 | Serial.print("FAULT_SHORT_VCC"); 78 | break; 79 | case NO_MAX31855: 80 | Serial.print("NO_MAX31855"); 81 | break; 82 | default: 83 | Serial.print(temperature); 84 | break; 85 | } 86 | Serial.print(" "); 87 | } 88 | 89 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /MAX31855/examples/MAX31855x8_TimerISR/Timer.ino: -------------------------------------------------------------------------------- 1 | // The following works for the ATMEGA328, and posibly others. Refer to the microcontroller's datasheet 2 | // Assuming a 16MHz clock, we set the prescaler to 1024. This gives a timer speed of 16,000,000 / 1024 = 15,625. This means 3 | // the timer counts from 0 to 15,625 in one second. We'd like to sample the MAX31855 every 125ms (0.125s) or 8 times per 4 | // second so we set the compare register OCR1A to 15,625 * 0.125 = 1953. 5 | 6 | // Create the temperature object, defining the pins used for communication 7 | MAX31855 temp = MAX31855(MISO, CS, SCK); 8 | 9 | // Store the temperatures as they are read 10 | volatile float temps[8]; 11 | 12 | 13 | void initializeThermocoupleTimer(void) { 14 | cli(); // Disable global interrupts 15 | ADMUX = 0; // Initialize the MUX and enable ADC and set frequency 16 | ADCSRA = (1<=0 && thermocouple <= 7) 62 | temperature = temps[thermocouple]; 63 | else 64 | temperature = temp.readJunction(CELSIUS); 65 | 66 | // Reenable interrupts 67 | interrupts(); 68 | 69 | // Return the temperature 70 | return temperature; 71 | } 72 | 73 | 74 | -------------------------------------------------------------------------------- /MAX31855/examples/MAX31855x8_TimerISR/MAX31855x8_TimerISR.ino: -------------------------------------------------------------------------------- 1 | /* 2 | MAX31855 library example sketch 3 | 4 | This sample code is designed to be used on the MAX31855x8 breakout board. 5 | Look for the MAX31855 breakout boards on www.whizoo.com. 6 | 7 | This sample code corresponds to the MAX31855x8 board. The board has a single MAX31855 IC on it, and uses a multiplexer 8 | to select the correct thermoucouple. The MAX31855 takes around 100ms to take an accurate temperature reading, so 9 | the best sample rate one can expect is to sample all 8 channels once-per-second. If you are only sampling 2 channels 10 | then you can do it 4 times-per-second, and so on. 11 | 12 | There are 2 versions of the MAX31855x8 board: 3.3V and 5V. There is a solder jumper on the board 13 | that can be changed to go from one voltage to the other. 14 | 15 | This sample code uses an Interrupt Service Routine (ISR) and a timer to continuously take readings in background. This 16 | enables the main loop to obtain readings without waiting. 17 | 18 | In the MAX31855 library, there are 2 functions: 19 | 1. float readJunction([CELCUIS|FAHRENHEIT]) 20 | Returns the internal temperature of the MAX31855 IC 21 | 22 | 2. float readThermocouple([CELCUIS|FAHRENHEIT]) 23 | Returns the temperature of the probe connected to the MAX31855 24 | 25 | readJunction() and readThermocouple() will return the temperature, or one of these errors: 26 | #define FAULT_OPEN 10000 // No thermocouple 27 | #define FAULT_SHORT_GND 10001 // Thermocouple short to ground 28 | #define FAULT_SHORT_VCC 10002 // Thermocouple short to VCC 29 | #define NO_MAX31855 10003 // MAX31855 not communicating 30 | 31 | Note: If you connect the thermocouple probe the wrong way around, the temperature will go up 32 | instead of down (and vice versa). No problem, just reverse the terminals. 33 | 34 | Released under WTFPL license. 35 | 36 | 27 October 2014 by Peter Easton 37 | 38 | */ 39 | #include 40 | 41 | // Pin connections to the MAX31855x8 board 42 | // The power requirement for the board is less than 2mA. Most microcontrollers can source or sink a lot more 43 | // than that one each I/O pin. For example, the ATmega328 supports up to 20mA. For convenience, the board 44 | // is placed directly on top of a row of I/O pins on the microcontroller. Power is supplied to the board by 45 | // holding the GND pin low and the VIN pin high 46 | #define GND 3 47 | #define T0 4 48 | #define T1 5 49 | #define T2 6 50 | #define VIN 7 51 | #define MISO 8 52 | #define CS 9 53 | #define SCK 10 54 | 55 | // Get the junction (IC) temperature. Can be anything as long as it isn't 0-7 56 | #define JUNCTION 10 57 | 58 | 59 | void setup() { 60 | // Display temperatures using the serial port 61 | Serial.begin(9600); 62 | 63 | // Initialize pins 64 | pinMode(GND, OUTPUT); 65 | pinMode(T0, OUTPUT); 66 | pinMode(T1, OUTPUT); 67 | pinMode(T2, OUTPUT); 68 | pinMode(VIN, OUTPUT); 69 | 70 | // Power up the board 71 | digitalWrite(GND, LOW); 72 | digitalWrite(VIN, HIGH); 73 | delay(200); 74 | 75 | // Start taking the temperature of all thermocouples 76 | initializeThermocoupleTimer(); 77 | 78 | // Wait a second to allow every thermocouple to be read 79 | delay(1000); 80 | } 81 | 82 | 83 | void loop () { 84 | // Display the junction temperature 85 | float temperature = getTemperature(JUNCTION); 86 | Serial.print("J="); 87 | printTemperature(temperature); 88 | 89 | 90 | // Display the temperatures of the 8 thermocouples 91 | for (int thermocouple=0; thermocouple<8; thermocouple++) { 92 | temperature = getTemperature(thermocouple); 93 | 94 | if (temperature == FAULT_OPEN) 95 | continue; 96 | Serial.print(" T"); 97 | Serial.print(thermocouple); 98 | Serial.print("="); 99 | printTemperature(temperature); 100 | } 101 | Serial.println(); 102 | 103 | // Wait a second before taking the next set of readings 104 | delay(1000); 105 | } 106 | 107 | 108 | // Print the temperature, or the type of fault 109 | void printTemperature(double temperature) { 110 | switch ((int) temperature) { 111 | case FAULT_OPEN: 112 | Serial.print("FAULT_OPEN"); 113 | break; 114 | case FAULT_SHORT_GND: 115 | Serial.print("FAULT_SHORT_GND"); 116 | break; 117 | case FAULT_SHORT_VCC: 118 | Serial.print("FAULT_SHORT_VCC"); 119 | break; 120 | case NO_MAX31855: 121 | Serial.print("NO_MAX31855"); 122 | break; 123 | default: 124 | Serial.print(temperature); 125 | break; 126 | } 127 | Serial.print(" "); 128 | } 129 | 130 | 131 | 132 | 133 | -------------------------------------------------------------------------------- /MAX31855/examples/MAX31855x8/MAX31855x8.ino: -------------------------------------------------------------------------------- 1 | /* 2 | MAX31855 library example sketch 3 | 4 | This sample code is designed to be used on the MAX31855x8 breakout board. 5 | Look for the MAX31855 breakout boards on www.whizoo.com. 6 | 7 | This sample code corresponds to the MAX31855x8 board. The board has a single MAX31855 IC on it, and uses a multiplexer 8 | to select the correct thermoucouple. The MAX31855 takes around 100ms to take an accurate temperature reading, so 9 | the best sample rate one can expect is to sample all 8 channels once-per-second. If you are only sampling 2 channels 10 | then you can do it 4 times-per-second, and so on. 11 | 12 | There are 2 versions of the MAX31855x8 board: 3.3V and 5V. There is a solder jumper on the board 13 | that can be changed to go from one voltage to the other. 14 | 15 | This sample code shows how to take a temperature reading: 16 | 1. Set the multiplexer to the correct thermocouple channel 17 | 2. Wait 125ms (0.125 seconds) for the MAX31855 to take an accurate reading 18 | 3. Get the temperature from the MAX31855 19 | 20 | In the MAX31855 library, there are 2 functions: 21 | 1. float readJunction([CELCUIS|FAHRENHEIT]) 22 | Returns the internal temperature of the MAX31855 IC 23 | 24 | 2. float readThermocouple([CELCUIS|FAHRENHEIT]) 25 | Returns the temperature of the probe connected to the MAX31855 26 | 27 | readJunction() and readThermocouple() will return the temperature, or one of these errors: 28 | #define FAULT_OPEN 10000 // No thermocouple 29 | #define FAULT_SHORT_GND 10001 // Thermocouple short to ground 30 | #define FAULT_SHORT_VCC 10002 // Thermocouple short to VCC 31 | #define NO_MAX31855 10003 // MAX31855 not communicating 32 | 33 | Note: If you connect the thermocouple probe the wrong way around, the temperature will go up 34 | instead of down (and vice versa). No problem, just reverse the terminals. 35 | 36 | Released under WTFPL license. 37 | 38 | 27 October 2014 by Peter Easton 39 | 40 | */ 41 | #include 42 | 43 | // Pin connections to the MAX31855x8 board 44 | // The power requirement for the board is less than 2mA. Most microcontrollers can source or sink a lot more 45 | // than that one each I/O pin. For example, the ATmega328 supports up to 20mA. For convenience, the board 46 | // is placed directly on top of a row of I/O pins on the microcontroller. Power is supplied to the board by 47 | // holding the GND pin low and the VIN pin high 48 | #define GND 3 49 | #define T0 4 50 | #define T1 5 51 | #define T2 6 52 | #define VIN 7 53 | #define MISO 8 54 | #define CS 9 55 | #define SCK 10 56 | 57 | // Create the temperature object, defining the pins used for communication 58 | MAX31855 temp = MAX31855(MISO, CS, SCK); 59 | 60 | void setup() { 61 | // Display temperatures using the serial port 62 | Serial.begin(9600); 63 | 64 | // Initialize pins 65 | pinMode(GND, OUTPUT); 66 | pinMode(T0, OUTPUT); 67 | pinMode(T1, OUTPUT); 68 | pinMode(T2, OUTPUT); 69 | pinMode(VIN, OUTPUT); 70 | 71 | // Power up the board 72 | digitalWrite(GND, LOW); 73 | digitalWrite(VIN, HIGH); 74 | delay(200); 75 | } 76 | 77 | 78 | void loop () { 79 | // Display the junction temperature 80 | float temperature = temp.readJunction(CELSIUS); 81 | Serial.print("J="); 82 | printTemperature(temperature); 83 | 84 | // Display the temperatures of the 8 thermocouples 85 | for (int therm=0; therm<8; therm++) { 86 | // Select the thermocouple 87 | digitalWrite(T0, therm & 1? HIGH: LOW); 88 | digitalWrite(T1, therm & 2? HIGH: LOW); 89 | digitalWrite(T2, therm & 4? HIGH: LOW); 90 | // The MAX31855 takes 100ms to sample the thermocouple. 91 | // Wait a bit longer to be safe. We'll wait 0.125 seconds 92 | delay(125); 93 | 94 | temperature = temp.readThermocouple(CELSIUS); 95 | if (temperature == FAULT_OPEN) 96 | continue; 97 | Serial.print(" T"); 98 | Serial.print(therm); 99 | Serial.print("="); 100 | printTemperature(temperature); 101 | } 102 | 103 | Serial.println(); 104 | } 105 | 106 | 107 | // Print the temperature, or the type of fault 108 | void printTemperature(double temperature) { 109 | switch ((int) temperature) { 110 | case FAULT_OPEN: 111 | Serial.print("FAULT_OPEN"); 112 | break; 113 | case FAULT_SHORT_GND: 114 | Serial.print("FAULT_SHORT_GND"); 115 | break; 116 | case FAULT_SHORT_VCC: 117 | Serial.print("FAULT_SHORT_VCC"); 118 | break; 119 | case NO_MAX31855: 120 | Serial.print("NO_MAX31855"); 121 | break; 122 | default: 123 | Serial.print(temperature); 124 | break; 125 | } 126 | Serial.print(" "); 127 | } 128 | 129 | 130 | 131 | 132 | -------------------------------------------------------------------------------- /MAX31855/MAX31855.cpp: -------------------------------------------------------------------------------- 1 | // This library is derived from RocketScream.com's I2C MAX31855 library. 2 | // Please support them from buying products from their web site. 3 | // https://github.com/rocketscream/MAX31855/blob/master/MAX31855.cpp 4 | // 5 | // This is a library for the MAX31855 thermocouple IC 6 | // 7 | // Look for the MAX31855 breakout boards on www.whizoo.com. 8 | // 9 | // Written by Peter Easton (www.whizoo.com) 10 | // Released under WTFPL license 11 | // 12 | // Change History: 13 | // 13 May 2014 Initial Version 14 | 15 | #include "MAX31855.h" 16 | 17 | 18 | MAX31855::MAX31855(int miso, int cs, int clk) 19 | { 20 | _miso = miso; 21 | _cs = cs; 22 | _clk = clk; 23 | 24 | // MAX31855 data output pin 25 | // Use internal pullup to be able to detect no connection to the MAX31855 26 | pinMode(_miso, INPUT_PULLUP); 27 | // MAX31855 chip select input pin 28 | pinMode(_cs, OUTPUT); 29 | // MAX31855 clock input pin 30 | pinMode(_clk, OUTPUT); 31 | 32 | // Default output pins state 33 | digitalWrite(_cs, HIGH); 34 | digitalWrite(_clk, LOW); 35 | } 36 | 37 | /******************************************************************************* 38 | * Name: readThermocouple 39 | * Description: Read the thermocouple temperature either in Degree Celsius or 40 | * Fahrenheit. Internally, the conversion takes place in the 41 | * background within 100 ms. Values are updated only when the CS 42 | * line is high. 43 | * 44 | * Argument Description 45 | * ========= =========== 46 | * 1. unit Unit of temperature required: CELSIUS or FAHRENHEIT 47 | * 48 | * Return Description 49 | * ========= =========== 50 | * temperature Temperature of the thermocouple either in Degree Celsius or 51 | * Fahrenheit. If fault is detected, FAULT_OPEN, FAULT_SHORT_GND or 52 | * FAULT_SHORT_VCC will be returned. These fault values are outside 53 | * of the temperature range the MAX31855 is capable of. 54 | *******************************************************************************/ 55 | double MAX31855::readThermocouple(unit_t unit) 56 | { 57 | unsigned long data; 58 | double temperature; 59 | 60 | // Initialize temperature 61 | temperature = 0; 62 | 63 | // Shift in 32-bit of data from MAX31855 64 | data = readData(); 65 | 66 | // Is this a MAX31855? Bits 3 and 17 must be zero 67 | if (data & 0x20008) 68 | return NO_MAX31855; 69 | 70 | // If fault is detected 71 | if (data & 0x00010000) 72 | { 73 | // Check for fault type (3 LSB) 74 | switch (data & 0x00000007) 75 | { 76 | // Open circuit 77 | case 0x01: 78 | temperature = FAULT_OPEN; 79 | break; 80 | 81 | // Thermocouple short to GND 82 | case 0x02: 83 | temperature = FAULT_SHORT_GND; 84 | break; 85 | 86 | // Thermocouple short to VCC 87 | case 0x04: 88 | temperature = FAULT_SHORT_VCC; 89 | break; 90 | } 91 | } 92 | // No fault detected 93 | else 94 | { 95 | // Retrieve thermocouple temperature data and strip redundant data 96 | data = data >> 18; 97 | // Bit-14 is the sign 98 | temperature = (data & 0x00001FFF); 99 | 100 | // Check for negative temperature 101 | if (data & 0x00002000) 102 | { 103 | // 2's complement operation 104 | // Invert 105 | data = ~data; 106 | // Ensure operation involves lower 13-bit only 107 | temperature = data & 0x00001FFF; 108 | // Add 1 to obtain the positive number 109 | temperature += 1; 110 | // Make temperature negative 111 | temperature *= -1; 112 | } 113 | 114 | // Convert to Degree Celsius 115 | temperature *= 0.25; 116 | 117 | // If temperature unit in Fahrenheit is desired 118 | if (unit == FAHRENHEIT) 119 | { 120 | // Convert Degree Celsius to Fahrenheit 121 | temperature = (temperature * 9.0/5.0)+ 32; 122 | } 123 | } 124 | return (temperature); 125 | } 126 | 127 | /******************************************************************************* 128 | * Name: readJunction 129 | * Description: Read the thermocouple temperature either in Degree Celsius or 130 | * Fahrenheit. Internally, the conversion takes place in the 131 | * background within 100 ms. Values are updated only when the CS 132 | * line is high. 133 | * 134 | * Argument Description 135 | * ========= =========== 136 | * 1. unit Unit of temperature required: CELSIUS or FAHRENHEIT 137 | * 138 | * Return Description 139 | * ========= =========== 140 | * temperature Temperature of the cold junction either in Degree Celsius or 141 | * Fahrenheit. 142 | * 143 | *******************************************************************************/ 144 | double MAX31855::readJunction(unit_t unit) 145 | { 146 | double temperature; 147 | unsigned long data; 148 | 149 | // Shift in 32-bit of data from MAX31855 150 | data = readData(); 151 | 152 | // Is this a MAX31855? Bits 3 and 17 must be zero 153 | if (data & 0x20008) 154 | return NO_MAX31855; 155 | 156 | // Strip fault data bits & reserved bit 157 | data = data >> 4; 158 | // Bit-12 is the sign 159 | temperature = (data & 0x000007FF); 160 | 161 | // Check for negative temperature 162 | if (data & 0x00000800) 163 | { 164 | // 2's complement operation 165 | // Invert 166 | data = ~data; 167 | // Ensure operation involves lower 11-bit only 168 | temperature = data & 0x000007FF; 169 | // Add 1 to obtain the positive number 170 | temperature += 1; 171 | // Make temperature negative 172 | temperature *= -1; 173 | } 174 | 175 | // Convert to Degree Celsius 176 | temperature *= 0.0625; 177 | 178 | // If temperature unit in Fahrenheit is desired 179 | if (unit == FAHRENHEIT) 180 | { 181 | // Convert Degree Celsius to Fahrenheit 182 | temperature = (temperature * 9.0/5.0)+ 32; 183 | } 184 | 185 | // Return the temperature 186 | return (temperature); 187 | } 188 | 189 | /******************************************************************************* 190 | * Name: readData 191 | * Description: Shift in 32-bit of data from MAX31855 chip. Minimum clock pulse 192 | * width is 100 ns. No delay is required in this case. 193 | * 194 | * Argument Description 195 | * ========= =========== 196 | * 1. NIL 197 | * 198 | * Return Description 199 | * ========= =========== 200 | * data 32-bit of data acquired from the MAX31855 chip. 201 | * 202 | *******************************************************************************/ 203 | unsigned long MAX31855::readData() 204 | { 205 | int bitCount; 206 | unsigned long data; 207 | 208 | // Clear data 209 | data = 0; 210 | 211 | // Select the MAX31855 chip 212 | digitalWrite(_cs, LOW); 213 | 214 | // Shift in 32-bit of data 215 | for (bitCount = 31; bitCount >= 0; bitCount--) 216 | { 217 | digitalWrite(_clk, HIGH); 218 | 219 | // If data bit is high 220 | if (digitalRead(_miso)) 221 | { 222 | // Need to type cast data type to unsigned long, else compiler will 223 | // truncate to 16-bit 224 | data |= ((unsigned long)1 << bitCount); 225 | } 226 | 227 | digitalWrite(_clk, LOW); 228 | } 229 | 230 | // Deselect MAX31855 chip 231 | digitalWrite(_cs, HIGH); 232 | 233 | return(data); 234 | } --------------------------------------------------------------------------------