# TMP006 Temperature Sensor Python Library

## Overview

Danger: 

![](https://cdn-learn.adafruit.com/assets/assets/000/019/776/medium800/temperature_TMP006.jpg?1411086125)

Are you looking for an easy way to measure the temperature of something without having to attach a sensor directly to it? &nbsp;Consider using a non-contact temperature sensor like the [TMP006](https://www.adafruit.com/products/1296)! &nbsp;This sensor reads the infra-red radiation, or heat, emitted from an object and can be easily read over an I2C connection. &nbsp;With the [TMP006 Python library](https://github.com/adafruit/Adafruit_Python_TMP)&nbsp;you can now use the TMP006 non-contact temperature sensor with your Raspberry Pi or BeagleBone Black project!

Before you get started make sure your Raspberry Pi is running the latest [Raspbian](http://www.raspberrypi.org/downloads/)&nbsp;or [Occidentalis](../../../adafruit-raspberry-pi-educational-linux-distro/occidentalis-v0-dot-2)&nbsp;operating system, and your BeagleBone Black is running the latest [official Debian operating system](http://beagleboard.org/latest-images). &nbsp;It will also help to familiarize yourself with the TMP006 sensor by reading its [Arduino guide](../../../infrared-thermopile-sensor-breakout/overview).

# TMP006 Temperature Sensor Python Library

## Hardware

Danger: 

Wiring the TMP006 to a Raspberry Pi or BeagleBone Black is easy because the board only&nbsp;uses an&nbsp;I2C bus for communication.

# Raspberry Pi

Connect the TMP006 to a Raspberry Pi as follows. &nbsp;Note that you must have [I2C enabled on your Raspberry Pi](../../../../adafruits-raspberry-pi-lesson-4-gpio-setup/configuring-i2c).

![](https://cdn-learn.adafruit.com/assets/assets/000/019/764/medium800/temperature_pi-tmp006_bb.png?1411071211)

- Connect **Pi 3.3V** power to **TMP006 VCC**.
- Connect **Pi GND** to **TMP006 GND**.
- Connect **Pi SDA** to **TMP006 SDA**.
- Connect **Pi SCL** to **TMP006 SCL**.

# BeagleBone Black

Connect the TMP006 to a BeagleBone Black as follows. &nbsp;If you aren't familiar with how GPIO pins are numbered, check out [this guide on BeagleBone Black GPIO](../../../../setting-up-io-python-library-on-beaglebone-black/using-the-bbio-library).

![](https://cdn-learn.adafruit.com/assets/assets/000/019/765/medium800/temperature_bbb-tmp006_bb.png?1411071229)

- Connect **BeagleBone Black P9\_1 DGND** to **TMP006 GND**.
- Connect **BeagleBone Black P9\_3 3.3V** power to **TMP006 VCC**.
- Connect **BeagleBone Black P9\_19 SCL** to **TMP006 SCL**.
- Connect **BeagleBone Black P9\_20 SDA** to **TMP006 SDA**.

# TMP006 Temperature Sensor Python Library

## Software

Danger: 

To install and use the [TMP006 Python library](https://github.com/adafruit/Adafruit_Python_TMP)&nbsp;follow the steps below.

Before you get started make sure your board is connected to the internet through an ethernet or wireless connection so you can download dependencies.

You'll also want to be familiar with connecting to a [Raspberry Pi](../../../../adafruits-raspberry-pi-lesson-6-using-ssh)&nbsp;or [BeagleBone Black](../../../../ssh-to-beaglebone-black-over-usb/overview)&nbsp;terminal with&nbsp;SSH.

# Dependencies

First install dependencies by executing in a terminal:

```auto
sudo apt-get update
sudo apt-get install build-essential python-dev python-pip python-smbus git
```

You can ignore warnings about dependencies&nbsp;which are already installed.

## Raspberry Pi

On a Raspberry Pi execute the following to make sure the RPi.GPIO library is installed:

```auto
sudo pip install RPi.GPIO
```

## BeagleBone Black

On a BeagleBone Black execute the following to make sure the Adafruit\_BBIO library is installed:

```auto
sudo pip install Adafruit_BBIO
```

# Library Install

Next download the&nbsp;TMP006 Python library&nbsp;to your home directory and install it by executing:

```auto
cd ~
git clone https://github.com/adafruit/Adafruit_Python_TMP.git
cd Adafruit_Python_TMP
sudo python setup.py install
```

That's all you need to do to install the Python library!

# Usage

To learn how to use the TMP006 Python library you can run and review an example program included with the library. &nbsp;To run the example navigate to the **examples** directory and run the **simpletest.py** script&nbsp;by executing:

```auto
cd examples
sudo python simpletest.py
```

If everything goes well you should see the sensor's object and die&nbsp;temperature displayed every second:

```auto
Press Ctrl-C to quit.
Object temperature: 25.368*C / 77.662*F
   Die temperature: 23.094*C / 73.569*F
Object temperature: 25.368*C / 77.662*F
   Die temperature: 23.094*C / 73.569*F
Object temperature: 26.763*C / 80.174*F
   Die temperature: 23.094*C / 73.569*F
...

```

If you see an error make sure you're running the program as root with the sudo command, and that the dependencies & library were&nbsp;installed successfully.

To understand the usage, open **simpletest.py** in a text editor and follow along with the description of the code below.

```auto
import Adafruit_TMP.TMP006 as TMP006
```

First the TMP006 module is imported with a Python import statement.&nbsp;

```auto
# Default constructor will use the default I2C address (0x40) and pick a default I2C bus.
#
# For the Raspberry Pi this means you should hook up to the only exposed I2C bus
# from the main GPIO header and the library will figure out the bus number based
# on the Pi's revision.
#
# For the Beaglebone Black the library will assume bus 1 by default, which is
# exposed with SCL = P9_19 and SDA = P9_20.
sensor = TMP006.TMP006()

# Optionally you can override the address and/or bus number:
#sensor = TMP006.TMP006(address=0x42, busnum=2)
```

Next an instance of the TMP006 class is created. &nbsp;

Notice that if nothing is passed in to the initializer function the library will pick a default I2C device address (0x40) and bus number. &nbsp;If you need to specify the I2C address or bus number you can do so by specifying them as optional parameters in the TMP006 initializer.

```auto
# Initialize communication with the sensor, using the default 16 samples per conversion.
# This is the best accuracy but a little slower at reacting to changes.
sensor.begin()

# Optionally initialize with a faster but less precise sample rate.  You can use
# any value from TMP006_CFG_1SAMPLE, TMP006_CFG_2SAMPLE, TMP006_CFG_4SAMPLE, 
# TMP006_CFG_8SAMPLE, or TMP006_CFG_16SAMPLE for the sample rate.
#sensor.begin(samplerate=TMP006.CFG_1SAMPLE)
```

After creating the TMP006 class instance the **begin()** function is called to initialize communication with the device. &nbsp;

Notice that you can specify an optional **samplerate** parameter which specifies how many samples will be taken to form a temperature reading. &nbsp;The more samples used the more accurate your reading, however there will be a trade-off in the speed of measurements.

```auto
# Loop printing measurements every second.
print 'Press Ctrl-C to quit.'
while True:
	obj_temp = sensor.readObjTempC()
	die_temp = sensor.readDieTempC()
	print 'Object temperature: {0:0.3F}*C / {1:0.3F}*F'.format(obj_temp, c_to_f(obj_temp))
	print '   Die temperature: {0:0.3F}*C / {1:0.3F}*F'.format(die_temp, c_to_f(die_temp))
	time.sleep(1.0)
```

Finally the example enters a loop where it reads&nbsp;temperature measurements and prints them out every second. &nbsp;The important thing to see are the two temperature measurement functions, **readObjTempC()** and **readDieTempC()**. &nbsp;

The **readObjTempC()** function will read the object temperature and return its value in Celsius. &nbsp;Make sure to skim&nbsp;the [TMP006&nbsp;user guide](http://www.ti.com/lit/ug/sbou107/sbou107.pdf)&nbsp;to understand how&nbsp;the TMP006 chip measures object temperature.

The **readDieTempC()** function will read the TMP006 die temperature and return its value in Celsius. &nbsp;Note that this value is the temperature of the chip's die and not the temperature of the object.

That's all there is to use the TMP006 Python library! &nbsp;If you run into issues or wish to contribute, feel free to followup on [the library's Github page](https://github.com/adafruit/Adafruit_Python_TMP).


## Related Guides

- [DIY WiFi Raspberry Pi Touchscreen Camera](https://learn.adafruit.com/diy-wifi-raspberry-pi-touch-cam.md)
- [Embedded Linux Board Comparison](https://learn.adafruit.com/embedded-linux-board-comparison.md)
- [Large Pi-based Thermometer and Clock](https://learn.adafruit.com/large-pi-based-thermometer-and-clock.md)
- [Adafruit BMP280 Barometric Pressure + Temperature Sensor Breakout](https://learn.adafruit.com/adafruit-bmp280-barometric-pressure-plus-temperature-sensor-breakout.md)
- [MPR121 Capacitive Touch Sensor on Raspberry Pi & BeagleBone Black](https://learn.adafruit.com/mpr121-capacitive-touch-sensor-on-raspberry-pi-and-beaglebone-black.md)
- [Adafruit 16 Channel Servo Driver with Raspberry Pi](https://learn.adafruit.com/adafruit-16-channel-servo-driver-with-raspberry-pi.md)
- [5" Display Kippah Portable Raspberry Pi](https://learn.adafruit.com/portable-kippah-pi.md)
- [Raspberry Pi NFC Minecraft Blocks](https://learn.adafruit.com/raspberry-pi-nfc-minecraft-blocks.md)
- [My Mini Race Car](https://learn.adafruit.com/my-mini-race-car.md)
- [Adafruit STEMMA Soil Sensor - I2C Capacitive Moisture Sensor](https://learn.adafruit.com/adafruit-stemma-soil-sensor-i2c-capacitive-moisture-sensor.md)
- [Internet of Things Printer for Raspberry Pi](https://learn.adafruit.com/pi-thermal-printer.md)
- [Adafruit WebIDE](https://learn.adafruit.com/webide.md)
- [DHT Humidity Sensing on Raspberry Pi or Beaglebone Black with GDocs Logging](https://learn.adafruit.com/dht-humidity-sensing-on-raspberry-pi-with-gdocs-logging.md)
- [Bosch BMP085 Breakout Board](https://learn.adafruit.com/bmp085.md)
- [Adafruit IO Connected Animated GIF Display](https://learn.adafruit.com/adafruit-io-connected-animated-gif-display.md)
