# 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:

```
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:

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

## BeagleBone Black

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

```
sudo pip install Adafruit_BBIO
```

# Library Install

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

```
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:

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

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

```
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.

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

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

```
# 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.

```
# 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.

```
# 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).


## Featured Products

### Contact-less Infrared Thermopile Sensor Breakout - TMP006

[Contact-less Infrared Thermopile Sensor Breakout - TMP006](https://www.adafruit.com/product/1296)
Unlike all the other temperature sensors we have, this breakout has a really cool IR sensor from TI that can measure the temperature of an object without touching it! Simply point the sensor towards what you want to measure and it will detect the temperature by absorbing IR waves emitted. The...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1296)
[Related Guides to the Product](https://learn.adafruit.com/products/1296/guides)
### Raspberry Pi Model B+ 512MB RAM

[Raspberry Pi Model B+ 512MB RAM](https://www.adafruit.com/product/1914)
OMG OMG OMG, did you hear? There's a Raspberry Pi&nbsp;called the Model B+ and check it out...more USB ports, more GPIO, better power supply, four mounting holes, less sticky-out SD card! Yep, that's right, the fantastic engineers at Raspberry Pi HQ have blessed us with a new design....

In Stock
[Buy Now](https://www.adafruit.com/product/1914)
[Related Guides to the Product](https://learn.adafruit.com/products/1914/guides)
### Raspberry Pi Model B 512MB RAM

[Raspberry Pi Model B 512MB RAM](https://www.adafruit.com/product/998)
Adafruit ships the **Raspberry Pi Model B 512MB RAM** as of 10/18/2012.  
  
The Raspberry Pi® is a single-board computer developed in the UK by the Raspberry Pi Foundation with the intention of stimulating the teaching of basic computer science in schools. The Raspberry...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/998)
[Related Guides to the Product](https://learn.adafruit.com/products/998/guides)
### BeagleBone Black Rev C - 4GB Flash - Pre-installed Debian

[BeagleBone Black Rev C - 4GB Flash - Pre-installed Debian](https://www.adafruit.com/product/1876)
Note: As of May 12, 2014 Adafruit is shipping Rev C. We have discontinued selling Rev B. There are no exchanges or "upgrades" for Rev B to Rev C.  
  
If you liked the BeagleBone Black Rev B, you will love the Rev C! The Rev C still has a blistering 1GHz processor and 512MB...

No Longer Stocked
[Buy Now](https://www.adafruit.com/product/1876)
[Related Guides to the Product](https://learn.adafruit.com/products/1876/guides)

## Related Guides

- [Running OpenGL-based Games & Emulators on Adafruit PiTFT Displays](https://learn.adafruit.com/running-opengl-based-games-and-emulators-on-adafruit-pitft-displays.md)
- [Using Melexis MLX90614 Non-Contact Sensors](https://learn.adafruit.com/using-melexis-mlx90614-non-contact-sensors.md)
- [Using DS18B20 Temperature Sensor with CircuitPython](https://learn.adafruit.com/using-ds18b20-temperature-sensor-with-circuitpython.md)
- [Raspberry Pi Pygame UI basics](https://learn.adafruit.com/raspberry-pi-pygame-ui-basics.md)
- [Controlling Motors using the Raspberry Pi and RasPiRobot Board V2](https://learn.adafruit.com/controlling-motors-using-the-raspberry-pi-and-raspirobot-board-v2.md)
- [Introducing the Raspberry Pi 2 - Model B](https://learn.adafruit.com/introducing-the-raspberry-pi-2-model-b.md)
- [Bone Box](https://learn.adafruit.com/bone-box.md)
- [Using the BMP085/180 with Raspberry Pi or Beaglebone Black](https://learn.adafruit.com/using-the-bmp085-with-raspberry-pi.md)
- [SSD1306 OLED Displays with Raspberry Pi and BeagleBone Black](https://learn.adafruit.com/ssd1306-oled-displays-with-raspberry-pi-and-beaglebone-black.md)
- [Analog Inputs for Raspberry Pi Using the MCP3008](https://learn.adafruit.com/reading-a-analog-in-and-controlling-audio-volume-with-the-raspberry-pi.md)
- [Adafruit MLX90640 IR Thermal Camera](https://learn.adafruit.com/adafruit-mlx90640-ir-thermal-camera.md)
- [Adafruit 2.4" PiTFT HAT with Resistive Touchscreen Mini Kit](https://learn.adafruit.com/adafruit-2-4-pitft-hat-with-resistive-touchscreen-mini-kit.md)
- [Adding a Real Time Clock to BeagleBone Black](https://learn.adafruit.com/adding-a-real-time-clock-to-beaglebone-black.md)
- [Adafruit ADXL343 + ADT7410 Sensor FeatherWing](https://learn.adafruit.com/adxl343-adt7410-sensor-featherwing.md)
- [NeoPixels on Raspberry Pi](https://learn.adafruit.com/neopixels-on-raspberry-pi.md)
