MCP9808 is a highly accurate digital temperature sensor ±0.5°C I2C mini module. They are embodied with user- programmable registers that facilitate temperature sensing applications. The MCP9808 high-accuracy temperature sensor has become an industry standard in terms of form factor and intelligence, providing calibrated, linearized sensor signals in digital, I2C format.
In this tutorial the interfacing of the MCP9808 sensor module with particle photon has been demonstrated. To read the temperature values, we have used raspberry pi with an I2c adapter. This I2C adapter makes the connection to the sensor module easy and more reliable.
Step 1: Hardware RequiredThe materials that we need for accomplishing our goal includes the following hardware components:
1. MCP9808
3. I2C Cable
4. I2C Shield for particle photon
Step 2: Hardware HookupThe hardware hookup section basically explains the wiring connections required between the sensor and the Particle Photon. Ensuring correct connections is the basic necessity while working on any system for the desired output. So, the requisite connections are as follows:
- The MCP9808 will work over I2C. Here is the example wiring diagram, demonstrating how to wire up each interface of the sensor.
- Out-of-the-box, the board is configured for an I2C interface, as such we recommend using this hookup if you’re otherwise agnostic. All you need is four wires!
Only four connections are required Vcc, Gnd, SCL and SDA pins and these are connected with the help of I2C cable.
These connections are demonstrated in the pictures above.
Step 3: Code for temperature measurementLets start with the Particle code now.
While using the sensor module with the Arduino, we include application.h
and spark_wiring_i2c.h
library. "application.h
" and spark_wiring_i2c.h
library contains the functions which facilitate the I2C communication between the sensor and the Particle.
The entire Particle code is given below for the convenience of the user:
#include
#include
// MCP9808 I2C address is 0x18(24)
#define Addr 0x18
float cTemp = 0, fTemp = 0;
void setup()
{
// Set variable
Particle.variable("i2cdevice", "MCP9808");
Particle.variable("cTemp", cTemp);
// 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();
delay(300);
}
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();
}
delay(300);
// Convert the data to 13-bits
int temp = ((data[0] & 0x1F) * 256 + data[1]);
if(temp > 4095)
{
temp -= 8192;
}
cTemp = temp * 0.0625;
fTemp = cTemp * 1.8 + 32;
// Output data to dashboard
Particle.publish("Temperature in Celsius : ", String(cTemp));
Particle.publish("Temperature in Fahrenheit : ", String(fTemp));
delay(500);
}
Particle.variable()
function creates the variables to store the output of the sensor and Particle.publish()
function displays the output on the dashboard of the site.
The sensor output is shown in the picture above for your reference.
Step 4: ApplicationsMCP9808 Digital Temperature Sensor has several industry level applications which incorporate industrial freezers and refrigerators along with various food processors. This sensor can be employed for various personal computers, servers as well as other PC peripherals.
Comments
Please log in or sign up to comment.