How to control the PWM output of the Nano

I'm trying to control the speed of a PWM fan (4 wire, DC brushless motor with a PWM wire). The PWM pin of the Arduino will connect to the PWM wire of the fan, and the 2 power wire would connect to the battery. There is another tacho wire that won't be used.

My question is, how do I actually change the speed of the fan? I'm a beginner but I feel there are many ways to do this. I want to a knob that I can turn. I'm thinking a potentiometer might be one way to do this, the Arduino can then sense a change in resistance somehow and change the duty cycle of the PWM. But I don't know the details of implementation. If you can recommend a webpage or tutorial that'll be appreciated.

I need the knob to be big enough to easily handle. Wouldn't want one of those knobs that one has to use a screwdriver to turn...
Thanks

Also

Your idea sounds perfectly feasible adn a great beginner project. Try doing some google work with arduino and potentiometer and you will find TONS of not only information and you tube videos and tutorials, and hackadays, and so much stuff that it would be impossible to list here. Just google a little and you'll find something already written that's exactly what you're looking for. This has been done so many times that even trying to choose one to show you would be hard just because we don't know much about your skill level. But I promise that this particular topic has been covered like a million times all over the internet. Someone has done it at your level whatever that level is.

1 Like

There is an important book you should have in your library, it is called Arduino Cookbook, it is very good and would have everything you need for your project. Since you are new I highly suggest you also learn a little CAD to draw your schematics. You can download a full CAD program called KiCad for free, the only thing asked is a freewill donation. I use it and it takes me from idea through completed circuit boards. Learning it is not an over the weekend project unless you have CAD experience. There are others available some as demos with some limits. Schematics is the language of electronics. A lot of users use the frizzy program but after a few items it is useless and definitely does not contain enough information to properly debug a project unless you are intimately familiar with it.

Tell me the frequency of your PWM signal? Once I know the answer, I will show you the way of changing the speed of the fan by changing the duty cycle of the PWM signal using a potentiometer.

4-pin computer fans should be PWM-ed with 25kHz (according to the Intel specifications).
But the fans I tried work just fine on the default PWM frequency of an Arduino.

Connect fan ground and the blue PWM wire to ground and pin 5 of the (classic) Nano.
Connect 12volt fan power to ground (black) and fan power (yellow or red).
Connect the pot to Nano 5volt and ground, and the center pin of the pot to A0.
This is all you need for basic control.

const byte potPin = A0, fanPin = 5; // 1kHz PWM

void setup() {
}

void loop() {
  analogWrite(fanPin, analogRead(potPin) >> 2);
}

If you have that working, then Google for the more complicated 25kHz examples. Like this one.
Leo..

Connect the fan gnd to Nano gnd.
Connect the blue PWM to pin 5 of the Nano.

Tom... :grinning: :+1: :coffee: :australia:

Thanks.
The fan I'm talking is actually this:
https://www.mouser.com/datasheet/2/632/BUB0712HD_SM-2948392.pdf

It does say 25kHz for PWM, and after doing some reading on Arduino, it seems the analogWrite function doesn't output anywhere near that frequency (do you know how what is the frequency for analogWrite?) So right now I'm currently reading about the timers.

So it seems so much easier to just use the analogWrite. Now you said the fans you tried worked fine, but I wonder from a theory perspective what's the drawbacks (if any)?

As a side question, these 4 wire PWM fans seems to be for computer use. I wonder if they have bigger fans for say, home ventilation use or HVAC use that work similarly (4 wire, PWM, can be controlled via Arduino)?

thanks

I picked pin5, because pin5 and pin6 are about 1kHz on an Uno.
The other PWM pins are about 500Hz.
Google also knows those things. Try "Arduino PWM frequency".

The drawbacks of a lower PWM frequency is a possible audible PWM whitle in the fan, and/or not being able to dial down to very low speeds.

Many larger fans also seem to use the same Intel specification as computer fans.
Leo..

Thanks. I will try the analogWrite function, but I'm still interested in learning about using 25khz PWM.
This is the one source I found:

It does mention that pin5 and pin6 are 977 hz (timer 0). It did provide some code but I'm having some trouble understanding it:

//configure Timer 1 (pins 9,10) to output 25kHz PWM
void setupTimer1(){
    //Set PWM frequency to about 25khz on pins 9,10 (timer 1 mode 10, no prescale, count to 320)
    TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11);
    TCCR1B = (1 << CS10) | (1 << WGM13);
    ICR1 = 320;
    OCR1A = 0;
    OCR1B = 0;
}

//equivalent of analogWrite on pin 9
void setPWM1A(float f){
    f=f<0?0:f>1?1:f;
    OCR1A = (uint16_t)(320*f);
}
//equivalent of analogWrite on pin 10
void setPWM1B(float f){
    f=f<0?0:f>1?1:f;
    OCR1B = (uint16_t)(320*f);
}

It'll be nice if someone can explain what this line means:

TCCR1A = (1 << COM1A1) | (1 << COM1B1) | (1 << WGM11);

While I can Google things like OCR1A and WGM11, it's hard to understand the symbols like << and expressions like f=f<0?0:f>1?1:f

Are there are any good libraries for PWM? It seems a lot of work to deal with that low level stuff if one wants a PWM frequency beside what's offered with analogwrite()...

Changing the PWM frequency on the Nano is a question of two lines of code:

// Pins D5 and D6 - 31.4 kHz
TCCR0B = 0b00000001; // x1
TCCR0A = 0b00000001; // phase correct

After then you could use the analogWrite() function as before, but with 31 KHz frequency

More info

Thanks.
So why does the tutorial I pasted contain so much more code, dealing with OCRnA/OCRnB, etc.

Meaning is: (:? is known as ternary operator in C Language.)

if(f < 0)
{
      f = 0;
} 
else   //f = f>1?1:f;
{
   if(f > 1)
   {
       f = 1;
   }
   else
   {
        f = f;
   }
}

Meaning:
1. Place 1 (HIGH) at COM1A1 position of TCCR1A Register without affecting other bits:
TCCR1Ax

or
2. Keep shifting 1 (HIGH) to the left until it appears at COM1A1 position of TCCR1A register.

or
3. Shift 1 to the left by n-bits; where, n is equal to the positional index of COM1A1 bit in the TCCR1A register, which is 7 here.

The bitshift operators (<<) and the OR are in any C++ or Arduino reference: Arduino Reference - Arduino Reference

The register names and what they do are all in the Holy Datasheet.

There's a lot of power in that pdf document.

I appreciate the help so far. We've been talking about sending a PWM signal to the PWM wire of a 4 wire fan to control the speed.

The fan also comes with a 3 wire version (non - PWM). I wonder if it's possible to control the speed for the non - PWM version by lowering the drive voltage instead? It's a "12V" fan so I wonder what's the difference (if any) between going the PWM route with a 50% duty cycle (so average voltage is 6V) VS feeding constant 6V to the power?

The difference I'm interested in are static pressure and flow rate. With fans, it usually delivers max static pressure at low flow rate, and pressure drops as flow rate increases.
Thanks

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