In this example we show how to connect a MCP9808 digital temperature sensor to a MSP-EXP432P401R LaunchPad, the example will use the Energia IDE. I used a MCP9808 module, which you can see below
The MCP9808 digital temperature sensor converts temperatures between -20°C and +100°C to a digital word with ±0.5°C (max.) accuracy. The MCP9808 comes with user-programmable registers that provide flexibility for temperature sensing applications. The registers allow user-selectable settings such as Shutdown or low-power modes and the specification of temperature Event and Critical output boundaries. When the temperature changes beyond the specified boundary limits, the MCP9808 outputs an Event signal.
The user has the option of setting the event output signal polarity as an active-low or active-high comparator output for thermostat operation, or as temperature event interrupt output for microprocessor-based systems. The event output can also be configured as a Critical temperature output.
This sensor has an industry standard 2-wire, SMBus and Standard I2C™Compatible compatible (100kHz/400kHz bus clock) serial interface, allowing up to eight sensors to be controlled in a single serial bus.
More info here – http://www.microchip.com/wwwproducts/en/en556182
Connection
Here is a picture of the launchpad so you can see what pins we are referring to below in the table
| Module Connection | MSP432 Connection |
| SDA | J1-10 SDA |
| SCL | J1-9 SCL |
| Gnd | J3-22 Gnd |
| Vcc | J1-1 3.3v |
Code
[codesyntax lang=”cpp”]
#include<Wire.h>
// MCP9808 I2C address is 0x18(24)
#define Addr 0x18
void setup()
{
// Initialise I2C communication as MASTER
Wire.begin();
// Initialise Serial Communication, set baud rate = 9600
Serial.begin(9600);
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select configuration register
Wire.write(0x01);
// Continuous conversion mode, Power-up default
Wire.write(0x00);
Wire.write(0x00);
// Stop I2C Transmission
Wire.endTransmission();
// Start I2C Transmission
Wire.beginTransmission(Addr);
// Select resolution rgister
Wire.write(0x08);
// Resolution = +0.0625 / C
Wire.write(0x03);
// Stop I2C Transmission
Wire.endTransmission();
}
void loop()
{
unsigned int data[2];
// Starts I2C communication
Wire.beginTransmission(Addr);
// Select data register
Wire.write(0x05);
// Stop I2C transmission
Wire.endTransmission();
// Request 2 bytes of data
Wire.requestFrom(Addr, 2);
// Read 2 bytes of data
// temp MSB, temp LSB
if(Wire.available() == 2)
{
data[0] = Wire.read();
data[1] = Wire.read();
}
// Convert the data to 13-bits
int temp = ((data[0] & 0x1F) * 256 + data[1]);
if(temp > 4095)
{
temp -= 8192;
}
float cTemp = temp * 0.0625;
float fTemp = cTemp * 1.8 + 32;
// Output data to screen
Serial.print("Temperature in Celsius : ");
Serial.print(cTemp);
Serial.println(" C");
Serial.print("Temperature in Fahrenheit : ");
Serial.print(fTemp);
Serial.println(" F");
delay(500);
}
[/codesyntax]
Output
Open the serial monitor and you should see something like this
Temperature in Celsius : 28.25 C
Temperature in Fahrenheit : 82.85 F
Temperature in Celsius : 29.06 C
Temperature in Fahrenheit : 84.31 F
Temperature in Celsius : 29.81 C
Temperature in Fahrenheit : 85.66 F
Temperature in Celsius : 30.19 C
Temperature in Fahrenheit : 86.34 F
Temperature in Celsius : 30.50 C
Temperature in Fahrenheit : 86.90 F
Temperature in Celsius : 30.81 C
Temperature in Fahrenheit : 87.46 F
Links
MCP9808 High Accuracy I2C IIC Temperature Sensor Breakout Board


