user4387: I am trying to build a simple fan controller (single fan). So far I have successfully created a circuit which powers the fan with an external 12v DC adapter and added a transistor (NPN) to switch it on and off. The transistor is connected to a PWM port on the Arduino. 
When putting HIGH on the port the fan works perfectly at high speed, but when I want to lower the speed (say analogWrite(fanPin, 150)) it starts to make a humming noise. I believe this is caused by the use of PWM.
What is the best way to control the speed of the fan without (too much) noise?
ps. The fan is a Cooler Master 3-wire PC fan. Transistor is a P2N2222A.
Update: Here is the schema I am using:


user2881: Try using pin 3 (or 11) and lower the prescaler of timer 2 using the following code in your setup;
//move from /64 prescaler to /32 prescaler
TCCR2B |= _BV(CS20);//set bit (remove this line for a /8 prescaler)
TCCR2B |= _BV(CS21);//set bit
TCCR2B &= ~_BV(CS22);//clear bit

Halving the prescaler will double the frequency.

user5174: It might help to use a capacitor to smooth your PWM signal.
See http://ww1.microchip.com/downloads/en/AppNotes/00771b.pdf

user29365: I had the same problem with a 5V fan.  Something simple worked for me: To decrease the PWM frequency.  You probably want to do this using pin 9 or 10 (for the Uno) as changing the frequencies of other PWM pins messes up the delay and millis functions.  Details on how to do it and a very convenient function are provided in http://playground.arduino.cc/Code/PwmFrequency.  In my case, using the lowest frequency possible (31250/1024 = 31Hz), I completely eliminated the hum for higher speeds and significantly reduced it for lower speeds.  And by the way, I used the PWM pin to drive a MOSFET since the current of my fan was 140 mA, much higher than the 20 mA Arduino pins can safely provide.

4387: <p>I am trying to build a simple fan controller (single fan). So far I have successfully created a circuit which powers the fan with an external 12v DC adapter and added a transistor (NPN) to switch it on and off. The transistor is connected to a PWM port on the Arduino. </p>

<p>When putting <code>HIGH</code> on the port the fan works perfectly at high speed, but when I want to lower the speed (say <code>analogWrite(fanPin, 150)</code>) it starts to make a humming noise. I believe this is caused by the use of PWM.</p>

<p>What is the best way to control the speed of the fan without (too much) noise?</p>

<p>ps. The fan is a Cooler Master 3-wire PC fan. Transistor is a P2N2222A.</p>

<p><strong>Update</strong>: Here is the schema I am using:</p>

<p><img src="https://i.stack.imgur.com/UIYfW.png" alt="schema"></p>
 2881: <p>Try using pin 3 (or 11) and lower the prescaler of timer 2 using the following code in your <code>setup</code>;</p>

<pre><code>//move from /64 prescaler to /32 prescaler
TCCR2B |= _BV(CS20);//set bit (remove this line for a /8 prescaler)
TCCR2B |= _BV(CS21);//set bit
TCCR2B &amp;= ~_BV(CS22);//clear bit
</code></pre>

<p>Halving the prescaler will double the frequency.</p>
 5174: <p>It might help to use a capacitor to smooth your PWM signal.</p>

<p>See <a href="http://ww1.microchip.com/downloads/en/AppNotes/00771b.pdf" rel="nofollow">http://ww1.microchip.com/downloads/en/AppNotes/00771b.pdf</a></p>
 29365: <p>I had the same problem with a 5V fan.  Something simple worked for me: To <strong>decrease</strong> the PWM frequency.  You probably want to do this using pin 9 or 10 (for the Uno) as changing the frequencies of other PWM pins messes up the delay and millis functions.  Details on how to do it and a very convenient function are provided in <a href="http://playground.arduino.cc/Code/PwmFrequency" rel="nofollow noreferrer">http://playground.arduino.cc/Code/PwmFrequency</a>.  In my case, using the lowest frequency possible (31250/1024 = 31Hz), I completely eliminated the hum for higher speeds and significantly reduced it for lower speeds.  And by the way, I used the PWM pin to drive a MOSFET since the current of my fan was 140 mA, much higher than the 20 mA Arduino pins can safely provide.</p>
