Skip to content

Commit 66382c1

Browse files
committed
Initial structure
1 parent 990e95a commit 66382c1

File tree

6 files changed

+256
-1
lines changed

6 files changed

+256
-1
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1-
# MKRTherm
1+
= MKRTherm Library for Arduino =
2+
3+
Allows you to read the temperature sensors conencted to your MKR Therm shield.
4+
5+
== License ==
6+
7+
Copyright (c) 2018 Arduino SA. All rights reserved.
8+
9+
This library is free software; you can redistribute it and/or
10+
modify it under the terms of the GNU Lesser General Public
11+
License as published by the Free Software Foundation; either
12+
version 2.1 of the License, or (at your option) any later version.
13+
14+
This library is distributed in the hope that it will be useful,
15+
but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17+
Lesser General Public License for more details.
18+
19+
You should have received a copy of the GNU Lesser General Public
20+
License along with this library; if not, write to the Free Software
21+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA

examples/ReadSensor/ReadSensor.ino

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
MKR ENV Shield - Read Sensors
3+
4+
This example reads the sensor connected to the MKR Therm shield
5+
and prints them to the Serial Monitor once a second.
6+
7+
The circuit:
8+
- Arduino MKR board
9+
- Arduino MKR Therm Shield attached
10+
- A Thermocouple temperature sensor connected to the shield
11+
12+
This example code is in the public domain.
13+
*/
14+
15+
#include <MKRTherm.h>
16+
17+
void setup() {
18+
19+
Serial.begin(9600);
20+
21+
while (!Serial);
22+
23+
THERM.begin();
24+
}
25+
26+
void loop() {
27+
28+
Serial.print("ref ");
29+
Serial.print(THERM.readRefTemperature());
30+
Serial.println(" °C");
31+
32+
Serial.print("celcius ");
33+
Serial.print(THERM.readCelsiusTemperature());
34+
Serial.println(" °C");
35+
36+
Serial.println();
37+
38+
delay(1000);
39+
}

keywords.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#######################################
2+
# Syntax Coloring Map For MKRTherm
3+
#######################################
4+
# Class
5+
#######################################
6+
7+
MKRTherm KEYWORD1
8+
THERM KEYWORD1
9+
10+
#######################################
11+
# Methods and Functions
12+
#######################################
13+
14+
begin KEYWORD2
15+
end KEYWORD2
16+
17+
readCelsiusTemperature KEYWORD2
18+
readRefTemperature KEYWORD2
19+
20+
#######################################
21+
# Constants
22+
#######################################

library.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=MKRTherm
2+
version=1.0.0
3+
author=Arduino
4+
maintainer=Arduino <[email protected]>
5+
sentence=Allows you to read the temperature sensors conencted to your MKR Therm shield.
6+
paragraph=
7+
category=Sensors
8+
url=https://github.com/arduino-libraries/MKRTherm
9+
architectures=samd
10+
includes=MKRTherm.h

src/MKRTherm.cpp

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
This file is part of the MKRTherm library.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
#include "MKRTherm.h"
21+
22+
#include "SPI.h"
23+
24+
THERMClass::THERMClass(int cs) : _cs(cs)
25+
{
26+
}
27+
28+
int THERMClass::begin()
29+
{
30+
pinMode(_cs, OUTPUT);
31+
digitalWrite(_cs, HIGH);
32+
SPI.begin();
33+
34+
return 1;
35+
}
36+
37+
void THERMClass::end()
38+
{
39+
SPI.end();
40+
}
41+
42+
uint32_t THERMClass::readSensor()
43+
{
44+
uint32_t read;
45+
46+
digitalWrite(_cs, LOW);
47+
delay(1);
48+
49+
SPI.beginTransaction(SPISettings(4000000, MSBFIRST, SPI_MODE0));
50+
51+
read = SPI.transfer(0);
52+
read <<= 8;
53+
read |= SPI.transfer(0);
54+
read <<= 8;
55+
read |= SPI.transfer(0);
56+
read <<= 8;
57+
read |= SPI.transfer(0);
58+
59+
SPI.endTransaction();
60+
61+
digitalWrite(_cs, HIGH);
62+
delay(1);
63+
return read;
64+
}
65+
66+
67+
double THERMClass::readCelsiusTemperature()
68+
{
69+
uint32_t rawword;
70+
double celsius;
71+
72+
rawword = readSensor();
73+
74+
// Check for reading error
75+
if (rawword & 0x7) {
76+
return NAN;
77+
}
78+
// The temperature is stored in the last 14 word's bits
79+
// sendend by the Thermocouple-to-Digital Converter
80+
if (rawword & 0x80000000) {
81+
// Negative value, drop the lower 18 bits and explicitly extend sign bits.
82+
rawword = 0xFFFFC000 | ((rawword >> 18) & 0x00003FFFF);
83+
} else {
84+
// Positive value, just drop the lower 18 bits.
85+
rawword >>= 18;
86+
}
87+
// multiply for the LSB value
88+
celsius = rawword*0.25f;
89+
90+
return celsius;
91+
}
92+
93+
94+
95+
double THERMClass::readRefTemperature()
96+
{
97+
uint32_t rawword;
98+
double ref;
99+
100+
rawword = readSensor();
101+
102+
// ignore first 4 FAULT bits
103+
rawword >>= 4;
104+
105+
// The cold junction reference temperature is stored in the first 11 word's bits
106+
// sendend by the Thermocouple-to-Digital Converter
107+
rawword = rawword & 0x7FF;
108+
// check sign bit and convert to negative value.
109+
if (rawword & 0x800) {
110+
ref = (0xF800 | (rawword & 0x7FF))*0.0625;
111+
} else {
112+
// multiply for the LSB value
113+
ref = rawword * 0.0625f;
114+
}
115+
116+
return ref;
117+
}
118+
119+
THERMClass THERM;

src/MKRTherm.h

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
This file is part of the MKRTherm library.
3+
Copyright (c) 2018 Arduino SA. All rights reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
21+
#ifndef _MKRTHERM_H_
22+
#define _MKRTHERM_H_
23+
24+
25+
#include "Arduino.h"
26+
#include <SPI.h>
27+
28+
29+
class THERMClass {
30+
private:
31+
int _cs;
32+
public:
33+
THERMClass(int cs = A4);
34+
35+
int begin();
36+
void end();
37+
38+
uint32_t readSensor();
39+
double readCelsiusTemperature();
40+
double readRefTemperature();
41+
};
42+
43+
extern THERMClass THERM;
44+
45+
#endif

0 commit comments

Comments
 (0)