Skip to content
This repository was archived by the owner on Dec 20, 2018. It is now read-only.

Commit dabf482

Browse files
committed
Initial commit.
0 parents  commit dabf482

File tree

8 files changed

+615
-0
lines changed

8 files changed

+615
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build/
2+
dist/
3+
*.egg-info
4+
*.pyc

Adafruit_TMP/TMP006.py

+156
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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

Adafruit_TMP/__init__.py

Whitespace-only changes.

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2014 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 all
13+
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 THE
21+
SOFTWARE.

README.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Adafruit Python TMP006
2+
======================
3+
4+
Python library for accessing the TMP006 non-contact temperature sensor on a Raspberry Pi or Beaglebone Black.
5+
6+
Designed specifically to work with the Adafruit TMP006 sensor ----> https://www.adafruit.com/products/1296
7+
8+
To install, first make sure some dependencies are available by running the following commands (on a Raspbian
9+
or Beaglebone Black Debian install):
10+
11+
````
12+
sudo apt-get update
13+
sudo apt-get install build-essential python-dev python-smbus
14+
````
15+
16+
Then download the library by clicking the download zip link to the right and unzip the archive somewhere on your Raspberry Pi or Beaglebone Black. Then execute the following command in the directory of the library:
17+
18+
````
19+
sudo python setup.py install
20+
````
21+
22+
Make sure you have internet access on the device so it can download the required dependencies.
23+
24+
See examples of usage in the examples folder.
25+
26+
Adafruit invests time and resources providing this open source code, please support Adafruit and open-source hardware by purchasing products from Adafruit!
27+
28+
Written by Tony DiCola for Adafruit Industries.
29+
MIT license, all text above must be included in any redistribution

examples/simpletest.py

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/python
2+
# Copyright (c) 2014 Adafruit Industries
3+
# Author: Tony DiCola
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+
# Can enable debug output by uncommenting:
24+
#import logging
25+
#logging.basicConfig(level=logging.DEBUG)
26+
27+
import time
28+
29+
import Adafruit_TMP.TMP006 as TMP006
30+
31+
32+
# Default constructor will use the default I2C address (0x40) and pick a default I2C bus.
33+
#
34+
# For the Raspberry Pi this means you should hook up to the only exposed I2C bus
35+
# from the main GPIO header and the library will figure out the bus number based
36+
# on the Pi's revision.
37+
#
38+
# For the Beaglebone Black the library will assume bus 1 by default, which is
39+
# exposed with SCL = P9_19 and SDA = P9_20.
40+
sensor = TMP006.TMP006()
41+
42+
# Optionally you can override the address and/or bus number:
43+
#sensor = TMP006.TMP006(address=0x42, busnum=2)
44+
45+
# Initialize communication with the sensor, using the default 16 samples per conversion.
46+
# This is the best accuracy but a little slower at reacting to changes.
47+
sensor.begin()
48+
49+
# Optionally initialize with a faster but less precise sample rate. You can use
50+
# any value from TMP006_CFG_1SAMPLE, TMP006_CFG_2SAMPLE, TMP006_CFG_4SAMPLE,
51+
# TMP006_CFG_8SAMPLE, or TMP006_CFG_16SAMPLE for the sample rate.
52+
#sensor.begin(samplerate=TMP006.CFG_1SAMPLE)
53+
54+
# Loop printing measurements every second.
55+
print 'Press Ctrl-C to quit.'
56+
while True:
57+
print 'Object temperature: {0:0.4F} *C'.format(sensor.readObjTempC())
58+
print ' Die temperature: {0:0.4F} *C'.format(sensor.readDieTempC())
59+
time.sleep(1.0)

0 commit comments

Comments
 (0)