In this article we look at the MCP9808 digital temperature sensor and connect it to a MSP430FR4133 LaunchPad. We will have an example using the Energia IDE showing the sensor in action.
First a little bit of information regarding the sensor
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.
Features
Accuracy:
±0.25°C (typical) from -40°C to +125°C
±0.5°C (maximum) from -20°C to +100°C
User Selectable Measurement Resolution:
0.5°C, 0.25°C, 0.125°C, 0.0625°C
User Programmable Temperature Limits:
Temperature Window Limit
Critical Temperature Limit
User Programmable Temperature Alert Output
Operating Voltage Range: 2.7V to 5.5V
More details about the sensor at http://www.microchip.com/wwwproducts/en/MCP9808
This typically comes in a breakout such as the one in the breakout below
Parts Required
Name | Link |
MSP430FR4133 LaunchPad | MSP430FR4133 LaunchPad development board MSP-EXP430FR4133 |
MCP9808 | High Accuracy Temperature Sensor MCP9808 I2C Breakout Board Module |
Connecting wire | Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire |
Schematic/Connection
Here are the connectors on the launchpad
MSP430FR4133 Launchpad | Sensor |
3.3v | Vdd |
Gnd | Gnd |
SDA P8_2 | SDA |
SCL P8_3 | SCL |
Code Example
[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]
Results
Open the serial monitor and you should see something like this
Temperature in Celsius : 23.50 C
Temperature in Fahrenheit : 74.30 F
Temperature in Celsius : 23.50 C
Temperature in Fahrenheit : 74.30 F
Temperature in Celsius : 23.56 C
Temperature in Fahrenheit : 74.41 F
Temperature in Celsius : 23.50 C
Temperature in Fahrenheit : 74.30 F
Temperature in Celsius : 23.50 C
Temperature in Fahrenheit : 74.30 F