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

Commit 19a9d9e

Browse files
committed
Initial commit.
0 parents  commit 19a9d9e

File tree

8 files changed

+535
-0
lines changed

8 files changed

+535
-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_MCP9808/MCP9808.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
# Default I2C address for device.
27+
MCP9808_I2CADDR_DEFAULT = 0x18
28+
29+
# Register addresses.
30+
MCP9808_REG_CONFIG = 0x01
31+
MCP9808_REG_UPPER_TEMP = 0x02
32+
MCP9808_REG_LOWER_TEMP = 0x03
33+
MCP9808_REG_CRIT_TEMP = 0x04
34+
MCP9808_REG_AMBIENT_TEMP = 0x05
35+
MCP9808_REG_MANUF_ID = 0x06
36+
MCP9808_REG_DEVICE_ID = 0x07
37+
38+
# Configuration register values.
39+
MCP9808_REG_CONFIG_SHUTDOWN = 0x0100
40+
MCP9808_REG_CONFIG_CRITLOCKED = 0x0080
41+
MCP9808_REG_CONFIG_WINLOCKED = 0x0040
42+
MCP9808_REG_CONFIG_INTCLR = 0x0020
43+
MCP9808_REG_CONFIG_ALERTSTAT = 0x0010
44+
MCP9808_REG_CONFIG_ALERTCTRL = 0x0008
45+
MCP9808_REG_CONFIG_ALERTSEL = 0x0002
46+
MCP9808_REG_CONFIG_ALERTPOL = 0x0002
47+
MCP9808_REG_CONFIG_ALERTMODE = 0x0001
48+
49+
50+
class MCP9808(object):
51+
"""Class to represent an Adafruit MCP9808 precision temperature measurement
52+
board.
53+
"""
54+
55+
def __init__(self, address=MCP9808_I2CADDR_DEFAULT, busnum=I2C.get_default_bus()):
56+
"""Initialize MCP9808 device on the specified I2C address and bus number.
57+
Address defaults to 0x18 and bus number defaults to the appropriate bus
58+
for the hardware.
59+
"""
60+
self._logger = logging.getLogger('Adafruit_MCP9808.MCP9808')
61+
self._device = I2C.Device(address, busnum)
62+
63+
def begin(self):
64+
"""Start taking temperature measurements. Returns True if the device is
65+
intialized, False otherwise.
66+
"""
67+
# Check manufacturer and device ID match expected values.
68+
mid = self._device.readU16BE(MCP9808_REG_MANUF_ID)
69+
did = self._device.readU16BE(MCP9808_REG_DEVICE_ID)
70+
self._logger.debug('Read manufacturer ID: {0:04X}'.format(mid))
71+
self._logger.debug('Read device ID: {0:04X}'.format(did))
72+
return mid == 0x0054 and did == 0x0400
73+
74+
def readTempC(self):
75+
"""Read sensor and return its value in degrees celsius."""
76+
# Read temperature register value.
77+
t = self._device.readU16BE(MCP9808_REG_AMBIENT_TEMP)
78+
self._logger.debug('Raw ambient temp register value: 0x{0:04X}'.format(t & 0xFFFF))
79+
# Scale and convert to signed value.
80+
temp = (t & 0x0FFF) / 16.0
81+
if t & 0x1000:
82+
temp -= 256.0
83+
return temp

Adafruit_MCP9808/__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 MCP9808
2+
=======================
3+
4+
Python library for accessing the MCP9808 precision temperature sensor on a Raspberry Pi or Beaglebone Black.
5+
6+
Designed specifically to work with the Adafruit MCP9808 sensor ----> https://www.adafruit.com/product/1782
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

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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_MCP9808.MCP9808 as MCP9808
30+
31+
32+
# Default constructor will use the default I2C address (0x18) 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 = MCP9808.MCP9808()
41+
42+
# Optionally you can override the address and/or bus number:
43+
#sensor = MCP9808.MCP9808(address=0x20, busnum=2)
44+
45+
# Initialize communication with the sensor.
46+
sensor.begin()
47+
48+
# Loop printing measurements every second.
49+
print 'Press Ctrl-C to quit.'
50+
while True:
51+
print 'Temperature: {0:0.4F} *C'.format(sensor.readTempC())
52+
time.sleep(1.0)

0 commit comments

Comments
 (0)