Temperature tracking fan is on problem

Hi guys.

I’ve got some problem to do this

with MLX90614 and dc motor, I make some cooling fan depends on notebook’s temperature.

This is my code

#include <i2cmaster.h>

const int p1 = 11;
const int p2 = 12;
const int p3 = 8;

void setup(){

pinMode(p1, OUTPUT);

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;


// read
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 (measurement resolution of the MLX90614)
double tempData = 0x0000; // zero out the data
int frac; // data past the decimal point

// 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;

float celcius = tempData - 273.15;
float fahrenheit = (celcius*1.8) + 32;

Serial.print("Celcius: ");

Serial.print("Fahrenheit: ");

if (celcius >= 30) {
// turn LED on:
analogWrite(p1, 50);
else if (celsius >= 37){
// turn LED off:
analogWrite(p1, 255);
else {
analogWrite(p1, 0);

if (celsius >= 30) {
// turn LED on:
digitalWrite(p1, HIGH);
digitalWrite(p2, HIGH);
else {
digitalWrite(p1, LOW);
digitalWrite(p2, LOW);


I try to make a fan speed change in 3 steps.

However whatever temperature that I put in, it still spin same speed…

How can I makes this fan spins in 3 steps?

sorry for the bad english


 if (celcius >= 30) {
    // turn LED on:
    analogWrite(p1, 50);
  else if (celsius >= 37){
    // turn LED off:
    analogWrite(p1, 255)

Think about this for a minute. If the temperature is greater than 37 then it is also greater than 30. So the first if statement is true and it runs that code. The second is an else if, so since it ran the first one it skips the second one. If you'll think about this for a minute, there is an easy way to arrange the tests so that it will work. Since you're testing for greater than, you'll have to go in decending order.

if (27 <= celsius < 29) {
analogWrite(p1, 50);
else if (29 <= celsius < 33) {
analogWrite(p1, 150);
else if (33 <= celsius < 38) {
analogWrite(p1, 250);
else {
analogWrite(p1, 0);

I changed this way.

still doesn’t work…

Because you can’t do two comparisons at once like that. It works in math class but it’s different on a computer.

if(27 <= celcius && celcius < 29)

But you really don’t even need the double comparisons at all if you do it right.

If temp is greater than 37 turn fan on high. Else if temp is greater than 33 turn fan on medium. Else if fan greater than 28 turn fan on low. See if the first one is true the others don’t even get considered because they’re else clauses.

Hi, Welcome to the forum.

Please read the first post in any forum entitled how to use this forum. http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code. It will be formatted in a scrolling window that makes it easier to read.

Thanks .. Tom... :)


Have you been able to use a bit of code to just control the speed of your fan?

Tom... :)