MCP9802 temperature sensor and Arduino



MCP9802 is a digital temperature sensor from Microchip that measures temperatures between -55°C and +125°C to a digital word. It provides an accuracy of ±1°C (maximum) from -10°C to +85°C. The MCP9802 sensor comes with user-programmable registers that provide flexibility for temperature sensing applications. The register settings also allow user-selectable 9-bit to 12-bit temperature measurement resolution. This sensor has an industry standard 2-wire I2C compatible serial interface, allowing up to eight devices to be controlled in a single serial bus. In this blog post I am going to write about an Arduino sketch to interface the MCP9802 sensor with an Arduino for temperature sensing application. For illustration, I am using the MCP9802 sensor onboard the I2C EEPROM+Sensor breakout board.

Arduino and MCP9802



The hardware connections between MCP9802 and Arduino is straightforward. Two pull-up resistors are required on the I2C signal lines. On Arduino Uno, the I2C lines are multiplexed with analog pins A4 (SDA) and A5 (SCL).

Hardare connections between MCP9802 and Arduino

The following Arduino sketch configures the MCP9802 sensor resolution to 12-bit. The Arduino receives the temperature bytes and sends them out to serial port. The values are received and displayed on the serial monitor window on PC. Note that the 7-bit slave addresses of the
MCP9802 sensor on board is 0×48.

#include <Wire.h>
 
#define address 0x48   //I2C address of MCP9802
#define baudrate 9600  // Baud rate for serial communication 
int TempByte1, TempByte2;
 
void setup()
{
Wire.begin();
Serial.begin(baudrate);
 
// Write to Configuration Register 
Wire.beginTransmission(address);
Wire.write(0x01);  // Address to Configuration register
Wire.write(0x64);  // ADC resolution: 12-bit,
Wire.endTransmission();
}
 
 
void loop()
{
 
// Read temperature 
Wire.beginTransmission(address);
Wire.write((byte)0x00);    // Pointer to temperature register
Wire.endTransmission();
 
Wire.beginTransmission(address);
Wire.requestFrom(address, 2);  // Now read two bytes of temperature data
if (Wire.available()) {
  TempByte1 = Wire.read();
  TempByte2 = Wire.read();
 
  int Temperature = ((TempByte1 << 8) | TempByte2);
  Temperature = Temperature >> 4;
  float TempC = 1.0*Temperature*0.0625;
  float TempF = TempC*1.8+32.0;
  Serial.print(" Temperature= ");
  Serial.print(TempC);
  Serial.print(" C, ");
  Serial.print(TempF);
  Serial.println(" F");
}
else{
  Serial.println("No response ");
}
Wire.endTransmission();
delay(1000);
}

Temperature readings displayed on Serial Monitor terminal window

Leave a Reply

Your email address will not be published. Required fields are marked *