Step 4: Communicating with the Temperature Sensor

Summary: The infrared temperature sensor is an I2C component. Normally this just means calling on a few commands from the Arduino "Wire" library to get data, but unfortunately, this temperature sensor requires some particular timing adjustments which are not possible using the Wire library. The code I use involves a custom "i2cmaster" library, which you must import according to the following steps. Do some additional research if you're having trouble finding the libraries folder on your particular OS.

Source: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1214872633

1) Importing the Custom "i2cmaster" Library:

A) Go to:


and download the i2cmaster.zip

Make a folder in /{arduino root}/hardware/libraries and extract the
i2cmaster.h and twimaster.c files. Now rename the .c file of twimaster to .cpp (MAKE SURE TO RENAME AND PUT THESE FILES IN THE RIGHT LOCATION I.E. IN THE LIBRARIES FOLDER OF THE ARDUINO CODE)

Make sure you restart if you load a new library into it so it can be found when it is called.

B) Now you need to modify twimaster.c
Open it in a simple text editor and change the following if you are using an Arduino Uno

Edit the twimaster.c to reflect the 16MHz clock, and change the bus frequency to 50Khz by changing the code at the beginning to:

#ifndef F_CPU
#define F_CPU 16000000UL

/* I2C clock in Hz */
#define SCL_CLOCK 50000L

2) Testing the Temperature Sensor:
Here's some test code which prints to the serial monitor the temperature in Celsius to two-digits. If it works, you've got your sensor hooked up properly and the library properly imported.


void setup()
i2c_init(); //Initialise the i2c bus
PORTC = (1 << PORTC4) | (1 << PORTC5);//enable pullups

void loop()
int dev = 0x5A<<1;
int data_low = 0;
int data_high = 0;
int pec = 0;

data_low = i2c_readAck(); //Read 1 byte and then send ack
data_high = i2c_readAck(); //Read 1 byte and then send ack
pec = i2c_readNak();

//This converts high and low bytes together and processes temperature, MSB is a error bit and is ignored for temps
double tempFactor = 0.02; // 0.02 degrees per LSB
double tempData = 0x0000;
int frac;

// This masks off the error bit of the high byte, then moves it left 8 bits and adds the low byte.
tempData = (double)(((data_high & 0x007F) << 8) + data_low);
tempData = (tempData * tempFactor)-0.01;
tempData = tempData - 273.15;
Serial.print((int)tempData); //Print temp in degrees C to serial

arnefl3 years ago
#include <i2cmaster.h>