Temperature Controlled Fan with i2c LED

Hello, I am a beginner creating an Arduino Project where I use temperature sensors to control the speed of a fan and the temperature of the sensor is displayed in an i2c LCD.

However, the reading of my fan seems to go way off and reaches 400C, I have tried many ways to solve this but i do not seem to understand the source of the problem.

Heres the code of my work:

// C++ code
#include <Adafruit_LiquidCrystal.h>
Adafruit_LiquidCrystal lcd_1(0);

float temp;
int tempPin = A5;

#define fan 9
void setup()
{
  
  pinMode(fan, OUTPUT);
  
  lcd_1.begin(16,2);
  
  lcd_1.print("Turning");
  lcd_1.setCursor(2,1);
  lcd_1.print("On....");
  delay(1000);
  lcd_1.clear();
  
  lcd_1.print("Temp. Controlled");
  lcd_1.setCursor(2,2);	
  lcd_1.print("Fan Speed");
  delay(2000);
  lcd_1.clear();
}

void loop() {
  lcd_1.setCursor(0,0);
  lcd_1.print("Recording");
  lcd_1.setCursor(2,1);
  lcd_1.print("Temperature...");
  delay(3000);
  lcd_1.clear();
  lcd_1.setCursor(0,2);
  
 temp = analogRead(tempPin);
 int sensorValue = analogRead (tempPin);
 float voltage = sensorValue *(5.0/1023.0);
 float temp =(voltage - 0.5)* 100.0;
  
  lcd_1.setCursor(0,0);
  lcd_1.print("Temperature = ");
  lcd_1.setCursor(5,1);
  lcd_1.print(temp);
  delay(3000);
  lcd_1.clear();
  
  if (temp<25) 
  {
    analogWrite(fan, 0);
    lcd_1.print("Fan OFF");
    delay(2000);
    lcd_1.clear();
  }
  else if (temp<=28)
  {
    analogWrite(fan, 51);
    lcd_1.print("Fan speed: 20%");
    delay(2000);
    lcd_1.clear(); 
  }
  else if (temp<=31)
  {
    analogWrite(fan, 102);
    lcd_1.print("Fan speed: 40%");
    delay(2000);
    lcd_1.clear();
  }
  else if (temp<=34)
  {
    analogWrite(fan, 153);
    lcd_1.print("Fan speed: 60%");
    delay(2000);
    lcd_1.clear();
  }
  else if (temp<=37)
  {
    analogWrite(fan, 200);
    lcd_1.print("Fan speed: 80%");
    delay(2000);
    lcd_1.clear();
  }
  else
  {
    analogWrite(fan, 255); 
    lcd_1.print("Fan speed: 100%");
    delay(2000);
    lcd_1.clear();
  }
}  

And here is the wiring of my project:

I would really appreciate the help. Thank you.

Sounds like you meant to say i2c LCD?

Yes, Im sorry for the confusion.

Hi, @johncomet
What is the part number of your temperature sensor.

What is your fan?
I hope not just a motor in your Fritzy image.
The output pin will not be able to supply the current needed to operate the fan.

Can you post some images of your project?
So we can see your component layout.

Thanks.. Tom... :smiley: :+1: :coffee: :australia:

Hello, I have only begun making the layout of my project through Tinkercad, however we are planning to make the fan with makeshift materials as we are only needed to create a miniature.

As pointed out, your fan will need far more current and maybe voltage than an Arduino pin can provide.

I suggest you acquire a 4-pin PC cooling fan. You can connect the speed control pin of the fan directly to an Arduino output pin. You can also connect the speed sense pin to an Arduino input and measure the RPM.

1 Like

What voltage do you measure from the temp sensor output?

It appears to be 5 volts.

Im not sure what you mean by part number, but its a TMP36 Sensor.

I see the problem.
You can't use A5 for the temp sensor use A0
A5 and A6 are used by the LCD I2C

Oh my God, it worked! Thank you so much.

It'a a very common mistake. The SCL and SDA pins are actually connected to the A5 and A4 pins. so you can't use A5 and A4 when you have an I2C device.

Hello, so I am making a project which is about a TMP35 Sensor reading the room temperature and then displaying that temperature on the i2c LCD and then depending on that specific temperature, a DC Motor turns on, however theres a problem in my work. The temperature isnt accurate at all. It reaches -18 degrees celsius and sometimes at 400 degrees celsius. I do not know what seems to be causing this problem because it completely works fine in a simulation. Any help would be greatly appreciated. Thank you.

Code for my work:

// C++ code
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C address 0x27, 16 column and 2 rows
  
float temp;
const int tempPin = A0;

#define fan 9
void setup()
{
  

  pinMode(fan, OUTPUT);

  lcd.init(); 
  lcd.backlight();
  
  lcd.begin(16,2);
  
  lcd.print("Turning");
  lcd.setCursor(2,1);
  lcd.print("On....");
  delay(2000);
  lcd.clear();
  
  lcd.print("Temp. Controlled");
  lcd.setCursor(2,2);	
  lcd.print("Fan Speed");
  delay(2000);
  lcd.clear();
}

void loop() {
  lcd.setCursor(0,0);
  lcd.print("Recording");
  lcd.setCursor(2,1);
  lcd.print("Temperature...");
  delay(2000);
  lcd.clear();
  lcd.setCursor(0,2);
  
 temp = analogRead(tempPin);
 int sensorValue = analogRead (tempPin);
 float voltage = sensorValue *(5.0/1023.0);
 float temp =(voltage - 0.5)* 100.0;
  
  lcd.setCursor(0,0);
  lcd.print("Temperature = ");
  lcd.setCursor(5,1);
  lcd.print(temp);
  delay(2000);
  lcd.clear();
  
  if (temp<23) 
  {
    analogWrite(fan, 0);
    lcd.print("Fan OFF");
    delay(1000);
    lcd.clear();
  }
  else if (temp<=26)
  {
    analogWrite(fan, 51);
    lcd.print("Fan speed: 20%");
    delay(1000);
    lcd.clear(); 
  }
  else if (temp<=30)
  {
    analogWrite(fan, 102);
    lcd.print("Fan speed: 40%");
    delay(1000);
    lcd.clear();
  }
  else if (temp<=34)
  {
    analogWrite(fan, 153);
    lcd.print("Fan speed: 60%");
    delay(1000);
    lcd.clear();
  }
  else if (temp<=37)
  {
    analogWrite(fan, 200);
    lcd.print("Fan speed: 80%");
    delay(1000);
    lcd.clear();
  }
  else
  {
    analogWrite(fan, 255); 
    lcd.print("Fan speed: 100%");
    delay(1000);
    lcd.clear();
  }
}  
  

Simulation of my Work:

Actual Testing of my Work: (its a bit messy, ill fix it up when i figure out the problem)


Thank you.

  • Always show us a good schematic of your proposed circuit.

  • There is no kickback diode across your motor.

  • A 9v PP3 battery is a poor choice to power circuitry like this.

  • What is the transistor part number ?

1 Like

If it works perfect in sim, but not in the real world (IRL) that points to a problem with your wiring.

Please post a complete schematic.

A smoke alarm battery will not run a motor long, if at all.

You are missing the flyback diode to protect the mystery MOSFET.

Is the mystery MOSFET of the logic level variety?

3 Likes

I see. Im still a beginner at arduino as this is my first actual project.

What is a good alternative to the battery?

Is the diode necessary to the MOSFET? Also I hope the original problem would get answered as well. Thank you.

Here is a more complete DC motor driver.

image

The diode is to shunt the high voltage (hundreds of volts, at least) generated when the MOSFET turns off and the motor magnetic field collapses. Without the diode, the MOSFET can be, quickly, destroyed.

Still don't know the MOSFET part number. If it is not logic level you will have a hard time with consistent operation.

The optional cap will bypass motor brush noise and help to keep the power clean.

Clean up some of the things already pointed out. Should see some improvement.

1 Like

I see, thats very helpful. I'll put these components you suggested into my project. Thank you so much for that. However, my main problem is that the tmp35 sensor doesnt read the temperature accurately. Do you think that it needs to be replaced or theres just something wrong with my wiring? Thank you.

Also the MOSFET Number is IRFZ44N.

How do you know? What is your standard?

That looks like an intermittent. Check all connections. Replace jumpers.

1 Like

Its supposed to read the surrounding room temperature which is just right about 20-30 degrees celsius, but it reaches abnormal numbers.

I had a similar problem before however which reached the same abnormal numbers but that was only because I had set it up at the wrong analog pin. This one is set at A0.