Skip to content

Commit 142276e

Browse files
Added Support for Fahrenheit and Kelvin units
1 parent eb14d3e commit 142276e

File tree

4 files changed

+72
-8
lines changed

4 files changed

+72
-8
lines changed

examples/ReadSensor/ReadSensor.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
This example reads the temperatures measured by the thermocouple
55
connected to the MKR THERM shield and prints them to the Serial Monitor
66
once a second.
7-
7+
88
The circuit:
99
- Arduino MKR board
1010
- Arduino MKR THERM Shield attached
1111
- A K Type thermocouple temperature sensor connected to the shield
12-
12+
1313
This example code is in the public domain.
1414
*/
1515

@@ -34,7 +34,7 @@ void loop() {
3434
Serial.println(" °C");
3535

3636
Serial.print("Temperature ");
37-
Serial.print(THERM.readTemperature());
37+
Serial.print(THERM.readTemperature(CELSIUS));
3838
Serial.println(" °C");
3939

4040
Serial.println();
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
MKR THERM Shield - Read Sensors
3+
4+
This example reads the temperatures measured by the thermocouple
5+
connected to the MKR THERM shield and prints them to the Serial Monitor
6+
once a second.
7+
8+
The circuit:
9+
- Arduino MKR board
10+
- Arduino MKR THERM Shield attached
11+
- A K Type thermocouple temperature sensor connected to the shield
12+
13+
This example code is in the public domain.
14+
*/
15+
16+
#include <Arduino_MKRTHERM.h>
17+
18+
void setup() {
19+
20+
Serial.begin(9600);
21+
22+
while (!Serial);
23+
24+
if (!THERM.begin()) {
25+
Serial.println("Failed to initialize MKR THERM shield!");
26+
while (1);
27+
}
28+
}
29+
30+
void loop() {
31+
32+
Serial.print("Reference temperature ");
33+
Serial.print(THERM.readReferenceTemperature());
34+
Serial.println(" °C");
35+
36+
Serial.print("Temperature in Fahrenheit");
37+
Serial.print(THERM.readTemperature(FAHRENHEIT));
38+
Serial.println(" °F");
39+
40+
Serial.print("Temperature in Kelvin");
41+
Serial.print(THERM.readTemperature(KELVIN));
42+
Serial.println(" °F");
43+
44+
Serial.println();
45+
46+
delay(1000);
47+
}

src/MKRTHERM.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ uint32_t THERMClass::readSensor()
7373
return read;
7474
}
7575

76-
float THERMClass::readTemperature()
76+
float THERMClass::readTemperature(int units)
7777
{
7878
uint32_t rawword;
7979
float celsius;
@@ -82,9 +82,9 @@ float THERMClass::readTemperature()
8282

8383
// Check for reading error
8484
if (rawword & 0x7) {
85-
return NAN;
85+
return NAN;
8686
}
87-
// The temperature is stored in the last 14 word's bits
87+
// The temperature is stored in the last 14 word's bits
8888
// sendend by the Thermocouple-to-Digital Converter
8989
if (rawword & 0x80000000) {
9090
// Negative value, drop the lower 18 bits and explicitly extend sign bits.
@@ -96,7 +96,18 @@ float THERMClass::readTemperature()
9696
// multiply for the LSB value
9797
celsius = rawword * 0.25f;
9898

99-
return celsius;
99+
if(units=FAHRENHEIT)
100+
{
101+
return (celsius * 9.0 / 5.0) + 32.0;
102+
}
103+
else if(units=KELVIN)
104+
{
105+
return (celsius + 273.15);
106+
}
107+
else if(units=CELSIUS)
108+
{
109+
return celsius;
110+
}
100111
}
101112

102113
float THERMClass::readReferenceTemperature()

src/MKRTHERM.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,20 @@
2323
#include <Arduino.h>
2424
#include <SPI.h>
2525

26+
enum {
27+
FAHRENHEIT,
28+
CELSIUS,
29+
KELVIN
30+
};
31+
2632
class THERMClass {
2733
public:
2834
THERMClass(int cs = A4, SPIClass& spi = SPI);
2935

3036
int begin();
3137
void end();
3238

33-
float readTemperature();
39+
float readTemperature(int units = CELSIUS);
3440
float readReferenceTemperature();
3541

3642
private:

0 commit comments

Comments
 (0)