Raspberry Pi - TCN75A Temperature Sensor Java Tutorial
by Dcube Tech Ventures in Circuits > Electronics
1142 Views, 2 Favorites, 0 Comments
Raspberry Pi - TCN75A Temperature Sensor Java Tutorial
TCN75A is a two-wire serial temperature sensor incorporated with temperature-to-digital converter. It is incorporated with user programmable registers that provide flexibility for temperature-sensing applications. The register settings allow users to configure power saving mode, shutdown mode, one shot mode etc.The sensor has an i2c compatible serial interface which can facilitate connection of upto eight devices in a single serial bus. Here is its demonstration with raspberry pi using java code.
What You Need..!!
Connections:
Take an 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 TCN75A 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:
The java code for TCN75A can be downloaded from our github repository- DCUBE Store.
Here is the link for the same :
https://github.com/DcubeTechVentures/TCN75A/blob/master/Java/TCN75A.java
We have used pi4j library for java code, the steps to install pi4j on raspberry pi is described here:
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.
// TCN75A
// This code is designed to work with the TCN75A_I2CS I2C Mini Module
import com.pi4j.io.i2c.I2CBus;
import com.pi4j.io.i2c.I2CDevice;
import com.pi4j.io.i2c.I2CFactory;import java.io.IOException;
public class TCN75A
{
public static void main(String args[]) throws Exception
{
// Create I2C bus
I2CBus Bus = I2CFactory.getInstance(I2CBus.BUS_1);
// Get I2C device, TCN75A I2C address is 0x48(72)
I2CDevice device = Bus.getDevice(0x48);
// Select configuration register, 12-bit ADC resolution
device.write(0x01, (byte) 0x60);
Thread.sleep(500);
//Read 2 bytes of data
// 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 : %.2f C %n", cTemp);
System.out.printf("Temperature in Fahrenheit : %.2f F %n", fTemp);
}
}
Applications:
TCN75A is a temperature sensor that can be employed in personal computers and servers.It can also be deployed in entertainment systems,office equipments, hars disk drives and other PC peripherals.This sensor also find its application in data communication equipment.