# MCP9808 Temperature Sensor Python Library

## Overview

Warning: 

![](https://cdn-learn.adafruit.com/assets/assets/000/019/778/medium800/temperature_MCP9808.jpg?1411086246)

Do you need accurate&nbsp;temperature measurements in a project? &nbsp;Consider using the [MCP9808 precision temperature sensor](https://www.adafruit.com/product/1782)&nbsp;with up to 0.25 degrees Celsius accuracy! &nbsp;The MCP9808 will read temperature and make it available over an I2C&nbsp;connection. &nbsp;With the MCP9808 Python library you can now use the MCP9808 precision 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 MCP9808&nbsp;sensor by reading its [Arduino guide](../../../adafruit-mcp9808-precision-i2c-temperature-sensor-guide/overview).

# MCP9808 Temperature Sensor Python Library

## Hardware

Warning: 

Wiring the MCP9808&nbsp;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 MCP9808&nbsp;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/770/medium800/temperature_pi-mcp9808_bb.png?1411071416)

- Connect **Pi GND** to **MCP9808 Gnd**.
- Connect **Pi 3.3V** to **MCP9808 Vdd**.
- Connect **Pi SCL** to **MCP9808 SCL**.
- Connect **Pi SDA** to **MCP9808 SDA**.

# BeagleBone Black

Connect the MCP9808&nbsp;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/771/medium800/temperature_bbb-mcp9808_bb.png?1411071433)

- Connect **BeagleBone Black P9\_1 DGND** to **MCP9808 Gnd**.
- Connect **BeagleBone Black P9\_3 3.3V** to **MCP9808 Vdd**.
- Connect **BeagleBone Black P9\_19 I2C SCL** to **MCP9808 SCL**.
- Connect **BeagleBone Black P9\_20 I2C SDA** to **MCP9808 SDA**.

# MCP9808 Temperature Sensor Python Library

## Software

Warning: 

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

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

You'll also want to be familiar with connecting to a [Raspberry Pi](../../../../adafruits-raspberry-pi-lesson-6-using-ssh)&nbsp;or&nbsp;[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 MCP9808&nbsp;Python library&nbsp;to your home directory and install it by executing:

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

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

# Usage

To learn how to use the MCP9808 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&nbsp; **simpletest.py** script&nbsp;by executing:

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

If everything goes well you should see the sensor's temperature displayed every second:

```auto
Press Ctrl-C to quit.
Temperature: 23.750*C / 74.750*F
Temperature: 25.688*C / 78.237*F
Temperature: 27.000*C / 80.600*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_MCP9808.MCP9808 as MCP9808
```

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

```auto
# Default constructor will use the default I2C address (0x18) 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 = MCP9808.MCP9808()

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

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

Notice that if nothing is passed in to the initializer function the library will pick a default I2C device address (0x18) 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 MCP9808 initializer.

```auto
# Initialize communication with the sensor.
sensor.begin()
```

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

```auto
# Loop printing measurements every second.
print 'Press Ctrl-C to quit.'
while True:
	temp = sensor.readTempC()
	print 'Temperature: {0:0.3F}*C / {1:0.3F}*F'.format(temp, c_to_f(temp))
	time.sleep(1.0)
```

Finally the example enters a loop where it reads the sensor's temperature and prints it&nbsp;out every second. &nbsp;The important thing to see is&nbsp;the temperature measurement function&nbsp;**readTempC()**. &nbsp;The **readTempC()** function will read the sensor&nbsp;temperature and return its value in Celsius.

That's all there is to use the MCP9808&nbsp;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_MCP9808).


## Featured Products

### MCP9808 High Accuracy I2C Temperature Sensor Breakout Board

[MCP9808 High Accuracy I2C Temperature Sensor Breakout Board](https://www.adafruit.com/product/1782)
This I2C digital temperature sensor is one of the more accurate/precise we've ever seen, with a typical accuracy of ±0.25°C over the sensor's -40°C to +125°C range and precision of +0.0625°C. They work great with any microcontroller using standard i2c. There are...

In Stock
[Buy Now](https://www.adafruit.com/product/1782)
[Related Guides to the Product](https://learn.adafruit.com/products/1782/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

- [Speech Synthesis on the Raspberry Pi](https://learn.adafruit.com/speech-synthesis-on-the-raspberry-pi.md)
- [Kombucha Thermostat with CircuitPython and Feather ](https://learn.adafruit.com/kombucha-thermostat-with-circuitpython-and-feather.md)
- [Monitor PiCam and temperature on a PiTFT via adafruit.io](https://learn.adafruit.com/monitor-picam-and-temperature-on-a-pitft-via-adafruit-dot-io.md)
- [MicroPython Hardware: I2C Devices](https://learn.adafruit.com/micropython-hardware-i2c-devices.md)
- [CircuitPython I2C and SPI Under the Hood](https://learn.adafruit.com/circuitpython-basics-i2c-and-spi.md)
- [Adafruit MCP9808 Precision I2C Temperature Sensor Guide](https://learn.adafruit.com/adafruit-mcp9808-precision-i2c-temperature-sensor-guide.md)
- [Adafruit FT232H With SPI & I2C Devices](https://learn.adafruit.com/adafruit-ft232h-with-spi-and-i2c-libraries.md)
- [Wearable Continuous Temperature Monitor with Adafruit IO](https://learn.adafruit.com/wearable-temperature-monitor.md)
- [I2C Addresses and Troublesome Chips](https://learn.adafruit.com/i2c-addresses.md)
- [Wood Case for Raspberry Pi 3](https://learn.adafruit.com/wood-case-for-raspberry-pi-3.md)
- [3.5" PiTFT OctoPrint Rig](https://learn.adafruit.com/3-dot-5-pitft-octoprint-rig.md)
- [Sensor-Locked Secrets with CircuitPython](https://learn.adafruit.com/sensor-locked-secrets-with-circuitpython.md)
- [Adafruit's Raspberry Pi Lesson 9. Controlling a DC Motor](https://learn.adafruit.com/adafruit-raspberry-pi-lesson-9-controlling-a-dc-motor.md)
- [Getting Started With Windows IoT Core on Raspberry Pi](https://learn.adafruit.com/getting-started-with-windows-iot-on-raspberry-pi.md)
- [Adafruit WebIDE](https://learn.adafruit.com/webide.md)
