hi guys
i`m trying to control the fan speed with a temperature sensor, display the temperature and fan speed on dedicated LCD.
i have wired up every thing correctly.
managed to display temp on LCD as well as serial monitor.
managed to switch the fan on and off.
but when it comes to control speed bit in programming i get stuck and the fan doesnt move....
here is my code.
#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;
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
}
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.0; // 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 < 40 && tempc > 10 ) {
fanspeed = tempc + 50;
analogWrite(3,fanspeed);
}
else if ( tempc < 10 ) {
fanspeed = 0;
analogWrite(3,fanspeed);
}
else{
analogWrite(3,255);
}
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
}
please help this is really wasting my time......... :0 :0 :0 :0 :0 :0 :0
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.
PaulS:
Why are you storing the temperatures in an array, during the averaging process? The array is completely unnecessary.
if ( tempc < 40 && tempc > 10 ) {
This is hard to read.
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.
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.
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.
#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 ????
digitalWrite() turns the pin HIGH or LOW, one time. analogWrite() starts the pin toggling on and off, so that it's on amount is the ratio specified.
Ditch the complicated code. Write a sketch that calls analogWrite() with various values, from 0 to 200, with a 2 second delay between changes. If the fan speed does not change, the fan is not meant to be variable speed.
int fanstart = 0; // start fan speed at 0
int fadeAmount = 20; // how many points to fade the LED by
void setup() {
// declare pin 3 to be an output:
pinMode(3, OUTPUT);
}
void loop() {
// set the speed of pin 3:
analogWrite(3, fanstart);
// change the speed for next time through the loop:
fanstart = fanstart + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (fanstart == 0 || fanstart == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
nothing happens the fan runs on one speed !
Connections are the same
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