AskSin++
MLX90614.h
1 //- -----------------------------------------------------------------------------------------------------------------------
2 // AskSin++
3 // 2018-04-03 papa Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
4 // 2018-08-25 jp112sdl Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
5 //- -----------------------------------------------------------------------------------------------------------------------
6 
7 /***************************************************
8  This is a library for the MLX90614 Temp Sensor
9 
10  Designed specifically to work with the MLX90614 sensors in the
11  adafruit shop
12  ----> https://www.adafruit.com/products/1748
13  ----> https://www.adafruit.com/products/1749
14 
15  These sensors use I2C to communicate, 2 pins are required to
16  interface
17  Adafruit invests time and resources providing this open source code,
18  please support Adafruit and open-source hardware by purchasing
19  products from Adafruit!
20 
21  Written by Limor Fried/Ladyada for Adafruit Industries.
22  BSD license, all text above must be included in any redistribution
23  ****************************************************/
24 
25 #ifndef __SENSORS_MLX90614_h__
26 #define __SENSORS_MLX90614_h__
27 
28 #include <Sensors.h>
29 #include <Wire.h>
30 
31 #if defined(ARDUINO) && ARDUINO >= 100
32 #include "Arduino.h"
33 #else
34 #include "WProgram.h"
35 #endif
36 
37 // RAM
38 #define MLX90614_RAWIR1 0x04
39 #define MLX90614_RAWIR2 0x05
40 #define MLX90614_TA 0x06
41 #define MLX90614_TOBJ1 0x07
42 #define MLX90614_TOBJ2 0x08
43 // EEPROM
44 #define MLX90614_TOMAX 0x20
45 #define MLX90614_TOMIN 0x21
46 #define MLX90614_PWMCTRL 0x22
47 #define MLX90614_TARANGE 0x23
48 #define MLX90614_EMISS 0x24
49 #define MLX90614_CONFIG 0x25
50 #define MLX90614_ADDR 0x0E
51 #define MLX90614_ID1 0x3C
52 #define MLX90614_ID2 0x3D
53 #define MLX90614_ID3 0x3E
54 #define MLX90614_ID4 0x3F
55 
56 namespace as {
57 
58 template <byte ADDRESS=0x5a>
59 class MLX90614 : public Sensor {
60 public:
61  int16_t _temperatureAmb = -400;
62  int16_t _temperatureObj = -400;
63 
64  void init () {
65  Wire.begin();
66  _present = read16(MLX90614_CONFIG) != 0xffff;
67  DPRINT("MLX90614 ");
68  if (_present) {
69  DPRINTLN("OK");
70  } else {
71  DPRINTLN("ERROR");
72  }
73  }
74  void measure (__attribute__((unused)) bool async=false) {
75  if( present() == true ) {
76  _temperatureAmb = readAmbientTempC();
77  //DPRINT("MLX90614 Ambient T = ");DDECLN(_temperatureAmb);
78  _temperatureObj = readObjectTempC();
79  //DPRINT("MLX90614 Object T = ");DDECLN(_temperatureObj);
80  }
81  }
82 private:
83  int16_t readObjectTempC(void) {
84  return readTemp(MLX90614_TOBJ1);
85  }
86 
87  int16_t readAmbientTempC(void) {
88  return readTemp(MLX90614_TA);
89  }
90 
91  float readTemp(uint8_t reg) {
92  float temp;
93 
94  temp = read16(reg);
95  temp = read16(reg); // have to do this twice
96  temp *= .02;
97  temp -= 273.15;
98  return temp * 10;
99  }
100 
101  uint16_t read16(uint8_t a) {
102  uint16_t ret;
103 
104  Wire.beginTransmission(ADDRESS); // start transmission to device
105  Wire.write(a); // sends register address to read from
106  Wire.endTransmission(false); // end transmission
107 
108  Wire.requestFrom(ADDRESS, (uint8_t)3);// send data n-bytes read
109  ret = Wire.read(); // receive DATA
110  ret |= Wire.read() << 8; // receive DATA
111 
112  uint8_t pec = Wire.read();
113 
114  return ret;
115  }
116 public:
117  int16_t temperatureAmb () { return _temperatureAmb; }
118  int16_t temperatureObj () { return _temperatureObj; }
119 };
120 
121 }
122 #endif
as::MLX90614
Definition: MLX90614.h:59
as::Sensor
Definition: Sensors.h:11