Fan controlling

Hello all,
I am trying to use a thermistor to read temperature , and based on readings it runs motor with fan as needed :
i used an example code and i modified to get Arduino read temperature, and instead of just displaying it on LCD, its also control DC motor, i just would like you check the code and give me your opinion whether its correct or not also i am little bit confused about sending the power from Arduino to the motor, can be done like this? while Arduino also send power to the LCD ?

#include <LiquidCrystal.h>
int tempPin = 0;
//                BS  E  D4 D5  D6 D7
LiquidCrystal lcd(7, 8, 9, 10, 11, 12);
void setup()
{
  lcd.begin(16, 2);
}

#define pwm 9

byte degree[8] = 
              {
                0b00011,
                0b00011,
                0b00000,
                0b00000,
                0b00000,
                0b00000,
                0b00000,
                0b00000
              };
void loop()
{
  int tempReading = analogRead(tempPin);
  // This is OK
  double tempK = log(10000.0 * ((1024.0 / tempReading - 1)));
  tempK = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * tempK * tempK )) * tempK );       //  Temp Kelvin
  float tempC = tempK - 273.15;            // Convert Kelvin to Celcius
  float tempF = (tempC * 9.0)/ 5.0 + 32.0; // Convert Celcius to Fahrenheit
  /*  replaced
    float tempVolts = tempReading * 5.0 / 1024.0;
    float tempC = (tempVolts - 0.5) * 10.0;
    float tempF = tempC * 9.0 / 5.0 + 32.0;
  */
  // Display Temperature in C
  lcd.setCursor(0, 0);
  lcd.print("Temp         C  ");
  // Display Temperature in F
  //lcd.print("Temp         F  ");
  lcd.setCursor(6, 0);
  // Display Temperature in C
  lcd.print(tempC);
  // Display Temperature in F
  //lcd.print(tempF);
  delay(500);
if(tempC <26 )
    { 
      analogWrite(9,0);
      lcd.print("Fan OFF            ");
      delay(100);
    }
    
    else if(tempC==26)
    {
      analogWrite(pwm, 51);
      lcd.print("Fan Speed: 20%   ");
      delay(100);
    }
    
     else if(tempC==27)
    {
      analogWrite(pwm, 102);
      lcd.print("Fan Speed: 40%   ");
      delay(100);
    }
    
     else if(tempC==28)
    {
      analogWrite(pwm, 153);
      lcd.print("Fan Speed: 60%   ");
      delay(100);
    }
    
    else if(tempC==29)
    {
      analogWrite(pwm, 204);
      lcd.print("Fan Speed: 80%    ");
      delay(100);
    }
     else if(tempC>29)
    {
      analogWrite(pwm, 255);
      lcd.print("Fan Speed: 100%   ");
      delay(100);
    } 
  delay(3000);
}

It’s unlikely the temp will == those exact temperatures… better to set thresholds than test exact values.

Also, invert your if-else tree, so the highest temperature is tested first, then step down through the values, otherwise the pwm value will be twitching continuously.

Even better, map the temperature band across the range, and just have one analogwrite(), that outputs a continuous proportional control value in the relevant range.

I would do what lastchancename suggested. Invert the order and use the < instead of the == because what happens at 27.33 degrees? Nothing? You don't want that!

Maybe it can look something like this:

if(tempC>29)
    {
      analogWrite(pwm, 255);
      lcd.print("Fan Speed: 100%   ");
      delay(100);
    } 
else if (tempC>27)
 {
      analogWrite(pwm, 153);
      lcd.print("Fan Speed: 60%   ");
      delay(100);
    }
else if (tempC>26)
 {
      analogWrite(pwm, 102);
      lcd.print("Fan Speed: 40%   ");
      delay(100);
    }
// etc.....

If I wanted to perfect it more, having the PWM range from 0-255, I would control the fan proportionally with temperature. But I guess that's overkill.

Look at the map() function, you can create a PWM value to use from the tempC directly, like this:

PWMtoUse = map(tempC, 25, 29, 0, 255);

anything below 25 can be truncated to 0% pwm, and 29 and above will max at 100%.
I would use integers though instead of floating numbers for the map(). That's all you need for the PWM and display%. You can convert them to integers but still have the original math for the temperatures as floating point numbers.
For the display, use another map function to convert that same 25-29 temperature range to a 0-100 value to show the fan %.

Thank you all for your replies, i will give it a try and revert, but i would like to confirm plugging the DC motor, is it going to get power from the same arduino ? i mean its already powering the LCD, also what should i place before the engine to avoid Arduino damage ? as i know connecting motor could produce power and damage arduino (Acting as a Dynamo)
Thanks.

I don't know how large or small that fan is, but unless it was extremely small I would drive it with a circuit to prevent problems. A quick internet search gave me this example:
Reef6ff6d95e23f2ab7b3da058d27fca0
Where it says +12V, it can be any voltage that the fan can use but don't get it from the arduino's 5v pin.
Where it shows a BC337 transistor, almost any transistor can work as long as the motor doesn't draw more than 100 mA or more. Heavier currents will require finding a transistor that can conduct more current without burning itself out.
The resistors shown are also non-critical. Any value around 10,000 will work for the resistor tied to ground, and the other one can range from about 100 to 600.
If this circuit or selecting components scare you, put the fan specs on here and you will have 100 volunteers willing to recommend driving circuits.
I would draw one up that will work, but I usually like to design around the components I can easily obtain, so let's see those fan motor specs!

Thank you so much,
I just would like to ask a silly question, and excuse me for this as i am totally new, when the motor get its power from outsourced power, how it can receive the control from Arduino ?
the fan motor just got two polls
i know signal sent through ```
analogWrite() function
so how the engine will receive orders?

If you look at the circuit in post #5, the Arduino controls the transistor.
The transistor controls the current going to the fan.

So a small controlling current from the Arduino going through the base, controls the larger fan current running through the Emitter - Collector of the transistor.

Tom... :grinning: :+1: :coffee: :australia:

Yes, what TomGeorge said is true. The awesome usefulness of a transistor is that a small current can control a larger current. Similarly, with a tube or FET a small voltage can control a larger current.

Think of it like you jumping into and pressing the gas pedal of a 40-ton gravel hauler in a gravel mine. Obviously the energy of your foot doesn't move 40 tons of rock. The gas powers the movement, but the truck amplifies your small motion of your foot and moves those rocks much more than you could do without the truck.

That same thing happens when a very weak radio signal is received by a powerful home receiver. That tiny voltage fluctuation radiating through space is amplified and some 100-Watt speakers can thump you good in the chest.

Amplification baby! Your tiny Arduino signal can be hooked up to control a 36" fan if we wanted to!