Bus Pirate: MCP9800 temperature sensor

From DP

Jump to: navigation , search

Contents

MCP9800/1/2/3 Temperature Sensor

The MCP9800 is an inexpensive, high-accuracy, 2-wire (I2C) temperature sensor. It features selectable 9/10/11/12 bit resolution with a typical accuracy of ±0.5°c at 25°c, programmable alarms, and low power consumption. It is available in SOT-23-5, MSOP-8, and SOIC-8 packages.


Overview


Setup

Mcp9800pinout.png

For the purposes of this demo we will be using the 8-pin MCP9801; it is functionally equivalent to the 5-pin MCP9800 except for the addition of user selectable I2C address pins. To ensure reliable operation do not leave the address pins A0-2 floating.

Bus Pirate MCP9800 (pin)
MOSI/SDA SDA (1)
CLOCK/SCL SCLK (2)
+3.3v, Vpullup +3.3 (8)
GND GND (4)

Pull-up resistors are required, the Bus Pirate's on-board pull-ups are used. Ensure the Vpullup pin is connected to +v.


HiZ>m << Enter mode selection
1. HiZ
2. 1-WIRE
3. UART
4. I2C
5. SPI
6. 2WIRE
7. 3WIRE
8. LCD
9. DIO
x. exit(without change)

(1)>4 << Select I2C
Set speed:
1. ~5KHz
2. ~50KHz
3. ~100KHz
4. ~400KHz

(1)>3 << Select 100KHz
Ready
I2C>W << Enable PSU
Power supplies ON
I2C>P << Enable pull-ups
Pull-up resistors ON

Interfacing

To find the I2C address we used the Bus Pirate's I2C address scan macro (1). The addresses can also be found in the datasheet on page page 9, table 3-2.


I2C>(1) << Address scan macro
Searching I2C address space. Found devices at:
0x90(0x48 W) 0x91(0x48 R)

The MCP9800 has sensible defaults and you can read the temperature without any changes to the configuration. We will however first set the ADC to sample at 12-bit resolution.

To set the ADC to 12-bit resolution we need to write to the CONFIG register at address 0b00000001.

Mcp9800config.png


I2C>[0x90 0x01 0x60 << Write address, register pointer, data to write
I2C START BIT
WRITE: 0x90 ACK << MCP9801 write address
WRITE: 0x01 ACK << Set write pointer to CONFIG register
WRITE: 0x60 ACK << Value to write

We can now check our work by reading back the contents of the CONFIG register. You do not need to set the register pointer if it has been set from a previous read or write.


I2C>[0x91 r << Read address and read command
I2C START BIT
WRITE: 0x91 ACK << MCP9801 read address
READ: 0x60 << Value we wrote

We will now read the temperature from the ambient temperature register, TA, at address 0b00000000.


I2C>[0x90 0x00 << Write address, register pointer
NACK
I2C START BIT
WRITE: 0x90 ACK << MCP9801 write address
WRITE: 0x00 ACK << Set write pointer to TA register
I2C>[0x91 rr << Read address, two read commands
I2C START BIT
WRITE: 0x91 ACK << MCP9801 read address
READ: 0x1B << Temperature MSB
READ: ACK 0xA0 << Temperature LSB

The first byte of the temperature data is the MSB and the second byte is the LSB; the LSB does not need to be read if your application does not require it. You can use the Bus Pirate = command to convert the hexidecimal.


I2C>=0x1b
0x1B = 27 = 0b00011011

As you can see the sensor has returned our temperature of 27°c.

Calculating Ambient Temperature

The temperature data is formatted in two's compliment, if you are working with negative temperatures the 7th bit of the MSB will indicate the temperature's sign (see register 5-2 on datasheet page 16).

Mcp9800equation.png

Using the equation on page 16, equation 5-2, of the datasheet we can calculate the ambient temperature.

Our 12-bit temperature result is 0b000110111010 which is decimal 442. Using the equation we can find the ambient temperature:

442 × 2^-4 = 27.625°c

Note: if you are using 9, 10, or 11 bit resolutions substitute -1, -2, or -3 in the equation respectively.