/ adafruit_mcp9808.py
adafruit_mcp9808.py
1 # The MIT License (MIT) 2 # 3 # Copyright (c) 2017 Scott Shawcroft 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_mcp9808` - MCP9808 I2C Temperature Sensor 24 ==================================================== 25 26 CircuitPython library to support MCP9808 high accuracy temperature sensor. 27 28 * Author(s): Scott Shawcroft 29 30 Implementation Notes 31 -------------------- 32 33 **Hardware:** 34 35 * Adafruit `MCP9808 High Accuracy I2C Temperature Sensor Breakout 36 <https://www.adafruit.com/products/1782>`_ (Product ID: 1782) 37 38 **Software and Dependencies:** 39 40 * Adafruit CircuitPython firmware (0.8.0+) for the ESP8622 and M0-based boards: 41 https://github.com/adafruit/circuitpython/releases 42 * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice 43 44 **Notes:** 45 46 #. Datasheet: http://www.adafruit.com/datasheets/MCP9808.pdf 47 48 """ 49 50 __version__ = "0.0.0-auto.0" 51 __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_MCP9808.git" 52 53 from adafruit_bus_device.i2c_device import I2CDevice 54 55 # Resolution settings 56 HALF_C = 0x0 57 QUARTER_C = 0x1 58 EIGHTH_C = 0x2 59 SIXTEENTH_C = 0x3 60 61 62 class MCP9808: 63 """Interface to the MCP9808 temperature sensor.""" 64 65 # alert_lower_temperature_bound 66 # alert_upper_temperature_bound 67 # critical_temperature 68 # temperature 69 # temperature_resolution 70 71 def __init__(self, i2c_bus, address=0x18): 72 self.i2c_device = I2CDevice(i2c_bus, address) 73 74 # Verify the manufacturer and device ids to ensure we are talking to 75 # what we expect. 76 self.buf = bytearray(3) 77 self.buf[0] = 0x06 78 with self.i2c_device as i2c: 79 i2c.write_then_readinto(self.buf, self.buf, out_end=1, in_start=1) 80 81 ok = self.buf[2] == 0x54 and self.buf[1] == 0 82 83 # Check device id. 84 self.buf[0] = 0x07 85 with self.i2c_device as i2c: 86 i2c.write_then_readinto(self.buf, self.buf, out_end=1, in_start=1) 87 88 if not ok or self.buf[1] != 0x04: 89 raise ValueError( 90 "Unable to find MCP9808 at i2c address " + str(hex(address)) 91 ) 92 93 @property 94 def temperature(self): 95 """Temperature in celsius. Read-only.""" 96 self.buf[0] = 0x05 97 with self.i2c_device as i2c: 98 i2c.write_then_readinto(self.buf, self.buf, out_end=1, in_start=1) 99 100 # Clear flags from the value 101 self.buf[1] = self.buf[1] & 0x1F 102 if self.buf[1] & 0x10 == 0x10: 103 self.buf[1] = self.buf[1] & 0x0F 104 return (self.buf[1] * 16 + self.buf[2] / 16.0) - 256 105 return self.buf[1] * 16 + self.buf[2] / 16.0