PWM fan noise with map function

Hi guys, ive 99% finished my aquarium cooling project and let me just say what a mission it was being a noob. The final issue i have now is the PWM noise. I can get rid of the noise by using a 470uf cap in parallel with the fan load, however because im using the map function to vary the fan speed, the cap forces the fans to spin 100%. any help would be great.

#include <LiquidCrystal_I2C.h> //header for I2C lcd
LiquidCrystal_I2C lcd(0x27,16,2); //I2C address = 0x27

const long eventTime_Tem = 2000; //delay time

unsigned long previousTime_Tem = 0; 
int x;
unsigned long temptot = 0;

int tempMin =24;
int tempMax = 27;
int fanSpeed;
int PWMfanPin = 9;
int PWMval;
int sensorVal;


void setup() {
  Serial.begin(9600);
  analogReference(INTERNAL); //set reference of 1.1V
  lcd.init();
  lcd.backlight();
  lcd.clear();
}

void loop() {
int x;
unsigned long currentTime = millis(); //assigning the value of mills
unsigned long temptot = 0;
//taking 100 sample and adding
for(x=0; x<100 ; x++)
{
  temptot += analogRead(A0);
  }
float  sensorValue = temptot/100; //calculating average
float voltage = sensorValue * (1100 / 1023); //convert sensor reading into milli volt
float temp = (voltage*0.1) ; //convert milli volt to temperature degree Celsius 
{
if(currentTime-previousTime_Tem >= eventTime_Tem); //Check for delay time
}


  if (temp < tempMin ){
    fanSpeed = 0;
    PWMval = 0;
    analogWrite(PWMfanPin, PWMval);
  }

if ((temp > tempMin) && (temp < tempMax)){
  
  //fanSpeed = 50;
  //PWMval = 60;
  
  PWMval = map(temp, tempMin, tempMax, 20, 255);// Need to solve PWM noise - 470uf incorrect
  
  fanSpeed = map(temp, tempMin, tempMax, 0, 100);

  analogWrite(PWMfanPin, PWMval);

}

  if (temp > tempMax){
    fanSpeed = 100;
    PWMval = 255;
    analogWrite(PWMfanPin, PWMval);
  }

  
  lcd.setCursor(0,0);
  lcd.print("Water Tmp:");
  lcd.print(temp,1);
  lcd.print((char)223);
  lcd.print("C");
  lcd.setCursor(0,1);
  lcd.print("Fan Speed:");
  lcd.print(fanSpeed);
  lcd.print("%  ");
  Serial.print("Water Temp: ");
  Serial.print(temp,1);
  Serial.print(", Fan Speed: ");
  Serial.print(fanSpeed);
  Serial.println();
  previousTime_Tem = currentTime;


delay(1000);
}

Credit to

for the code i needed.

You can change the Arduino PWM frequency on the pin9 to 31KHz, which will bring the frequency out of the audible range and the fan will not make noise.
To do this, just add two lines to the setup:

// Pins D9 and D10 - 31.4 kHz
TCCR1A = 0b00000001; // 8bit
TCCR1B = 0b00000001; // x1 phase correct

See the tutorial for other PWM options:

2 Likes

Thanks for the quick reply!!!

I also forgot to write im using an IRF540N MOSFET.
awesome. Ill give that a crack.

hahaha PERFECT!!!!!
Thank you so much!!!!! :slight_smile:

If your problem is solved you can mark the topic by clicking the :ballot_box_with_check: Solution button at the bottom of the reply that answered your question. This will make it easy for helpers to see that it is solved and for others with the same question to find the answer quickly.

Is there a way to allow the map function read decimals. The reason why I need it to is because my low temp is 24 and high is 27. That only gives the map 4 stages of output. It would also help the LCD display fanSpeed correctly.

Try to multiple the temperature to 10 or 100.

That will gives you 10 times more control steps

Too easy. Thank you so much again :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.