Fan speed don't go to 100%

Hi,

I'm a newby with programming and now am fiddling for days with the following.
I want a fancontroller pwm that goes faster when it's closer to the maximum temperature.
Buy in my sketch the fan speed don't go past 50%.
Hopefully somebody can help me.
My code is:

    /**
     * ReadSHT1xValues
     *
     * Read temperature and humidity values from an SHT1x-series (SHT10,
     * SHT11, SHT15) sensor.
     *
     * Copyright 2009 Jonathan Oxer <jon@oxer.com.au>
     * www.practicalarduino.com
     */

    #include <SHT1x.h>

    // Specify data and clock connections and instantiate SHT1x object
    #define dataPin  10
    #define clockPin 11
    SHT1x sht1x(dataPin, clockPin);
    int tempMin = 22;
    int tempMax = 24;
    int alertLed = 8;
    int fanSpeed;
    int fanLCD;
    int fan = 9;
    
    void setup()
    {
       Serial.begin(9600); // Open serial connection to report values to host
       Serial.println("Starting up");
    }

    void loop()
    {
      float temp_c;
      float temp_f;
      float humidity;
      pinMode(alertLed, OUTPUT);
      pinMode (fan, OUTPUT);
      

      // Read values from the sensor
      temp_c = sht1x.readTemperatureC();
      if (temp_c < tempMin) {
          fanSpeed = 0;
          digitalWrite(fan, 0); 
                            } 
          if((temp_c >= tempMin) && (temp_c <= tempMax)) 
               { 
                fanSpeed = map(temp_c, tempMin, tempMax, 32, 255); // the actual speed of fan
                fanLCD = map(temp_c, tempMin, tempMax, 0, 100); // speed of fan to display on LCD
                analogWrite(fan, fanSpeed); // spin the fan at the fanSpeed speed
               }
      if(temp_c > tempMax) 
               {
               digitalWrite(alertLed, HIGH);
               }   
      else {
          digitalWrite(alertLed, LOW); 
      }
        Serial.print("TEMP: ");
        Serial.print(temp_c); // display the temperature
        Serial.print("C ");
        Serial.print("FANS: ");
        Serial.print(fanLCD); // display the fan speed
        Serial.println("%");
        Serial.print(fanSpeed);
        delay(200);



      delay(2000);
    }

If this is working i will try so start fan at 30% even when it's lower than the minimum temperature.

      pinMode(alertLed, OUTPUT);
      pinMode (fan, OUTPUT);

Why are these in loop()? They belong in setup().

Have you written a sketch that simply varies the speed of the fan? Maybe the problem isn't with the code.

i moved pinmode to setup, still no lucht though. I will let you know soon, if I can alter the speed. Tnx

the map function is integer math which truncates values. MIght be the cause?

this one... map 22..24 to 32..255
I'd check this again..
Try as simple as IF...

as said map is truncating,

float t = getTemperature();
float fanspeed = 0;

if (t =< 22) fanspeed = 32;
else if (t >= 24) fanspeed = 255
else fanspeed = 32 + (t - 22) * 111.5;  // 111.5 = (255-32)/(max-min) in float math

It now works thx.

I just started over again, and now it goes perfect.

Tomorrow i can try to add a minimum start speed of 20%, so it will never drops below that.

@robtillaart
Tomorrow i will try your code also, I now still have the "map" code , but i noticed the speed goes up only if the temp is up a full degree, and with your setup i think it the fan speed will also increase if temp goes up only a few decimals.

Bedankt :wink:

let us hear if it works (or not)

@Rob.

Everything works, many tnx for it.

I think my code is a bit messy, don't know how to make it better but it works.

This is my code so far.

  #include <SHT1x.h>
    #define dataPin  10
    #define clockPin 11
  
    int fan = 9; // pwm lan
    int led = 8; // Allert led 
    SHT1x sht1x(dataPin, clockPin);
    
    int tempMin = 23; // Temp to accelerate more dan minimun Fan Speed
    int tempMax = 30; // the maximum temperature when fan is at 100%
    int fanSpeed;
    int fanLCD;
    int minFanSpeed = 30; // minimal speed fan in % 
     
    void setup() {
    Serial.begin(9600); // Open serial connection to report values to host
    pinMode(fan, OUTPUT);
    pinMode(led, OUTPUT);
  
    }
     
    void loop() {
        
     float temp = sht1x.readTemperatureC();
     float humidity = sht1x.readHumidity();
     
     if (temp < tempMin) 
         {
         fanSpeed = minFanSpeed * 2.55;   
         }
       else if (temp >= tempMax)
         {
       fanSpeed = 255; 
       digitalWrite (led, HIGH);
         } 
     else 
         {
     fanSpeed = (minFanSpeed * 2.55)  + (temp - tempMin) * ((minFanSpeed * 2.55)/(tempMax-tempMin));  // 2.55 is to make from procent to a 255 value  
     digitalWrite (led, LOW);
   delay (1000);
         }
     
 

   analogWrite(fan,fanSpeed);  
   Serial.println (temp); 
   Serial.println (fanSpeed);
   Serial.println (fanSpeed/2.55,0); //For LCD display in the future with no decimals
   delay(2000);  

    }

Tnx

lxx_28:
I think my code is a bit messy, don't know how to make it better but it works.

Press CTRL-T in the IDE for one.