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

<p>I wonder if this could be adapted into a solar tracker.</p>
<p>Cool!</p><p>you could build a little motor/current driver to power the servo from the arduino.</p>
Nice work, Now weaponize it :)
<p>good idea...a water gun would be good for hot summer days</p>
<p>well, this device can actually save people lives! adding an array of those PIR sensors and automating a water cannon, can extinct blazes! a semi-automatic water cannon for small firefighter departments can be awesome!</p>
<p>place it out in dry areas where fires are likely, have it water down area's that are getting to hot. Or if something really bad happens, notify the fire department.</p>
Use a solid state relay to power the fan from arduino.
<p>+1 To that idea</p>
Thanks. Also, I have gotten 3 from eBay cheaply.
<p>They do come in handy. I used my relay to power some Christmas lights and make the flicker to music.</p>
<p>Very cool...I'm working on a similar problem. Have you thought about combining it with a motion sensor?</p>
Very cool!
nice one brilliant (applause) :)
<p>Can i use a DS1624 IC chip instead of an IR sensor?</p><p>and great work btw!!</p>
This is a brilliant idea, you could make many monies from this! :D
I am starting a personal site just for my projects but this caught my attention. <br> <br>A newbie to Arduino, I want to build a table fan that follows me as I move in the room. Seems that my fan is always pointing the wrong place. On oscillate it is right only twice in a cycle ! <br> <br>See what I have planned. www.paulckruger.com
#include &lt;i2cmaster.h&gt;<br> <br>
Ckever idea, and well done!

About This Instructable


281 favorites


More by ePums: Heat-Seeking Desk Fan (using Arduino)
Add instructable to: