/ adafruit_mlx90614.py
adafruit_mlx90614.py
  1  # The MIT License (MIT)
  2  #
  3  # Copyright (c) 2018 Mikey Sklar for Adafruit Industries
  4  #
  5  # Permission is hereby granted, free of charge, to any person obtaining a copy
  6  # of this software and associated documentation files (the "Software"), to deal
  7  # in the Software without restriction, including without limitation the rights
  8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9  # copies of the Software, and to permit persons to whom the Software is
 10  # furnished to do so, subject to the following conditions:
 11  #
 12  # The above copyright notice and this permission notice shall be included in
 13  # all copies or substantial portions of the Software.
 14  #
 15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 21  # THE SOFTWARE.
 22  """
 23  `adafruit_mlx90614`
 24  ====================================================
 25  
 26  CircuitPython module for the MLX90614 IR object temperature sensor.
 27  
 28  * Author(s): Mikey Sklar based on code from these projects:
 29    Limor Fried - https://github.com/adafruit/Adafruit-MLX90614-Library
 30    Bill Simpson - https://github.com/BillSimpson/ada_mlx90614
 31    Mike Causer - https://github.com/mcauser/micropython-mlx90614
 32  
 33  Implementation Notes
 34  --------------------
 35  
 36  **Hardware:**
 37  
 38  * Adafruit `Melexis Contact-less Infrared Sensor - MLX90614 3V
 39    <https://www.adafruit.com/product/1747>`_ (Product ID: 1747)
 40  
 41  * Adafruit `Melexis Contact-less Infrared Sensor - MLX90614 5V
 42    <https://www.adafruit.com/product/1748>`_ (Product ID: 1748)
 43  
 44  * Sensors:
 45    https://www.adafruit.com/product/1747
 46    https://www.adafruit.com/product/1748
 47  
 48  * Datasheet:
 49    https://cdn-shop.adafruit.com/datasheets/MLX90614.pdf
 50  
 51  **Software and Dependencies:**
 52  
 53  * Adafruit CircuitPython firmware for the supported boards:
 54    https://github.com/adafruit/circuitpython/releases
 55  """
 56  
 57  from micropython import const
 58  
 59  import adafruit_bus_device.i2c_device as i2c_device
 60  
 61  
 62  # imports
 63  
 64  __version__ = "0.0.0-auto.0"
 65  __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_mlx90614.git"
 66  
 67  # Internal constants:
 68  _MLX90614_I2CADDR = const(0x5A)
 69  
 70  # RAM
 71  _MLX90614_RAWIR1 = const(0x04)
 72  _MLX90614_RAWIR2 = const(0x05)
 73  _MLX90614_TA = const(0x06)
 74  _MLX90614_TOBJ1 = const(0x07)
 75  _MLX90614_TOBJ2 = const(0x08)
 76  
 77  # EEPROM
 78  _MLX90614_TOMAX = const(0x20)
 79  _MLX90614_TOMIN = const(0x21)
 80  _MLX90614_PWMCTRL = const(0x22)
 81  _MLX90614_TARANGE = const(0x23)
 82  _MLX90614_EMISS = const(0x24)
 83  _MLX90614_CONFIG = const(0x25)
 84  _MLX90614_ADDR = const(0x0E)
 85  _MLX90614_ID1 = const(0x3C)
 86  _MLX90614_ID2 = const(0x3D)
 87  _MLX90614_ID3 = const(0x3E)
 88  _MLX90614_ID4 = const(0x3F)
 89  
 90  
 91  class MLX90614:
 92      """Create an instance of the MLX90614 temperature sensor.  You must pass in
 93      the following parameters:
 94      - i2c: An instance of the I2C bus connected to the sensor.
 95      - frequency=100000 - this sensor does not respond to the default 400000 i2c bus speed
 96  
 97      Optionally you can specify:
 98      - address: The I2C address of the sensor.
 99      If not specified the sensor's default value will be assumed."""
100  
101      def __init__(self, i2c_bus, address=_MLX90614_I2CADDR):
102          self._device = i2c_device.I2CDevice(i2c_bus, address)
103          self.buf = bytearray(2)
104          self.buf[0] = _MLX90614_CONFIG
105  
106      @property
107      def ambient_temperature(self):
108          """Ambient Temperature in celsius."""
109          return self._read_temp(_MLX90614_TA)
110  
111      @property
112      def object_temperature(self):
113          """Object Temperature in celsius."""
114          return self._read_temp(_MLX90614_TOBJ1)
115  
116      def _read_temp(self, register):
117          temp = self._read_16(register)
118          temp *= 0.02
119          temp -= 273.15
120          return temp
121  
122      def _read_16(self, register):
123          # Read and return a 16-bit unsigned big endian value read from the
124          # specified 16-bit register address.
125          with self._device as i2c:
126              self.buf[0] = register
127              i2c.write_then_readinto(self.buf, self.buf, out_end=1)
128              return self.buf[1] << 8 | self.buf[0]