Introduction: Raspberry Pi MCP9803 Temperature Sensor Java Tutorial

MCP9803 is a 2-wire high accuracy temperature sensor. They are embodied with user- programmable registers that facilitate temperature sensing applications. This sensor is suited for highly sophisticated multi-zone temperature monitoring system. Here is the demonstration with a java code using Raspberry Pi.

Step 1: What You Need..!!

Picture of What You Need..!!

1. Raspberry Pi

2. MCP9803

LINK : https://www.controleverything.com/content/Temperature?sku=MCP9803_I2CS

3. I²C Cable

LINK : https://www.controleverything.com/content/I2C-Cables?sku=I2CAFF3

4. I²C Shield for Raspberry Pi

LINK : https://www.controleverything.com/content/I2C-Master?sku=TOUTPI2

5. Ethernet Cable

Step 2: Connections

Picture of Connections

Take a I2C shield for raspberry pi and gently push it over the gpio pins of raspberry pi.

Then connect the one end of I2C cable to MCP9803 sensor and the other end to the I2C shield.

Also connect the Ethernet cable to the pi or you can use a WiFi module. Connections are shown in the picture above.

Step 3: Code

Picture of Code

The java code for MCP9803 can be downloaded from our github repository- ControlEverythingCommunity

Here is the link for the same :

https://github.com/ControlEverythingCommunity/MCP9...

The datasheet of MCP9803 can be found here:

http://ww1.microchip.com/downloads/en/DeviceDoc/21...

We have used pi4j library for java code, the steps to install pi4j on raspberry pi is described here:

http://pi4j.com/install.html

You can also copy the code from here, it is given as follows:

// Distributed with a free-will license.

// Use it any way you want, profit or free, provided it fits in the licenses of its associated works.

// MCP9803

// This code is designed to work with the MCP9808_I2CS I2C Mini Module available from ControlEverything.com.

// https://www.controleverything.com/content/Tempera...
import com.pi4j.io.i2c.I2CBus;

import com.pi4j.io.i2c.I2CDevice;

import com.pi4j.io.i2c.I2CFactory;

import java.io.IOException;

public class MCP9803

{

public static void main(String args[]) throws Exception

{

// Create I2C bus

I2CBus Bus = I2CFactory.getInstance(I2CBus.BUS_1);

// Get I2C device, MCP9803 I2C address is 0x48(72)

I2CDevice device = Bus.getDevice(0x48);

// Select configuration register

// Continuous conversion mode, Power-up, comparator mode, 12-bit resolution

device.write(0x01, (byte)0x60);

Thread.sleep(500);

// Read 2 bytes of data from address 0x00(0)

// temp msb, temp lsb

byte[] data = new byte[2];

device.read(0x00, data, 0, 2);

// Convert the data to 12-bits

int temp = ((data[0] & 0xFF) * 256 + (data[1] & 0xF0)) / 16;

if(temp > 2047)

{

temp -= 4096;

}

double cTemp = temp * 0.0625;

double fTemp = cTemp * 1.8 + 32;

// Output data to screen

System.out.printf("Temperature in Celsius is : %.2f C %n", cTemp);

System.out.printf("Temperature in Fahrenheit is : %.2f F %n", fTemp);

}

}

Step 4: Applications..:

MCP9803 can be employed in a wide arena of devices which include personal computer and peripherals, hard disk drives, various entertainment systems, office systems and data communication systems. This sensor can be incorporated in various sophisticated systems.

Comments