Go Down

Topic: Temperature Fan Control PWM Arduino Help! (Read 5 times) previous topic - next topic

Uni_man


Why are you storing the temperatures in an array, during the averaging process? The array is completely unnecessary.

Code: [Select]
if ( tempc < 40 && tempc > 10 ) {
This is hard to read.
Code: [Select]
if(tempc > 10 && tempc < 40)
{

is much closer to how people decide to do things. The compiler doesn't care, but it's easier to maintain understandable code.

What, exactly, is connected to pin 3? What kind of fan are you using?

thank you
the base of the TIP-122 darlington transistor with 1k resistor driving 12v fan.

dxw00d



You are Serial.printing the value of tempc. Do the results look right?

Add some debugging prints to the fan speed if statements to see which parts are being executed.

yes temperature measurements are right, thanks you


What about debugging prints to trace the path through the code?

Uni_man


tempc = tempc/8.0;               // better precision

You are doing floating point maths (using 8.0) on an integer variable.  I haven't checked but I reckon this may be your issue.

Try defining tempc as a float & see if it works any better.
Better might be:

tempc=tempc>>3;

or for closer rounding:

tempc=((tempc+4)>>3)


Also, why not put a delay at then end of the setup function just to check that the fan runs OK at full speed before moving into the loop.  If it doesn't then it sounds like a hardware issue.

thanks for the hint but temperature statements works fine and i have changed the value 8.0 to 8 this is just dividing the 8 samples by 8 its just to get better measurements.

Uni_man

Quote

What about debugging prints to trace the path through the code?

im about to do it now and see where the problem is because the fan is not moving at all but when i set it high it switch on so its just fan statements and the serial.print is the best hint i didnt even think of it.

Uni_man

i still can not get it  here is the updated code;
Code: [Select]
#include <LiquidCrystal.h>

int pin = 0;                // analog pin
int tempc = 0,tempf=0;      // temperature variables
int samples[8];             // variables to make a better precision
int maxi = -100,mini = 100; // to start max/min temperature
int i;
int TIP122pin = 3;
int fanspeed = 0;
int fanStemp = 0;

LiquidCrystal lcd(7, 8, 9, 10, 11, 12); // Customize for your connections.

void setup()
{
  Serial.begin(9600);       // start serial communication
  lcd.begin(16,2);          // set up the LCD's number of columns and rows
  pinMode(TIP122pin, OUTPUT); // Set pin for output to control TIP122 Base pin
  analogWrite(TIP122pin, 255); // control the fan speed by changing values from 0 to 255
  delay(200);
}

void loop()
{
 
tempc = 0;

for(i = 0;i<=7;i++){         // gets 8 samples of temperature
  samples[i] = ( 5.0 * analogRead(pin) * 100.0 ) / 1024.0;
  tempc = tempc + samples[i];
  delay(1000);
}


tempc = tempc/8;               // better precision
tempf = (tempc * 9)/ 5 + 32;     // converts to fahrenheit
if(tempc > maxi) {maxi = tempc;} // set max temperature
if(tempc < mini) {mini = tempc;} // set min temperature

if ( tempc > 10 && tempc < 40 ) {
    fanStemp = tempc + 50;
    fanspeed = fanStemp + 100;
    analogWrite(3,fanspeed);
    Serial.print("Fan started at temp+50+100 speed\n");
    Serial.print(fanspeed,DEC);
    Serial.print("\n");
    delay(100);
  }
    else if ( tempc < 10 ) {
    fanspeed = 0;
    analogWrite(3,fanspeed);
    Serial.print("Fan off\n");
    delay(100);
   }
   else{
     analogWrite(3,255);
     Serial.print("Fan started at full speed\n");
     delay(100);
   }
   
Serial.print(tempc,DEC); // serial print for debugging all serial.print commands
Serial.print(" Celsius, ");
Serial.print(tempf,DEC);
Serial.print(" fahrenheit -> ");
Serial.print(maxi,DEC);
Serial.print(" Max, ");
Serial.print(mini,DEC);
Serial.println(" Min");

lcd.setCursor(0, 0);  // set the cursor to (0,0)
lcd.print(tempc,DEC);  // print temp in C and F on the first row of the lcd
lcd.print(" C, ");
lcd.print(tempf,DEC);
lcd.print(" F");

lcd.setCursor(0, 1);  // set the cursor to (16,1)
lcd.print(maxi,DEC);   // Print max temp and min temp on the second row of lcd
lcd.print("  Max, ");
lcd.print(mini,DEC);
lcd.println(" Min.");

delay(1000);                   // delay before loop
}

i got the fan working and running but on 5v but i can not see the speed change am i using the right function [ analogWrite(3, fanspeed) ] why not [ digitalWrite(3,fanspeed) ] what is the difference i know the voltage may be one of the problems but still at least i should see a change when i change the added value of 100 in the fanspeed to 0 !!!  probable the connection ?? iam connecting Arduino Pin 3 (PWM) with TIP-122 Base (B) pin through 1k resistor.5V fan connect to the TIP-122 Collector (C). And the Emitter (E) pin connect to the Ground. i dont understand the statements are executed the way i want it on the serial print but ????

Go Up