Skip to content

Commit 50c193d

Browse files
authored
Merge pull request #3 from ashutoshshrm529/units
Added Support for Fahrenheit and Kelvin units with an example
2 parents 91f77b3 + 65f88b6 commit 50c193d

File tree

5 files changed

+74
-11
lines changed

5 files changed

+74
-11
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 (in Fahrenheit and kelvins) 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(" K");
43+
44+
Serial.println();
45+
46+
delay(1000);
47+
}

keywords.txt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#######################################
22
# Syntax Coloring Map For Arduino_MKRTHERM
3-
#######################################
3+
#######################################
44
# Class
55
#######################################
66

@@ -9,8 +9,8 @@ MKRTHERM KEYWORD1
99
THERM KEYWORD1
1010

1111
#######################################
12-
# Methods and Functions
13-
#######################################
12+
# Methods and Functions
13+
#######################################
1414

1515
begin KEYWORD2
1616
end KEYWORD2
@@ -21,3 +21,7 @@ readReferenceTemperature KEYWORD2
2121
#######################################
2222
# Constants
2323
#######################################
24+
25+
FAHRENHEIT LITERAL1
26+
CELSIUS LITERAL1
27+
KELVIN LITERAL1

src/MKRTHERM.cpp

Lines changed: 10 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,13 @@ float THERMClass::readTemperature()
9696
// multiply for the LSB value
9797
celsius = (int32_t)rawword * 0.25f;
9898

99-
return celsius;
99+
if (units == FAHRENHEIT) {
100+
return (celsius * 9.0 / 5.0) + 32.0;
101+
} else if(units == KELVIN) {
102+
return (celsius + 273.15);
103+
} else {
104+
return celsius;
105+
}
100106
}
101107

102108
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)