|
| 1 | +# Copyright (c) 2014 Adafruit Industries |
| 2 | +# Author: Tony DiCola |
| 3 | +# |
| 4 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +# of this software and associated documentation files (the "Software"), to deal |
| 6 | +# in the Software without restriction, including without limitation the rights |
| 7 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +# copies of the Software, and to permit persons to whom the Software is |
| 9 | +# furnished to do so, subject to the following conditions: |
| 10 | +# |
| 11 | +# The above copyright notice and this permission notice shall be included in |
| 12 | +# all copies or substantial portions of the Software. |
| 13 | +# |
| 14 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +# THE SOFTWARE. |
| 21 | +import logging |
| 22 | +import math |
| 23 | + |
| 24 | +import Adafruit_GPIO.I2C as I2C |
| 25 | + |
| 26 | + |
| 27 | +# Coefficient values, found from this whitepaper: |
| 28 | +# http://www.ti.com/lit/ug/sbou107/sbou107.pdf |
| 29 | +TMP006_B0 = -0.0000294 |
| 30 | +TMP006_B1 = -0.00000057 |
| 31 | +TMP006_B2 = 0.00000000463 |
| 32 | +TMP006_C2 = 13.4 |
| 33 | +TMP006_TREF = 298.15 |
| 34 | +TMP006_A2 = -0.00001678 |
| 35 | +TMP006_A1 = 0.00175 |
| 36 | +TMP006_S0 = 6.4 # * 10^-14 |
| 37 | + |
| 38 | +# Default device I2C address. |
| 39 | +TMP006_I2CADDR = 0x40 |
| 40 | + |
| 41 | +# Register addresses. |
| 42 | +TMP006_CONFIG = 0x02 |
| 43 | +TMP006_MANID = 0xFE |
| 44 | +TMP006_DEVID = 0xFF |
| 45 | +TMP006_VOBJ = 0x0 |
| 46 | +TMP006_TAMB = 0x01 |
| 47 | + |
| 48 | +# Config register values. |
| 49 | +TMP006_CFG_RESET = 0x8000 |
| 50 | +TMP006_CFG_MODEON = 0x7000 |
| 51 | +CFG_1SAMPLE = 0x0000 |
| 52 | +CFG_2SAMPLE = 0x0200 |
| 53 | +CFG_4SAMPLE = 0x0400 |
| 54 | +CFG_8SAMPLE = 0x0600 |
| 55 | +CFG_16SAMPLE = 0x0800 |
| 56 | +TMP006_CFG_DRDYEN = 0x0100 |
| 57 | +TMP006_CFG_DRDY = 0x0080 |
| 58 | + |
| 59 | + |
| 60 | +class TMP006(object): |
| 61 | + """Class to represent an Adafruit TMP006 non-contact temperature measurement |
| 62 | + board. |
| 63 | + """ |
| 64 | + |
| 65 | + def __init__(self, address=TMP006_I2CADDR, busnum=I2C.get_default_bus()): |
| 66 | + """Initialize TMP006 device on the specified I2C address and bus number. |
| 67 | + Address defaults to 0x40 and bus number defaults to the appropriate bus |
| 68 | + for the hardware. |
| 69 | + """ |
| 70 | + self._logger = logging.getLogger('Adafruit_TMP.TMP006') |
| 71 | + self._device = I2C.Device(address, busnum) |
| 72 | + |
| 73 | + def begin(self, samplerate=CFG_16SAMPLE): |
| 74 | + """Start taking temperature measurements. Samplerate can be one of |
| 75 | + TMP006_CFG_1SAMPLE, TMP006_CFG_2SAMPLE, TMP006_CFG_4SAMPLE, |
| 76 | + TMP006_CFG_8SAMPLE, or TMP006_CFG_16SAMPLE. The default is 16 samples |
| 77 | + for the highest resolution. Returns True if the device is intialized, |
| 78 | + False otherwise. |
| 79 | + """ |
| 80 | + if samplerate not in (CFG_1SAMPLE, CFG_2SAMPLE, CFG_4SAMPLE, CFG_8SAMPLE, |
| 81 | + CFG_16SAMPLE): |
| 82 | + raise ValueError('Unexpected samplerate value! Must be one of: ' \ |
| 83 | + 'CFG_1SAMPLE, CFG_2SAMPLE, CFG_4SAMPLE, CFG_8SAMPLE, or CFG_16SAMPLE') |
| 84 | + self._logger.debug('Using samplerate value: {0:04X}'.format(samplerate)) |
| 85 | + # Set configuration register to turn on chip, enable data ready output, |
| 86 | + # and start sampling at the specified rate. |
| 87 | + self._device.write16(TMP006_CONFIG, TMP006_CFG_MODEON | TMP006_CFG_DRDYEN | samplerate) |
| 88 | + # Check manufacturer and device ID match expected values. |
| 89 | + mid = self._device.readU16BE(TMP006_MANID) |
| 90 | + did = self._device.readU16BE(TMP006_DEVID) |
| 91 | + self._logger.debug('Read manufacturer ID: {0:04X}'.format(mid)) |
| 92 | + self._logger.debug('Read device ID: {0:04X}'.format(did)) |
| 93 | + return mid == 0x5449 and did == 0x0067 |
| 94 | + |
| 95 | + def sleep(self): |
| 96 | + """Put TMP006 into low power sleep mode. No measurement data will be |
| 97 | + updated while in sleep mode. |
| 98 | + """ |
| 99 | + control = self._device.readU16BE(TMP006_CONFIG) |
| 100 | + control &= ~(TMP006_CFG_MODEON) |
| 101 | + self._device.write16(TMP006_CONFIG, control) |
| 102 | + self._logger.debug('TMP006 entered sleep mode.') |
| 103 | + |
| 104 | + def wake(self): |
| 105 | + """Wake up TMP006 from low power sleep mode.""" |
| 106 | + control = self._device.readU16BE(TMP006_CONFIG) |
| 107 | + control |= TMP006_CFG_MODEON |
| 108 | + self._device.write16(TMP006_CONFIG, control) |
| 109 | + self._logger.debug('TMP006 woke from sleep mode.') |
| 110 | + |
| 111 | + def readRawVoltage(self): |
| 112 | + """Read raw voltage from TMP006 sensor. Meant to be used in the |
| 113 | + calculation of temperature values. |
| 114 | + """ |
| 115 | + raw = self._device.readS16BE(TMP006_VOBJ) |
| 116 | + self._logger.debug('Raw voltage: 0x{0:04X} ({1:0.4F} uV)'.format(raw & 0xFFFF, |
| 117 | + raw * 156.25 / 1000.0)) |
| 118 | + return raw |
| 119 | + |
| 120 | + def readRawDieTemperature(self): |
| 121 | + """Read raw die temperature from TMP006 sensor. Meant to be used in the |
| 122 | + calculation of temperature values. |
| 123 | + """ |
| 124 | + raw = self._device.readS16BE(TMP006_TAMB) |
| 125 | + self._logger.debug('Raw temperature: 0x{0:04X} ({1:0.4F} *C)'.format(raw & 0xFFFF, |
| 126 | + raw / 4.0 * 0.03125)) |
| 127 | + return raw >> 2 |
| 128 | + |
| 129 | + def readDieTempC(self): |
| 130 | + """Read sensor die temperature and return its value in degrees celsius.""" |
| 131 | + Tdie = self.readRawDieTemperature() |
| 132 | + return Tdie * 0.03125 |
| 133 | + |
| 134 | + def readObjTempC(self): |
| 135 | + """Read sensor object temperature (i.e. temperature of item in front of |
| 136 | + the sensor) and return its value in degrees celsius.""" |
| 137 | + # Read raw values and scale them to required units. |
| 138 | + Tdie = self.readRawDieTemperature() |
| 139 | + Vobj = self.readRawVoltage() |
| 140 | + Vobj *= 156.25 # 156.25 nV per bit |
| 141 | + self._logger.debug('Vobj = {0:0.4} nV'.format(Vobj)) |
| 142 | + Vobj /= 1000000000.0 # Convert nV to volts |
| 143 | + Tdie *= 0.03125 # Convert to celsius |
| 144 | + Tdie += 273.14 # Convert to kelvin |
| 145 | + self._logger.debug('Tdie = {0:0.4} K'.format(Tdie)) |
| 146 | + # Compute object temperature following equations from: |
| 147 | + # http://www.ti.com/lit/ug/sbou107/sbou107.pdf |
| 148 | + Tdie_ref = Tdie - TMP006_TREF |
| 149 | + S = 1.0 + TMP006_A1*Tdie_ref + TMP006_A2*math.pow(Tdie_ref, 2.0) |
| 150 | + S *= TMP006_S0 |
| 151 | + S /= 10000000.0 |
| 152 | + S /= 10000000.0 |
| 153 | + Vos = TMP006_B0 + TMP006_B1*Tdie_ref + TMP006_B2*math.pow(Tdie_ref, 2.0) |
| 154 | + fVobj = (Vobj - Vos) + TMP006_C2*math.pow((Vobj - Vos), 2.0) |
| 155 | + Tobj = math.sqrt(math.sqrt(math.pow(Tdie, 4.0) + (fVobj/S))) |
| 156 | + return Tobj - 273.15 |
0 commit comments