Skip to content

Commit

Permalink
Merge pull request #11 from RobTillaart/develop
Browse files Browse the repository at this point in the history
fix #9, refactor + fix examples
  • Loading branch information
RobTillaart authored Aug 30, 2020
2 parents 2e6c80e + 696a369 commit b67bc07
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 46 deletions.
28 changes: 17 additions & 11 deletions MAX31855.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
//
// FILE: MAX31855.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.2.1
// VERSION: 0.2.2
// PURPOSE: Arduino library for MAX31855 chip for K type thermocouple
// DATE: 2014-01-01
// URL: https://github.com/RobTillaart/MAX31855_RT
//
// HISTORY:
//
//
// 0.2.2 2020-08-30 fix#9 + fix failing examples + minor refactor
// 0.2.1 2020-08-26 read rawData and STATUS_NO_COMMUNICATION recognition (thanks to FabioBrondo)
// 0.2.0 2020-06-20 #pragma once; major refactor; removed pre 1.0 support; fix offset
// 0.1.10 2019-07-31 add 3 inline functions to test errors + demo sketch
Expand Down Expand Up @@ -62,7 +63,7 @@ uint8_t MAX31855::read()
// 18 - 30 TEMPERATURE (RAW)
// 31 SIGN
uint32_t value = _read();

if (value == 0xFFFFFFFF) // needs a pull up on miso pin to work properly!
{
// bit 3 and bit 17 should always be 0 - P10 datasheet
Expand All @@ -74,33 +75,38 @@ uint8_t MAX31855::read()

// process status bit 0-2
_status = value & 0x0007;
// if (_status) return _status;
if (_status != STATUS_OK)
{
return _status;
}

value >>= 3;

// reserved bit 3, allways 0
// reserved bit 3, always 0
value >>= 1;

// process internal bit 4-15
_internal = (value & 0x07FF) * 0.0625;
if (value & 0x0800)
// negative flag set ?
if (value & 0x0800)
{
_internal = -128 + _internal; // fix neg temp
_internal = -128 + _internal;
}
value >>= 12;

// Fault bit ignored as we have the 3 status bits
// _fault = value & 0x01;
value >>= 1;

// reserved bit 17, allways 0
// reserved bit 17, always 0
value >>= 1;

// process temperature bit 18-30 + sign bit = 31
_temperature = (value & 0x1FFF) * 0.25;
if (value & 0x2000) // negative flag
// negative flag set ?
if (value & 0x2000)
{
_temperature = -2048 + _temperature; // calculate neg temp
_temperature = -2048 + _temperature;
}
return _status;
}
Expand Down Expand Up @@ -140,7 +146,7 @@ float MAX31855::getTemperature()
float Vout = K_TC * (_temperature - _internal); // PAGE 8 datasheet

// 2: from Voltage to corrected temperature using the Seebeck Coefficient
float _temp = Vout / _SC + _internal;
float _temp = Vout / _SC + _internal;
return _temp;
}

Expand Down
20 changes: 15 additions & 5 deletions MAX31855.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@
//
// FILE: MAX31855.h
// AUTHOR: Rob Tillaart
// VERSION: 0.2.1
// VERSION: 0.2.2
// PURPOSE: Arduino library for MAX31855 chip for K type thermocouple
// DATE: 2014-01-01
// URL: https://github.com/RobTillaart/MAX31855_RT
// http://forum.arduino.cc/index.php?topic=208061
//
// Bbreakout board
//
// +---------+
// Vin | o |
// 3Vo | o |
// GND | o O | Thermocouple
// D0 | o O | Thermocouple
// CS | o |
// CLK | o |
// +---------+

#include "Arduino.h"

#define MAX31855_VERSION "0.2.1"
#define MAX31855_VERSION "0.2.2"


// STATE constants returnd by read()
Expand All @@ -29,7 +39,7 @@
// See http://www.analog.com/library/analogDialogue/archives/44-10/thermocouple.html
//
// As the MAX31855 is designed for K type sensors, one can calculate
// the factor needed to convert other sensors measurements.
// the factor needed to convert other sensors measurements.
// NOTE: this is only a linear approximation.
//
// Seebeck Coefficients (sensitivity) from the MAX31855 datasheet page 8
Expand All @@ -55,7 +65,7 @@ class MAX31855
// returns state - bitfield: 0 = STATUS_OK
uint8_t read();

float getInternal(void) const { return _internal; }
float getInternal(void) const { return _internal; }
float getTemperature(void);

uint8_t getStatus(void) const { return _status; };
Expand All @@ -71,7 +81,7 @@ class MAX31855
float getOffset() const { return _offset; };

// set the above E_TC (etc) Seebrecht Coefficients
// one can also set your own optimized values.
// one can also set your own optimized values.
void setSeebeckCoefficient(const float SC) { _SC = SC; };
float getSeebeckCoefficient() const { return _SC; };

Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,20 @@ For every type of TC there exist an MAX31855 variant, this library is primary
developed for the K-type sensor. However it has experimental support for all
other types of TC's. See details below.

Library tested with breakout board

```
+---------+
Vin | o |
3Vo | o |
GND | o O | Thermocouple
D0 | o O | Thermocouple
CS | o |
CLK | o |
+---------+
```

## Interface

To make a temperature reading call **tc.read()**.
Expand Down
11 changes: 7 additions & 4 deletions examples/Demo_getRawData/Demo_getRawData.ino
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
#include <SPI.h>
#include <MAX31855.h>

#define MAXDO 12 // Defining the MISO pin
#define MAXCS 10 // Defining the CS pin
#define MAXCLK 13 // Defining the SCK pin
#define MAXDO 7 // Defining the MISO pin
#define MAXCS 6 // Defining the CS pin
#define MAXCLK 5 // Defining the SCK pin


MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);
Expand All @@ -21,12 +21,13 @@ void setup ()
{
Serial.begin(115200);
delay(250);
thermocouple.begin();
}


void loop ()
{
uint8_t status = thermocouple.read();
int status = thermocouple.read();
if (status == STATUS_NO_COMMUNICATION)
{
Serial.println("NO COMMUNICATION");
Expand All @@ -41,3 +42,5 @@ void loop ()

delay(100);
}

// -- END OF FILE --
5 changes: 3 additions & 2 deletions examples/max31855_array/max31855_array.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: max31855_array.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.1.1
// PURPOSE: demo of array of thermocouples
// DATE: 2020-08-26
// URL: https://github.com/RobTillaart/MAX31855_RT
Expand All @@ -11,6 +11,7 @@

#include "MAX31855.h"

// note: pins are slightly different than other examples!
const int dataPin = 7;
const int clockPin = 6;

Expand Down Expand Up @@ -40,7 +41,7 @@ void setup()

void loop()
{
Serial.print("Time:\t");
Serial.print("\tTime:\t");
Serial.println(millis());

for (int i = 0; i < sensorCount; i++)
Expand Down
9 changes: 6 additions & 3 deletions examples/max31855_demo1/max31855_demo1.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: max31855_demo1.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// VERSION: 0.1.4
// PURPOSE: thermocouple lib demo application
// DATE: 2014-01-02
// URL: https://github.com/RobTillaart/MAX31855_RT
Expand Down Expand Up @@ -35,9 +35,12 @@ void loop()
}

float temp = tc.getTemperature();
int m = temp*10 - 200;
int m = temp * 10 - 200;

Serial.print(temp);
Serial.print('\t');
for (int i=0; i< m; i++) Serial.write(']');
for (int i = 0; i < m; i++) Serial.write(']');
Serial.println();
}

// -- END OF FILE --
10 changes: 6 additions & 4 deletions examples/max31855_demo2/max31855_demo2.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: max31855_demo2.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.3
// VERSION: 0.1.4
// PURPOSE: thermocouple lib demo application
// DATE: 2014-01-02
// URL: https://github.com/RobTillaart/MAX31855_RT
Expand All @@ -17,7 +17,7 @@ MAX31855 tc(clPin, csPin, doPin);

float t1, t2;

void setup()
void setup()
{
Serial.begin(115200);
Serial.print("Start max31855_demo2: ");
Expand All @@ -30,12 +30,14 @@ void setup()
delay(1000);
}

void loop()
void loop()
{
tc.read();
t2 = tc.getTemperature();
Serial.print("delta:\t");
Serial.println(t2-t1, 2);
Serial.println(t2 - t1, 2);
t1 = t2;
delay(1000);
}

// -- END OF FILE --
8 changes: 5 additions & 3 deletions examples/max31855_demo4/max31855_demo4.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const int clPin = 5;

MAX31855 tc(clPin, csPin, doPin);

void setup()
void setup()
{
Serial.begin(115200);
Serial.print("Start max31855_demo4: ");
Expand All @@ -25,7 +25,7 @@ void setup()
tc.begin();

uint32_t start = micros();
for (int i=0; i< 10; i++) tc.read();
for (int i = 0; i < 10; i++) tc.read();
uint32_t stop = micros();
Serial.print("10x read:\t");
Serial.println(stop - start);
Expand Down Expand Up @@ -70,6 +70,8 @@ void setup()
Serial.println();
}

void loop()
void loop()
{
}

// -- END OF FILE --
12 changes: 7 additions & 5 deletions examples/max31855_demo5/max31855_demo5.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: max31855_demo5.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.2
// VERSION: 0.1.4
// PURPOSE: thermocouple lib demo application
// DATE: 2014-01-02
// URL: https://github.com/RobTillaart/MAX31855_RT
Expand All @@ -28,14 +28,14 @@ void setup()
Serial.print(" temp before:\t");
Serial.println(t1, 2);

float o = tc.getOffset();
float offset = tc.getOffset();
Serial.print("offset before:\t");
Serial.println(o, 2);
Serial.println(offset, 2);

tc.setOffset(3.14);
o = tc.getOffset();
offset = tc.getOffset();
Serial.print(" offset after:\t");
Serial.println(o, 2);
Serial.println(offset, 2);

tc.read();
float t2 = tc.getTemperature();
Expand All @@ -51,3 +51,5 @@ void setup()
void loop()
{
}

// -- END OF FILE --
15 changes: 9 additions & 6 deletions examples/max31855_test_error/max31855_test_error.ino
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: max31855_test_error.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.0
// VERSION: 0.1.1
// PURPOSE: thermocouple lib inline tests
// DATE: 2019-07-31
// URL: https://github.com/RobTillaart/MAX31855_RT
Expand All @@ -18,7 +18,7 @@ MAX31855 tc(clPin, csPin, doPin);
void setup()
{
Serial.begin(115200);
Serial.print("Start max31855_demo0: ");
Serial.print("Start max31855_test_error: ");
Serial.println(MAX31855_VERSION);
Serial.println();

Expand All @@ -34,9 +34,12 @@ void loop()
if (tc.getStatus())
{
Serial.print("error:\t\t");
if (tc.shortToGND()) Serial.println("SHORT TO GROUND");
if (tc.shortToVCC()) Serial.println("SHORT TO VCC");
if (tc.openCircuit()) Serial.println("OPEN CIRCUIT");
if (tc.shortToGND()) Serial.println("SHORT TO GROUND");
if (tc.shortToVCC()) Serial.println("SHORT TO VCC");
if (tc.openCircuit()) Serial.println("OPEN CIRCUIT");
if (tc.genericError()) Serial.println("GENERIC ERROR");
if (tc.noRead()) Serial.println("NO READ");
if (tc.noCommunication()) Serial.println("NO COMMUNICATION");
}

float internal = tc.getInternal();
Expand All @@ -49,4 +52,4 @@ void loop()
delay(1000);
}

// END OF FILE
// -- END OF FILE --
2 changes: 1 addition & 1 deletion keywords.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ shortToVCC KEYWORD2
openCircuit KEYWORD2
genericError KEYWORD2
noRead KEYWORD2
noCOmmunication KEYWORD2
noCommunication KEYWORD2

setOffset KEYWORD2
getOffset KEYWORD2
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/MAX31855_RT"
},
"version":"0.2.1",
"version":"0.2.2",
"frameworks": "arduino",
"platforms": "*"
}
Loading

0 comments on commit b67bc07

Please sign in to comment.