Temperature Controlled Fan Speed using DHT11

Hello, I want my fan to respond based on the temperature that it receives from my DHT11 sensor. My 12v dc fan is supposed to be running when it hits 31C (max temp) but apparently it doesn’t. When I deleted the ‘if’ code (in both scripts) my fan is running just fine which mean that my wiring is correct, right? I want the fan to only start running when it reaches the max temp. I don’t really know if you can use the ‘map’ code like I did but I attached my second script as well (by replacing map with analogWrite). Please check my final sketches attached. Reminder: Both scripts failed to meet my needs when I insert the ‘if’ code.

first_script.ino (1.1 KB)

second_script.ino (958 Bytes)

first_script.ino (1.1 KB)

second_script.ino (958 Bytes)

I'm sorry but how do I delete this post?

Thank you and sorry for the trouble.

holdingpattern:
No trouble to me: just that if a thread's in the wrong place it's unlikely to get the responses you expect, since the folk who might be in the best position to answer, might not often read the "wrong" forum. I for example only saw it because I was about to post there, although it's a forum I seldom visit.

Yeah like visiting a fruit stall looking for vegetables. Thank you for the help once again.

Moved as requested.

Depending on the fan you are using then most will need an upper-lower limit as many will not work at the lowest speed.

Determine the minimum PWM needed so start the fan and use that as your lowest limit and zero as OFF.
Calculations for the rest of the fan speeds can be calculated from the lower limit upwards.

it is up to you if you want fully independent speeds or stepped speeds.
Use stepped speeds here on this set up You can see in the sketch where I use a minimum speed called tick.

ballscrewbob:
Moved as requested.

Depending on the fan you are using then most will need an upper-lower limit as many will not work at the lowest speed.

Determine the minimum PWM needed so start the fan and use that as your lowest limit and zero as OFF.
Calculations for the rest of the fan speeds can be calculated from the lower limit upwards.

it is up to you if you want fully independent speeds or stepped speeds.
Use stepped speeds here on this set up You can see in the sketch where I use a minimum speed called tick.

I tried;

if(temp < 29) { // temp is less than 29
     analogWrite(gate, 0);  
  }
  if(temp = 29) {
    analogWrite(gate, 80);
  }

But my fan start spinning right away eventhough it’s not 29C yet.

You need to learn "IF" "ELSE" structure I think.

If I were you I would start this project by writing a simple Analog In and PWM Out script. I would use a logic level MOSFET like the FQP30N06L just as an example to do low side switching on my fan. I would place a 10K resistor Gate to Ground on my mosfet. I would use a pot tied to 5 volts and ground with the wiper to my analog in. I would take my PWM out and tie it to my MOSFET Gate. I would watch my analog in and PWM out and slowly increase the analog in till the fan starts spinning and make sure the fan has a full speed control range. When I know where the fan starts spinning I can better map my analog input.

In your case the temperature from your DHT sensor.

Basic Analog In and PWM Out will look like this:

const int pwm = 3 ; //naming pin 3 as ‘pwm’ variable
const int adc = 0 ; //naming pin 0 of analog input side as ‘adc’
const int sensorValue = 0 ;
void setup()
{
pinMode(pwm,OUTPUT) ; //setting pin 2 as output
}
void loop()
{
int adc = analogRead(0) ; //reading analog voltage and storing it in an integer
adc = map(adc, 0, 1023, 0, 255); //map the analog input to 0 to 255 fot PWM out.
Serial.println(sensorValue);
Serial.print(" ");

analogWrite(pwm,adc) ;
}

You don’t need an IF statement or Else statement unless you want them but if you want fan speed control over an input temperature range I would be looking at mapping the input to a PWM out.

Ron

Ron_Blain:
If I were you I would start this project by writing a simple Analog In and PWM Out script. I would use a logic level MOSFET like the FQP30N06L just as an example to do low side switching on my fan. I would place a 10K resistor Gate to Ground on my mosfet. I would use a pot tied to 5 volts and ground with the wiper to my analog in. I would take my PWM out and tie it to my MOSFET Gate. I would watch my analog in and PWM out and slowly increase the analog in till the fan starts spinning and make sure the fan has a full speed control range. When I know where the fan starts spinning I can better map my analog input.

In your case the temperature from your DHT sensor.

Basic Analog In and PWM Out will look like this:

const int pwm = 3 ; //naming pin 3 as ‘pwm’ variable
const int adc = 0 ; //naming pin 0 of analog input side as ‘adc’
const int sensorValue = 0 ;
void setup()
{
pinMode(pwm,OUTPUT) ; //setting pin 2 as output
}
void loop()
{
int adc = analogRead(0) ; //reading analog voltage and storing it in an integer
adc = map(adc, 0, 1023, 0, 255); //map the analog input to 0 to 255 fot PWM out.
Serial.println(sensorValue);
Serial.print(" ");

analogWrite(pwm,adc) ;
}

You don’t need an IF statement or Else statement unless you want them but if you want fan speed control over an input temperature range I would be looking at mapping the input to a PWM out.

Ron

I made some changes to my code in loop;

void loop() {
    Serial.println(sensorVal);
    delay(3000); // wait 3 seconds before accessing sensor once again.

    sensorVal = analogRead(0);
    fanSpeed = map(sensorVal, 600, 662, 26, 80);  //map the analog input to 0 to 255 fot PWM out

    //set 661 as out cutout or cut in limit where the fan switches from off to the lower PWM limit
  if(sensorVal <=661){
    fanSpeed = 0;
  }
    analogWrite(gate,fanSpeed) ;
  }

And the fan stop when the sensorVal shows 661, and start again at 662 just like I want it to be. :slight_smile:

aishahmars:

  if(temp = 29) {

There's your problem.

Two other things:

  • you probably want it to spin when it is 29°C or higher, not only when it is exactly 29°C
  • add hysteresis: switch on at or above 29°C, off at or below 27°C. That to prevent constant switching when the temperature is about 29°C.

wvmarle:
There's your problem.

Two other things:

  • you probably want it to spin when it is 29°C or higher, not only when it is exactly 29°C
  • add hysteresis: switch on at or above 29°C, off at or below 27°C. That to prevent constant switching when the temperature is about 29°C.

I see. Thanks a lot.

Hey guys! I’m back with the final sketch which I’m able to set the temperature received from sensor as the output for the fan to start spinning. To those who helped, thanks a lot. I also added a 5v relay module to cut off the cpu current when it reaches a specific temp :slight_smile:

final_sketch.ino (1.18 KB)