Raspberry Pi MCP9808 Temperature Sensor Java Tutorial

by Dcube Tech Ventures in Circuits > Electronics

19 Views, 0 Favorites, 0 Comments

Raspberry Pi MCP9808 Temperature Sensor Java Tutorial

Raspberry Pi MCP9808 Temperature Sensor Java Tutorial

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, linearised sensor signals in digital, I2C format. Here is the demonstration with a java code using Raspberry Pi.

What You Need..!!

IMG_2733.JPG
pi.jpg

Connections

IMG_2733.JPG
pi.jpg
sensor.jpg
complete_connection.jpg

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 MCP9808 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.

Code

complete_connection.jpg

The java code for MCP9808 can be downloaded from our GitHub repository- Dcube store.

Here is the link for the same :

https://github.com/DcubeTechVentures/MCP9808

The datasheet of MCP9808 can be found here:

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

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.

// MCP9808

// This code is designed to work with the MCP9808_I2CS I2C Mini Module available in Dcube Store.

import com.pi4j.io.i2c.I2CBus;

import com.pi4j.io.i2c.I2CDevice;

import com.pi4j.io.i2c.I2CFactory;

import java.io.IOException;

public class MCP9808

{

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

{

// Create I2C bus

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

// Get I2C device, MCP9808 I2C address is 0x18(24)

I2CDevice device = Bus.getDevice(0x18);

Thread.sleep(300);

// Select configuration register

// Continuous conversion mode, Power-up default

byte[] config = new byte[2];

config[0] = 0x00;

config[1] = 0x00;

device.write(0x01, config, 0, 2);

// Select resolution rgister

// Resolution = +0.0625 / C

device.write(0x08, (byte)0x03);

Thread.sleep(300);

// Read 2 bytes of data from address 0x05(05)

// temp msb, temp lsb

byte[] data = new byte[2];

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

// Convert the data to 13-bits

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

if(temp > 4095)

{

temp -= 8192;

}

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);

}

}

Applications..:

MCP9808 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.