Skip to content

Commit 55c1fe8

Browse files
committed
MAX6875 Temp Sensor example
1 parent 843e163 commit 55c1fe8

File tree

6 files changed

+292
-0
lines changed

6 files changed

+292
-0
lines changed

libraries/MAX6675/MAX6675.cpp

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
MAX6675.cpp - Library for reading temperature from a MAX6675.
3+
4+
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
5+
http://creativecommons.org/licenses/by-sa/3.0/
6+
*/
7+
8+
#include <MAX6675.h>
9+
10+
MAX6675::MAX6675(uint8_t CS_pin, uint8_t SO_pin, uint8_t SCK_pin, uint8_t units)
11+
{
12+
pinMode(CS_pin, OUTPUT);
13+
pinMode(SO_pin, INPUT);
14+
pinMode(SCK_pin, OUTPUT);
15+
16+
digitalWrite(CS_pin, HIGH);
17+
18+
_CS_pin = CS_pin;
19+
_SO_pin = SO_pin;
20+
_SCK_pin = SCK_pin;
21+
_units = units;
22+
}
23+
24+
float MAX6675::read_temp()
25+
{
26+
uint16_t value = 0;
27+
uint8_t error_tc = 0;
28+
float temp = 0.0;
29+
30+
/*
31+
Initiate a temperature conversion. According to MAX's tech notes FAQ's
32+
for the chip, Line going high initiates a conversion, which means, we
33+
need to clock the chip low to high to initiate the conversion, then wait
34+
for the conversion to be complete before trying to read the data from
35+
the chip.
36+
*/
37+
digitalWrite(_CS_pin,LOW);
38+
delay(2);
39+
digitalWrite(_CS_pin,HIGH);
40+
delay(220);
41+
42+
/* Read the chip and return the raw temperature value */
43+
44+
/*
45+
Bring CS pin low to allow us to read the data from
46+
the conversion process
47+
*/
48+
digitalWrite(_CS_pin,LOW);
49+
50+
/* Cycle the clock for dummy bit 15 */
51+
digitalWrite(_SCK_pin,HIGH);
52+
delay(1);
53+
digitalWrite(_SCK_pin,LOW);
54+
55+
/*
56+
Read bits 14-3 from MAX6675 for the Temp. Loop for each bit reading
57+
the value and storing the final value in 'temp'
58+
*/
59+
for (int i=11; i>=0; i--) {
60+
digitalWrite(_SCK_pin,HIGH);
61+
value += digitalRead(_SO_pin) << i;
62+
digitalWrite(_SCK_pin,LOW);
63+
}
64+
65+
/* Read the TC Input inp to check for TC Errors */
66+
digitalWrite(_SCK_pin,HIGH);
67+
error_tc = digitalRead(_SO_pin);
68+
digitalWrite(_SCK_pin,LOW);
69+
70+
/*
71+
Read the last two bits from the chip, faliure to do so will result
72+
in erratic readings from the chip.
73+
*/
74+
for (int i=1; i>=0; i--) {
75+
digitalWrite(_SCK_pin,HIGH);
76+
delay(1);
77+
digitalWrite(_SCK_pin,LOW);
78+
}
79+
80+
// Disable Device
81+
digitalWrite(_CS_pin, HIGH);
82+
83+
/*
84+
Keep in mind that the temp that was just read is on the digital scale
85+
from 0˚C to 1023.75˚C at a resolution of 2^12. We now need to convert
86+
to an actual readable temperature (this drove me nuts until I figured
87+
this out!). Now multiply by 0.25. I tried to avoid float math but
88+
it is tough to do a good conversion to ˚F. THe final value is converted
89+
to an int and returned at x10 power.
90+
91+
2 = temp in deg F
92+
1 = temp in deg C
93+
0 = raw chip value 0-4095
94+
*/
95+
if(_units == 2) {
96+
temp = (value*0.25) * 9.0/5.0 + 32.0;
97+
} else if(_units == 1) {
98+
temp = (value*0.25);
99+
} else {
100+
temp = value;
101+
}
102+
103+
/* Output negative of CS_pin if there is a TC error, otherwise return 'temp' */
104+
if(error_tc != 0) {
105+
return -_CS_pin;
106+
} else {
107+
return temp;
108+
}
109+
}

libraries/MAX6675/MAX6675.h

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
MAX6675.h - Library for reading temperature from a MAX6675.
3+
4+
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
5+
http://creativecommons.org/licenses/by-sa/3.0/
6+
*/
7+
8+
#ifndef MAX6675_h
9+
#define MAX6675_h
10+
11+
#if defined(ARDUINO) && ARDUINO >= 100
12+
#include "Arduino.h"
13+
#else
14+
#include "WProgram.h"
15+
#endif
16+
17+
class MAX6675
18+
{
19+
public:
20+
MAX6675(uint8_t CS_pin, uint8_t SO_pin, uint8_t SCK_pin, uint8_t units);
21+
float read_temp();
22+
private:
23+
uint8_t _CS_pin;
24+
uint8_t _SO_pin;
25+
uint8_t _SCK_pin;
26+
uint8_t _units;
27+
uint8_t chip_read(uint8_t CS_pin, uint8_t &error_tc);
28+
};
29+
30+
#endif

libraries/MAX6675/README.markdown

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
MAX6675 Arduino Library
2+
=======================
3+
Version: **2.0.1**
4+
5+
This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>.
6+
7+
8+
Summary
9+
-------
10+
11+
The primary use of the library is to easily interface with a MAX6675 chip via it's SPI interface. Use the following code to initialize the library. V2 also greatly improves performance and timing of the chip for more accurate reads. Thanks Ed!
12+
13+
MAX6675 temp0(CS,SO,SCK,units);
14+
15+
Variables:
16+
17+
* CS is chip select pin of chip
18+
* SO is data out of the chip
19+
* SCK is the clock pin of the chip
20+
* units is one of the following:
21+
* 0 = raw 0-4095 value
22+
* 1 = temp in ˚C
23+
* 2 = temp in ˚F
24+
25+
Following this you can use the _read_temp()_ function to return the temperature as a float.
26+
27+
temperature = temp0.read_temp();
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
Single_Temp.pde - Example using the MAX6675 Library.
3+
Created by Ryan McLaughlin <[email protected]>
4+
5+
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
6+
http://creativecommons.org/licenses/by-sa/3.0/
7+
*/
8+
9+
#include <MAX6675.h>
10+
11+
int LED1 = 9; // Status LED Pin
12+
int CS = 10; // CS pin on MAX6675
13+
int SO = 12; // SO pin of MAX6675
14+
int SCK = 13; // SCK pin of MAX6675
15+
int units = 2; // Units to readout temp (0 = raw, 1 = ˚C, 2 = ˚F)
16+
float temperature = 0.0; // Temperature output variable
17+
18+
19+
// Initialize the MAX6675 Library for our chip
20+
MAX6675 temp(CS,SO,SCK,units);
21+
22+
23+
// Setup Serial output and LED Pin
24+
// MAX6675 Library already sets pin modes for MAX6675 chip!
25+
void setup() {
26+
Serial.begin(9600);
27+
pinMode(LED1, OUTPUT);
28+
}
29+
30+
void loop() {
31+
// Read the temp from the MAX6675
32+
temperature = temp.read_temp();
33+
34+
if(temperature < 0) {
35+
// If there is an error with the TC, temperature will be < 0
36+
Serial.print("Thermocouple Error on CS");
37+
Serial.println( temperature );
38+
digitalWrite(LED1, HIGH);
39+
} else {
40+
Serial.print("Current Temperature: ");
41+
Serial.println( temperature );
42+
digitalWrite(LED1, LOW);
43+
}
44+
45+
// Wait one second before reading again
46+
delay(1000);
47+
}

libraries/MAX6675/keywords.txt

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#######################################
2+
# Syntax Coloring Map For MAX6675
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
MAX6675 KEYWORD1
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
read_temp KEYWORD2
16+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// MAX6875 Hight temperature sensor with range: -200 degC - +1300 degC
2+
// Contribution by MrLynx
3+
// http://forum.mysensors.org/topic/641/max6675
4+
//
5+
// Connect:
6+
// MAX6675 - Arduino
7+
// CS0 - 4
8+
// SO - 3
9+
// SCLK - 5
10+
// VCC - 5V
11+
// GND - GND
12+
13+
#include <MySensor.h>
14+
#include <MAX6675.h>
15+
#include <SPI.h>
16+
uint8_t CS0 = 4; // CS pin on MAX6675
17+
uint8_t SO = 3; // SO pin of MAX6675
18+
uint8_t SCLK = 5; // SCK pin of MAX6675
19+
uint8_t units = 1; // Units to readout temp (0 = ˚F, 1 = ˚C)
20+
float temperature = 0.0; // Temperature output variable
21+
float lastTemperature;
22+
unsigned long SLEEP_TIME = 30000;
23+
boolean metric = true;
24+
MySensor gw;
25+
26+
MyMessage msg(0, V_TEMP);
27+
28+
// Initialize the MAX6675 Library for our chip
29+
30+
MAX6675 temp0(CS0, SO, SCLK, units);
31+
32+
void setup()
33+
{
34+
// Startup and initialize MySensors library. Set callback for incoming messages.
35+
gw.begin();
36+
37+
// Send the sketch version information to the gateway and Controller
38+
gw.sendSketchInfo("Max6675 Temp Sensor", "1.0");
39+
40+
// Present all sensors to controller
41+
gw.present(0, S_TEMP);
42+
}
43+
44+
void loop()
45+
{
46+
// Process incoming messages (like config from server)
47+
gw.process();
48+
49+
temperature = temp0.read_temp(); // Read the temp
50+
51+
if (temperature < 0) { // If there is an error with the TC, temperature will be < 0
52+
Serial.println("Thermocouple Error!!"); // There is a thermocouple error
53+
} else {
54+
Serial.print("Current Temperature: ");
55+
Serial.println( temperature ); // Print the temperature to Serial
56+
if (temperature != lastTemperature)
57+
gw.send(msg.setSensor(0).set(temperature, 1));
58+
lastTemperature = temperature;
59+
}
60+
61+
gw.sleep(SLEEP_TIME);
62+
}

0 commit comments

Comments
 (0)