Read Temperatures using I2C, TC04 sensors and Arduino


This articles describes how you can use I2C (TWI) communication, Arduino and temperature sensors to measure 3 different temperatures. It can be very helpful when you want to measure the outside, inside or other temperatures in the same time but you don’t want to use a lot of pins of your Arduino board.

I2C protocol makes it easier to connect more ICs using only 2 wires SDA and SCL and allows communication between them at relatively low speed. The I2C bus or TWI (Two Wire Interface) allows a single master IC to share communication lines with multiple slave ICs. In our case Arduino is the master and 3 x TC74 sensors are the slaves.

Schematic of the Arduino and TC74 sensors circuitry

Arduino i2c temperature

As you can see in the schematic all three TC74 sensors have their SDA and SCL pins connected in series to pin A4 and A5 on the Arduino board. The two 4.7K pull-up resistors must be used when using I2C communication. We use the serial monitor to display the data received from the board and one interesting thing you might notice is that the data is already printed in degrees because this is a digital temperature sensor so we won’t have to do any data conversion from analog to digital.

video presentation of the working Arduino projects

Tc74 addresses

Every IC that knows I2C protocol has a unique address as you can see in the table above and you must use that address when you initiate the communication from the master device to the slaves. In our case TC74A0 has binary address of 1001000, TC74A1 1001001 and TC74A2 1001010 which converts into decimal 72, 73 and 74 respectively. We use these values in our sketch as addresses for each sensor.

Arduino I2C Sketch Code

/* source of code:
http://www.electroschematics.com/9798/reading-temperatures-i2c-arduino/ */
#include 
int address1 = 72;  //decimal address of sensor 1
int address2 = 73;  //decimal address of sensor 2
int address3 = 74;  //decimal address of sensor 3

void setup() {
  Serial.begin(9600);
  Wire.begin();     // create a wire object
}

void loop() {
  int c1 = read_temp(address1);
  int c2 = read_temp(address2);
  int c3 = read_temp(address3);  
  Serial.print("Sensor 1: ");
  Serial.print(c1);
  Serial.print("C | ");
  Serial.print("Sensor 2: ");
  Serial.print(c2);
  Serial.print("C | ");
  Serial.print("Sensor 3: ");
  Serial.print(c3);   
  Serial.println("C");
  delay(500);  
}

int read_temp(int address) {
  //start the communication with IC with the address xx
  Wire.beginTransmission(address); 
  //send a bit and ask for register zero
  Wire.write(0);
  //end transmission
  Wire.endTransmission();
  //request 1 byte from address xx
  Wire.requestFrom(address, 1);
  //wait for response
  while(Wire.available() == 0);
  //put the temperature in variable c
  int c = Wire.read();   
  return c;
}

In the code we use function read_temp() that returns an integer, in our case the temperature. Inside the function we start the transmission by using function beginTransmission() from the Wire object which needs the address of our sensor. Next we send one bit 0 that indicates we want to read the value from the register 0. After that we end the transmission by sending a stop bit.

Next steps shows how the master (Arduino) reads from the slave I2C device. Wire.requestFrom(address, 1) command expects to receive 1 byte of data from the TC74 sensor. Wire.available() waits for data to be available on the I2C bus and when the data is received in a 8-bit value it is stored inside integer c with the Wire.read() function. We use the read_temp() function inside the loop() and we get the temperature values for each connected sensor.

This article shows how to use several temperature sensors but you can use any kind of integrated circuit that has I2C serial port interface. The advantage is you’ll use only 2 pins on your microcontroller freeing the others therefore you can use a smaller package microcontroller, for example a 8 pin type.


Sorry, comments are closed!