Skip to content

Commit a332ed2

Browse files
FischerMoseleyfischermoseley
andcommitted
Added begin and available functions, reworked examples for polling
Also added notes on packets being dropped, and tweaked the temperature reading syntax, added a soft resetToDefaults() function, currently working on -fpermissive enum typecasting stuff. This code probably doesn't work, just commiting so it's not lost. Co-Authored-By: Fischer Moseley <fischermoseley@users.noreply.github.com>
1 parent 0d0e2ee commit a332ed2

File tree

8 files changed

+123
-69
lines changed

8 files changed

+123
-69
lines changed

examples/Example1_BasicReadings/Example1_BasicReadings.ino

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ void setup(){
2020
Serial.begin(115200);
2121
Wire.begin();
2222
Wire.setClock(100000);
23+
tempSensor.begin();
2324

2425
//check if the sensor is connected
2526
if(tempSensor.isConnected()){
@@ -40,14 +41,16 @@ void setup(){
4041
}
4142
}
4243

43-
void loop(){ //print the thermocouple, ambient and delta temperatures every 200ms
44-
Serial.print("Thermocouple: ");
45-
Serial.print(tempSensor.thermocoupleTemp());
46-
Serial.print(" °C Ambient: ");
47-
Serial.print(tempSensor.ambientTemp());
48-
Serial.print(" °C Temperature Delta: ");
49-
Serial.print(tempSensor.tempDelta());
50-
Serial.print(" °C");
51-
Serial.println();
52-
delay(200);
44+
void loop(){ //print the thermocouple, ambient and delta temperatures every 200ms if available
45+
if(tempSensor.available()){
46+
Serial.print("Thermocouple: ");
47+
Serial.print(tempSensor.getThermocoupleTemp());
48+
Serial.print(" °C Ambient: ");
49+
Serial.print(tempSensor.getAmbientTemp());
50+
Serial.print(" °C Temperature Delta: ");
51+
Serial.print(tempSensor.getTempDelta());
52+
Serial.print(" °C");
53+
Serial.println();
54+
//delay(200);
55+
}
5356
}

examples/Example2_SetThermocoupleType/Example2_SetThermocoupleType.ino

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ void setup(){
2525
Serial.begin(115200);
2626
Wire.begin();
2727
Wire.setClock(100000);
28+
tempSensor.begin();
2829

2930
//check that the sensor is connected
3031
if(tempSensor.isConnected()){
@@ -59,13 +60,15 @@ void setup(){
5960
}
6061

6162
void loop(){ //print the thermocouple, ambient and delta temperatures every 200ms
62-
Serial.print("Thermocouple: ");
63-
Serial.print(tempSensor.thermocoupleTemp());
64-
Serial.print(" °C Ambient: ");
65-
Serial.print(tempSensor.ambientTemp());
66-
Serial.print(" °C Temperature Delta: ");
67-
Serial.print(tempSensor.tempDelta());
68-
Serial.print(" °C");
69-
Serial.println();
70-
delay(200);
63+
if(tempSensor.available()){
64+
Serial.print("Thermocouple: ");
65+
Serial.print(tempSensor.getThermocoupleTemp());
66+
Serial.print(" °C Ambient: ");
67+
Serial.print(tempSensor.getAmbientTemp());
68+
Serial.print(" °C Temperature Delta: ");
69+
Serial.print(tempSensor.getTempDelta());
70+
Serial.print(" °C");
71+
Serial.println();
72+
delay(200);
73+
}
7174
}

examples/Example3_SetFilterCoefficient/Example3_SetFilterCoefficient.ino

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@
1111
where 0 disables the filter, 1 corresponds to minimum filtering, and 7 enables maximum filtering. The "strength"
1212
of the filter just refers to how long it takes for the filter to respond to a step function input.
1313
14+
Quick Note! For some reason the getFilterCoefficient() function is a little wierd about returning the proper
15+
data. This is a known issue and while we've done our best to fix it, every once in a while it might return a 0,
16+
or the wrong value entirely. We think this is an issue with the MCP9600, and there's not much we can do about it.
17+
If you'd like to learn more or contact us, check out this issue on GitHub!
18+
19+
https://github.com/sparkfun/SparkFun_MCP9600_Arduino_Library/issues/1
20+
1421
Hardware Connections:
1522
Attach the Qwiic Shield to your Arduino/Photon/ESP32 or other
1623
Plug the sensor onto the shield
@@ -66,15 +73,15 @@ void setup(){
6673
}
6774

6875
void loop(){ //print the thermocouple, ambient and delta temperatures every 200ms
69-
Serial.print("Thermocouple: ");
70-
Serial.print(tempSensor.thermocoupleTemp());
71-
Serial.print(" °C Ambient: ");
72-
Serial.print(tempSensor.ambientTemp());
73-
Serial.print(" °C Temperature Delta: ");
74-
Serial.print(tempSensor.tempDelta());
75-
Serial.print(" °C");
76-
Serial.print(" Current coefficient: ");
77-
Serial.print(tempSensor.getFilterCoefficient(), BIN);
78-
Serial.println();
79-
delay(200);
76+
if(tempSensor.available()){
77+
Serial.print("Thermocouple: ");
78+
Serial.print(tempSensor.getThermocoupleTemp());
79+
Serial.print(" °C Ambient: ");
80+
Serial.print(tempSensor.getAmbientTemp());
81+
Serial.print(" °C Temperature Delta: ");
82+
Serial.print(tempSensor.getTempDelta());
83+
Serial.print(" °C");
84+
Serial.println();
85+
delay(200);
86+
}
8087
}

examples/Example4_SetResolution/Example4_SetResolution.ino

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,15 @@ void setup(){
7474
}
7575

7676
void loop(){ //print the thermocouple, ambient and delta temperatures every 200ms
77-
Serial.print("Thermocouple: ");
78-
Serial.print(tempSensor.thermocoupleTemp());
79-
Serial.print(" °C Ambient: ");
80-
Serial.print(tempSensor.ambientTemp());
81-
Serial.print(" °C Temperature Delta: ");
82-
Serial.print(tempSensor.tempDelta());
83-
Serial.print(" °C");
84-
Serial.println();
85-
delay(200);
77+
if(tempSensor.available()){
78+
Serial.print("Thermocouple: ");
79+
Serial.print(tempSensor.getThermocoupleTemp());
80+
Serial.print(" °C Ambient: ");
81+
Serial.print(tempSensor.getAmbientTemp());
82+
Serial.print(" °C Temperature Delta: ");
83+
Serial.print(tempSensor.getTempDelta());
84+
Serial.print(" °C");
85+
Serial.println();
86+
delay(200);
87+
}
8688
}

examples/Example5_BurstMode/Example5_BurstMode.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ void loop(){
5555

5656
//print the thermocouple, ambient and delta temperatures
5757
Serial.print("Thermocouple: ");
58-
Serial.print(tempSensor.thermocoupleTemp());
58+
Serial.print(tempSensor.getThermocoupleTemp());
5959
Serial.print(" °C Ambient: ");
60-
Serial.print(tempSensor.ambientTemp());
60+
Serial.print(tempSensor.getAmbientTemp());
6161
Serial.print(" °C Temperature Delta: ");
62-
Serial.print(tempSensor.tempDelta());
62+
Serial.print(tempSensor.getTempDelta());
6363
Serial.print(" °C");
6464
Serial.println();
6565

examples/Example6_ConfigureTemperatureAlert/Example6_ConfigureTemperatureAlert.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,11 @@ uint16_t updateTime = 200;
9090
void loop(){
9191
if((clock + updateTime) < millis()){
9292
Serial.print("Thermocouple: ");
93-
Serial.print(tempSensor.thermocoupleTemp());
93+
Serial.print(tempSensor.getThermocoupleTemp());
9494
Serial.print(" °C Ambient: ");
95-
Serial.print(tempSensor.ambientTemp());
95+
Serial.print(tempSensor.getAmbientTemp());
9696
Serial.print(" °C Temperature Delta: ");
97-
Serial.print(tempSensor.tempDelta());
97+
Serial.print(tempSensor.getTempDelta());
9898
Serial.print(" °C");
9999

100100
if(tempSensor.isTempGreaterThanLimit(risingAlert)){

src/SparkFun_MCP9600.cpp

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,22 @@ Distributed as-is; no warranty is given.
2626
#include "WProgram.h"
2727
#endif
2828

29-
//Class constructor
30-
MCP9600::MCP9600(uint8_t address, TwoWire &wirePort){
29+
/*-------------------------------- Device Status ------------------------*/
30+
31+
bool MCP9600::begin(uint8_t address, TwoWire &wirePort){
3132
_deviceAddress = address; //grab the address that the sensor is on
3233
_i2cPort = &wirePort; //grab the port that the user wants to use
34+
35+
//return true if the device is connected and the device ID is what we expect
36+
bool success = isConnected();
37+
success &= checkDeviceID();
38+
return success;
3339
}
3440

35-
36-
/*-------------------------------- Device Status ------------------------*/
41+
bool MCP9600::available(){
42+
uint8_t status = readSingleRegister(SENSOR_STATUS);
43+
return bitRead(status, 6);
44+
}
3745

3846
bool MCP9600::isConnected(){
3947
_i2cPort->beginTransmission(_deviceAddress);
@@ -48,25 +56,51 @@ bool MCP9600::checkDeviceID(){
4856
return (highByte(deviceID()) == DEV_ID_UPPER);
4957
}
5058

59+
bool MCP9600::resetToDefaults(){
60+
bool success = writeSingleRegister(SENSOR_STATUS, 0x00);
61+
success |= writeSingleRegister(THERMO_SENSOR_CONFIG, 0x00);
62+
success |= writeSingleRegister(DEVICE_CONFIG, 0x00);
63+
success |= writeSingleRegister(ALERT1_CONFIG, 0x00);
64+
success |= writeSingleRegister(ALERT2_CONFIG, 0x00);
65+
success |= writeSingleRegister(ALERT3_CONFIG, 0x00);
66+
success |= writeSingleRegister(ALERT4_CONFIG, 0x00);
67+
success |= writeSingleRegister(ALERT1_HYSTERESIS, 0x00);
68+
success |= writeSingleRegister(ALERT2_HYSTERESIS, 0x00);
69+
success |= writeSingleRegister(ALERT3_HYSTERESIS, 0x00);
70+
success |= writeSingleRegister(ALERT4_HYSTERESIS, 0x00);
71+
success |= writeDoubleRegister(ALERT1_LIMIT, 0x0000);
72+
success |= writeDoubleRegister(ALERT2_LIMIT, 0x0000);
73+
success |= writeDoubleRegister(ALERT3_LIMIT, 0x0000);
74+
success |= writeDoubleRegister(ALERT4_LIMIT, 0x0000);
75+
return success;
76+
}
5177

5278
/*----------------------------- Sensor Measurements ---------------------*/
5379

5480
float MCP9600::getThermocoupleTemp(bool units){
81+
//read register and convert to celcius
5582
int16_t raw = readDoubleRegister(HOT_JUNC_TEMP);
5683
float celcius = ((float)raw * DEV_RESOLUTION);
57-
units ? return celcius; : return (((float)celcius * 1.8f) + 32);
84+
85+
//clear data ready bit
86+
uint8_t status = readSingleRegister(SENSOR_STATUS);
87+
bitWrite(status, 6, 0);
88+
writeSingleRegister(SENSOR_STATUS, status);
89+
90+
//return in celcius or freedom units
91+
return units ? celcius : (((float)celcius * 1.8f) + 32);
5892
}
5993

6094
float MCP9600::getAmbientTemp(bool units){
6195
int16_t raw = readDoubleRegister(COLD_JUNC_TEMP);
6296
float celcius = ((float)raw * DEV_RESOLUTION);
63-
units ? return celcius; : return (((float)celcius * 1.8f) + 32);
97+
return units ? celcius : (((float)celcius * 1.8f) + 32);
6498
}
6599

66100
float MCP9600::getTempDelta(bool units){
67101
int16_t raw = readDoubleRegister(DELTA_JUNC_TEMP);
68102
float celcius = ((float)raw * DEV_RESOLUTION);
69-
units ? return celcius; : return (((float)celcius * 1.8f) + 32);
103+
return units ? celcius : (((float)celcius * 1.8f) + 32);
70104
}
71105

72106
signed long MCP9600::getRawADC(){
@@ -103,7 +137,7 @@ bool MCP9600::setAmbientResolution(Ambient_Resolution res){
103137

104138
Ambient_Resolution MCP9600::getAmbientResolution(){
105139
uint8_t config = readSingleRegister(DEVICE_CONFIG); //grab current device configuration
106-
return bitRead(config, 7); //return 7th bit from config register
140+
return static_cast<Ambient_Resolution>(bitRead(config, 7)); //return 7th bit from config register
107141
}
108142

109143
bool MCP9600::setThermocoupleResolution(Thermocouple_Resolution res){
@@ -125,7 +159,7 @@ Thermocouple_Resolution MCP9600::getThermocoupleResolution(){
125159
bool lowResolutionBit = bitRead(config, 5);
126160
bitWrite(res, 1, highResolutionBit); //set 1st bit of the enum to the 6th bit of the config register
127161
bitWrite(res, 0, lowResolutionBit); //set 0th bit of the enum to the 5th bit of the config register
128-
return res;
162+
return static_cast<Thermocouple_Resolution>(res);
129163
}
130164

131165
uint8_t MCP9600::setThermocoupleType(Thermocouple_Type type){
@@ -142,7 +176,7 @@ uint8_t MCP9600::setThermocoupleType(Thermocouple_Type type){
142176

143177
Thermocouple_Type MCP9600::getThermocoupleType(){
144178
uint8_t config = readSingleRegister(THERMO_SENSOR_CONFIG);
145-
return (config >> 4); //clear the non-thermocouple-type bits in the config registe
179+
return static_cast<Thermocouple_Type>(config >> 4); //clear the non-thermocouple-type bits in the config registe
146180
}
147181

148182
uint8_t MCP9600::setFilterCoefficient(uint8_t coefficient){
@@ -475,6 +509,9 @@ bool MCP9600::isTempGreaterThanLimit(uint8_t number){
475509
/*------------------------- Internal I2C Abstraction ---------------- */
476510

477511
uint8_t MCP9600::readSingleRegister(MCP9600_Register reg){
512+
//Attempt to read the register until we exit with no error code
513+
//This attempts to fix the bug where clock stretching sometimes failes, as
514+
//described in the MCP9600 eratta
478515
for(uint8_t attempts; attempts <= retryAttempts; attempts++){
479516
_i2cPort->beginTransmission(DEV_ADDR);
480517
_i2cPort->write(reg);
@@ -487,6 +524,9 @@ uint8_t MCP9600::readSingleRegister(MCP9600_Register reg){
487524
}
488525

489526
uint16_t MCP9600::readDoubleRegister(MCP9600_Register reg){
527+
//Attempt to read the register until we exit with no error code
528+
//This attempts to fix the bug where clock stretching sometimes failes, as
529+
//described in the MCP9600 eratta
490530
for(byte attempts; attempts <= retryAttempts; attempts++){
491531
_i2cPort->beginTransmission(DEV_ADDR);
492532
_i2cPort->write(reg);

src/SparkFun_MCP9600.h

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Distributed as-is; no warranty is given.
3030
#define retryAttempts 3 //how many times to attempt to read a register from the thermocouple before giving up
3131

3232
// register pointers for various device functions
33-
enum MCP9600_Register {
33+
enum MCP9600_Register: uint8_t {
3434
HOT_JUNC_TEMP = 0x00,
3535
DELTA_JUNC_TEMP = 0x01,
3636
COLD_JUNC_TEMP = 0x02,
@@ -53,7 +53,7 @@ enum MCP9600_Register {
5353
DEVICE_ID = 0x20,
5454
};
5555

56-
enum Thermocouple_Type {
56+
enum Thermocouple_Type: uint8_t {
5757
TYPE_K = 0b000,
5858
TYPE_J = 0b001,
5959
TYPE_T = 0b010,
@@ -64,19 +64,19 @@ enum Thermocouple_Type {
6464
TYPE_R = 0b111,
6565
};
6666

67-
enum Ambient_Resolution {
68-
RES_ZERO_POINT_0625 = 0b0,
69-
RES_ZERO_POINT_25 = 0b1,
67+
enum Ambient_Resolution: bool {
68+
RES_ZERO_POINT_0625 = 0,
69+
RES_ZERO_POINT_25 = 1,
7070
};
7171

72-
enum Thermocouple_Resolution {
72+
enum Thermocouple_Resolution: uint8_t {
7373
RES_18_BIT = 0b00,
7474
RES_16_BIT = 0b01,
7575
RES_14_BIT = 0b10,
7676
RES_12_BIT = 0b11,
7777
};
7878

79-
enum Burst_Sample {
79+
enum Burst_Sample: uint8_t {
8080
SAMPLES_1 = 0b000,
8181
SAMPLES_2 = 0b001,
8282
SAMPLES_4 = 0b010,
@@ -87,7 +87,7 @@ enum Burst_Sample {
8787
SAMPLES_128 = 0b111,
8888
};
8989

90-
enum Shutdown_Mode {
90+
enum Shutdown_Mode: uint8_t {
9191
NORMAL = 0x00,
9292
SHUTDOWN = 0x01,
9393
BURST = 0x02,
@@ -96,21 +96,20 @@ enum Shutdown_Mode {
9696
class MCP9600{
9797
public:
9898

99-
//Class constructor
100-
MCP9600(uint8_t address = DEV_ADDR, TwoWire &wirePort = Wire);
101-
10299
//Device status
100+
bool begin(uint8_t address = DEV_ADDR, TwoWire &wirePort = Wire); //Sets device I2C address to a user-specified address, over whatever port the user specifies.
101+
bool available(); //Returns true if the thermocouple (hot) junction temperature has been updated since we last checked. Also referred to as the data ready bit.
103102
bool isConnected(); //Returns true if the thermocouple will acknowledge over I2C, and false otherwise
104103
uint16_t deviceID(); //Returns the contents of the device ID register. The upper 8 bits are constant, but the lower contain revision data.
105104
bool checkDeviceID(); //Returns true if the constant upper 8 bits in the device ID register are what they should be according to the datasheet.
106105
bool resetToDefaults(); //Resets all device parameters to their default values. Returns 1 if there was an error, zero otherwise.
107106

108107
//Sensor measurements
109-
float getThermocoupleTemp(bool units = true); //Returns the thermocouple temperature. Set units to true for Celcius, or false for freedom units (Fahrenheit)
108+
float getThermocoupleTemp(bool units = true); //Returns the thermocouple temperature, and clears the data ready bit. Set units to true for Celcius, or false for freedom units (Fahrenheit)
110109
float getAmbientTemp(bool units = true); //Returns the ambient (IC die) temperature. Set units to true for Celcius, or false for freedom units (Fahrenheit)
111110
float getTempDelta(bool units = true); //Returns the difference in temperature between the thermocouple and ambient junctions. Set units to true for Celcius, or false for freedom units (Fahrenheit)
112-
signed long getRawADC(); //Returns the raw contents of the raw ADC register
113-
bool isInputRangeExceeded(); //Returns true if the MCP9600's EMF range has been exceeded, and false otherwise.
111+
signed long getRawADC(); //Returns the raw contents of the raw ADC register
112+
bool isInputRangeExceeded(); //Returns true if the MCP9600's EMF range has been exceeded, and false otherwise.
114113

115114
//Measurement configuration
116115
bool setAmbientResolution(Ambient_Resolution res); //Changes the resolution on the cold (ambient) junction, for either 0.0625 or 0.25 degree C resolution. Lower resolution reduces conversion time.

0 commit comments

Comments
 (0)