In this short tutorial I'm going to show to you how to implement , in the Arduino UNO environment, the basic functionalities to work with the TCN75.
Materials needed for this tutorial.
- 1 Arduino UNO board (naturally)
- Arduino IDE (naturally)
- 1 TCN75 (naturally I'll use my own board, see related posts)
- 4 jumper cables
Our tcn75 board is described here:
- Working with the RTCC MCP 79410 RTCC (Real Time Clock with Calendar)
- How to use the i2c bus with PIC12f1840 (a simple library)
- Temperature sensor and real time clock for Arduino
- A PIC 12f1840 developer board with mcp79410 RTC and tcn75 temperature sensor.
Ok let's start
Some theory
The TCN75 works trough the i2c bus, so what is the i2c bus?
I2C is an acronym for “Inter-Integrated Circuit”. In the late 1970s, Philips’ semiconductor division (now NXP) saw the need for simplifying and standardising the data lines that travel between various integrated circuits in their products. Their solution was the I2C bus. This reduced the number of wires to two (SDA – data, and SCL – clock). Here is a nice introductory video from NXP
From a hardware perspective, the wiring is very easy. Those of you with an Arduino Duemilanove, Uno or 100% compatible board, you will be using pins A4 for SDA (data) and A5 for SCL (clock).
If you are using an Arduino Mega, SDA is pin 20 and SCL is 21, so note that shields with I2C need to be specifically for the Mega. If you have another type of board, check your data sheet or try the Arduino team’s hardware website. And finally, if you are using a bare DIP ATmega328-PU microcontroller, you will use pins 27 for SDA and 28 for SCL.
Connecting Arduino to the TCN75 board
In the following table you'll find all the connections between Arduino and the TCN75 board
Arduino | TCN75 |
5V | V+ |
GND | V- |
A4 | SD |
A5 | SC |
That's all, now you only need to connect the USB plug to Arduino and to your PC. You should see the red led on the TCN75 lightned
Programming
First of all we need a function to convert temperature (TCN75 format) to Celsius (float).
float tcn75_convertTemp(byte aa, byte bb);
This function returns the temperature value expressed in Celsius starting from the two bytes representation of the TCN75.
To use the i2c bus Arduino needs to initialize the Wire library, so in the setup function we will put this initialization:
Wire.begin();
And to debug our code we will put also the following initialization:
Serial.begin(115200)
Then we need to read the temperature:
tcn75_getTemperature
In pseudo code the tcn75_getTemperature will look like the following
tcn75_getTemperature()
{
setTheRegisterPointerToZero()
requestTwoBytesToTheTCN()
readTwoBytesFromThei2cBus()
convertTwoBytesInTemperature()
}
Here you are a simple working sample.
#include "Wire.h" #define tcn75address 0x4F // with pins 5~7 set to GND, the device address is 0x48 void setup() { Wire.begin(); // wake up I2C bus Serial.begin(115200); } float tcn75_getTemperature(int address) { byte a, b; float temp=.0; Wire.beginTransmission(address); // Start transmission Wire.write((byte)0); // move your register pointer back to 00h Wire.endTransmission(); // Stop transmission Wire.requestFrom(address, 2); // Send me the contents of your first two bytes a = Wire.read(); // first received byte stored here b = Wire.read(); // second received byte stored here temp=tcn75_convertTemp(a, b); // convert received bytes into float value return(temp); } float tcn75_convertTemp(byte aa, byte bb) { float temp; if (aa>127) // check for below zero degrees { temp=((aa-128)*-1); if (bb==128) // check for 0.5 fraction { temp-=0.5; } } else // it must be above zero degrees { temp=aa; if (bb==128) // check for 0.5 fraction { temp+=0.5; } } return(temp); } void loop() { Serial.print("Current temperature = "); Serial.println(tcn75_getTemperature(tcn75address)); delay(2000); }
The TCN75 provides a lot of other complex functions such as a setpoint, alarm, goto the links at the beginning of the article to see all its functionalities
Gg1
Have you posted firmware for this project somewhere. Sorry if I have ovelooked the URL to it. Thanks.
To use the TCN75 with an Arduino board you don’t need any firmware, you have to connect the TCN75 with your arduino board…. nothing more