Author Topic: Controlling the speed of a DC motor via PWM  (Read 6075 times)

0 Members and 1 Guest are viewing this topic.

Offline engineheatTopic starter

  • Frequent Contributor
  • **
  • Posts: 268
  • Country: us
Re: Controlling the speed of a DC motor via PWM
« Reply #25 on: June 30, 2023, 06:24:54 pm »
I powered the fan with nominal voltage (12V) directly, without any electronics, and it ran fine. The flow and noise is a bit higher than what I want. Then I powered it with a 6V battery pack directly. It still ran, but the flow is too low although the noise was low too.

We've been talking about PWM up to this point, but what about just using an adjustable buck converter to bring down the voltage from 12V to something like 8 or 9V? How is it different than the PWM approach in terms of the static pressure and flow rate that I can expect?
Thanks
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 8185
  • Country: fi
Re: Controlling the speed of a DC motor via PWM
« Reply #26 on: June 30, 2023, 06:49:07 pm »
Adjustable buck converter is the ideal solution. Just make sure the fan starts at that voltage, even after the bearings wear out or dust accumulates making it harder to turn. Well, if it starts at 6V, then 8-9V won't be a problem. Both static pressure and flow rate go down when lowering the voltage. Some datasheets might show curves for different voltages, but I'd suggest simple testing.
 

Offline engineheatTopic starter

  • Frequent Contributor
  • **
  • Posts: 268
  • Country: us
Re: Controlling the speed of a DC motor via PWM
« Reply #27 on: June 30, 2023, 10:57:50 pm »
Adjustable buck converter is the ideal solution. Just make sure the fan starts at that voltage, even after the bearings wear out or dust accumulates making it harder to turn. Well, if it starts at 6V, then 8-9V won't be a problem. Both static pressure and flow rate go down when lowering the voltage. Some datasheets might show curves for different voltages, but I'd suggest simple testing.

Can you recommend a good adjustable buck converter? I played around with a no-name one years ago and it died after a few days. The motor wattage is less than 5W and draws less than 0.5A. I just need one that converts 12V to a range from 6V to 12V.

There's so many different ones, (and of different size) I don't know which to get or what to look for. Some have big components like the following one:



And some are really small:

https://www.sparkfun.com/products/15208


Thanks
 

Offline alligatorblues

  • Regular Contributor
  • *
  • Posts: 155
  • Country: us
Re: Controlling the speed of a DC motor via PWM
« Reply #28 on: July 01, 2023, 01:22:00 am »
When I need a pwm controller, i just purchase one off eBay for $3. Haven't had one fail yet. I even have a 10A for driving tec chips.
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 8185
  • Country: fi
Re: Controlling the speed of a DC motor via PWM
« Reply #29 on: July 01, 2023, 05:41:06 am »
Can you recommend a good adjustable buck converter?

Try some modules from Digikey/Mouser/etc. (use the parametric search). Real companies like Murata and WE make regulator modules, and while a tad more expensive than Ebay crap, they would work reliably.

If the fan is small (a few W), you don't need a switcher; dropping some voltage with a schottky diode (or string of standard diodes) is good enough. Say, 4-5 standard Si diodes in series to drop from 12V to 9V. Making 9V out of 12V by linear regulator / resistive dropping is still 75% efficient. An oversized yet non-synchronous buck module running at low load is not necessarily more efficient at all, could be even worse!
 

Online Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6319
  • Country: fi
    • My home page and email address
Re: Controlling the speed of a DC motor via PWM
« Reply #30 on: July 01, 2023, 11:29:34 am »
Say, 4-5 standard Si diodes in series to drop from 12V to 9V.
This was standard practice with 3-pin (voltage-controlled) PC fans (and many PWM-controlled ones are 3-pin compatible); for example, Noctua LNA and ULNA adapters simply add standard silicon diodes in series with the fan, dropping the voltage over the fan.  And Noctua fans are generally considered very high quality fans, too.
 

Offline engineheatTopic starter

  • Frequent Contributor
  • **
  • Posts: 268
  • Country: us
Re: Controlling the speed of a DC motor via PWM
« Reply #31 on: July 02, 2023, 12:03:00 am »
Well I decided to just try the PWM approach first. I'm using an Arduino Uno.

Here's my planned schematic:



I have a potentiometer that I plan to use to change the speed and it's connected to pin A0. Do I need any pull up or pull down resistors or capacitors anywhere?

Thanks
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 9964
  • Country: nz
Re: Controlling the speed of a DC motor via PWM
« Reply #32 on: July 02, 2023, 12:03:30 am »
What's going to blow up?  I was not suggesting PWM at mhz,

......
Fan does not care. The problem is switching the capacitor with unlimited current, even without load.

I'm an idiot. Shouldn't be posting late at night
Greek letter 'Psi' (not Pounds per Square Inch)
 
The following users thanked this post: Siwastaja

Offline ArdWar

  • Frequent Contributor
  • **
  • Posts: 373
  • Country: sc
Re: Controlling the speed of a DC motor via PWM
« Reply #33 on: July 02, 2023, 01:00:29 am »
Here's my planned schematic:


Put the MOSFET on low side (below the fan) if you're using NFET (like IRFZ44).
If you're using your schematic placement (high side) then you have to use PFET instead (IRF9540 etc) and some additional gate drive circuit due to the higher drain voltage.

If you want to drive them directly from GPIO, you actually have to pick logic level MOSFETs instead of those parts. But for your relatively low current application, those parts should still be fine.

It's good practice to put gate pullup resistor for PMOS and pulldown for NMOS.
« Last Edit: July 02, 2023, 01:04:29 am by ArdWar »
 

Offline Siwastaja

  • Super Contributor
  • ***
  • Posts: 8185
  • Country: fi
Re: Controlling the speed of a DC motor via PWM
« Reply #34 on: July 02, 2023, 08:36:21 am »


That won't work. N-channel MOSFET needs positive Vgs to keep on, you would need more than 12V+8V = 20V to keep it conducting, yet the IO pin only gives 5V. Simply move the MOSFET to ground side (swap MOSFET and fan in the schematic) because the fan is a floating load (as long as you don't need tacho output). IRFZ44N is massively oversized and does not turn fully on at 5V at higher Id, but given very low load current, it will still work here.
 

Offline Simon

  • Global Moderator
  • *****
  • Posts: 17830
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Controlling the speed of a DC motor via PWM
« Reply #35 on: July 02, 2023, 04:42:02 pm »
This will not work, piss the fan off or the fan may run pretty much at the full speed but with a shorter life time.

So put an inductor in series with the fan, a capacitor across the fan (it may have enough internal capacitance). Put a fast diode from the mosfet switch output to the oposite power polarity that the mosfet switches. You now have a basic buck converter. Use a sensible frequency like over 100kHz. Or just buy a speed controllable fan.
 

Offline eugene

  • Frequent Contributor
  • **
  • Posts: 495
  • Country: us
Re: Controlling the speed of a DC motor via PWM
« Reply #36 on: July 02, 2023, 09:14:41 pm »
I've controlled fans in this way (but with the transistor on the low side of the fan) and it worked fine. The caveat is that the PWM frequency needs to be low, like 50Hz. Experiment to see what makes your fan happy.

If the OP simply uses analogWrite() on the Arduino Nano, then they're going to get a PWM frequency that will be much too high. 490Hz, IIRC. That will make the fan unhappy. Interestingly, that frequency would not work with a 4 wire speed controllable fan either as they want ~25kHz...

Fortunately, it's not hard to adjust the PWM frequency of the Nano: https://www.etechnophiles.com/how-to-change-the-pwm-frequency-of-arduino-nano/
90% of quoted statistics are fictional
 

Offline engineheatTopic starter

  • Frequent Contributor
  • **
  • Posts: 268
  • Country: us
Re: Controlling the speed of a DC motor via PWM
« Reply #37 on: July 17, 2023, 07:38:07 am »
I changed the setup for the 2 wire fan to:



When PWM frequency is 30 Hz, it runs fine and I was able to control the speed with a pot. I implemented the low frequency PWM from scratch using millis(). But when I used the default frequency of 490Hz for analogwrite(), the fan did not like it. It does not spin at low duty cycles and only spins weakly at high duty cycle.

I got myself some 4 wire PWM fan as well. Those need 25khz. Fortunately I was able to figure out how to get 25khz using timers. The 4 wire fan also has a wire for tachometer. I'd like to know how those work and how to capture the speed using the Arduino. I think it just outputs a pulse per revolution?

I wasn't able to find much info on the tach in the datasheet:
https://www.digikey.com/en/products/detail/delta-electronics/BFB0712HH-AWPH/13997125?s=N4IgTCBcDaIEIDE4AYDsBGMAJLBaAggOoAKWIAugL5A

Thanks
 

Online Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6319
  • Country: fi
    • My home page and email address
Re: Controlling the speed of a DC motor via PWM
« Reply #38 on: July 17, 2023, 10:10:49 am »
I'd like to know how [4-pin PWM fans] work and how to capture the speed using the Arduino.
Already told you, in #24.

All 4-pin 12V PWM computer fans are supposed to follow the Intel 4-wire PWM Fan specification.

You need two N-channel MOSFETs with low Vgs(th), capable of sinking at least 10mA, and one pull-up resistor to 12V (R2).  I like NX138AKR, because they're cheap in SOT-23-3 and well suited for this (and won't be harmed by accidentally putting 12V or ground on the fan tach or PWM pins, as it can handle Vgs to ±20V), but many, many other N-channel enhancement mode MOSFETs are just as good for this; pick your favourite.

Additionally, you can use a small resistor (R1) to limit the very very short current spike when switching the PWM MOSFET (to protect the I/O pin; not all microcontrollers need this), and a pull-up resistor (R3) to your microcontroller I/O voltage, 3.3V or 5V, although typical microcontrollers do have sufficient internal pull-ups you can enable on inputs.  Their values shown in the above schematic are just examples.  For an ATtiny85 running at 5V, I intend to use R1=220Ω, R2=10kΩ, and omit R3.



The tach input is two pulses per rotation.  Thus, if you get P pulses per second, the fan is rotating at 30×P RPM.
Conversely, even at 3000 RPM, you only get about 100 pulses per second, so you can use an interrupt on the pin.

The above circuit inverts the logic of both signals, i.e. your microcontroller needs to generate PWM that is LOW during the duty cycle, and HIGH when idle; and the start of the tach pulse is a trailing edge, i.e. HIGH to LOW transition.

The fans are not required to start under 30% duty cycle.  The duty cycle is relative to the maximum speed the fan is capable of given the current supply voltage.
« Last Edit: July 17, 2023, 10:19:09 am by Nominal Animal »
 

Offline eugene

  • Frequent Contributor
  • **
  • Posts: 495
  • Country: us
Re: Controlling the speed of a DC motor via PWM
« Reply #39 on: July 17, 2023, 01:26:34 pm »
With all due respect, I don't think all of those transistors are necessary. The tach output is typically open collector, so all that's needed is to connect it directly to a GPIO pin with a pullup resistor to VCC. The PWM input can be driven directly by a GPIO pin. Supposedly, 5V logic is required, but they often work fine with 3.3V. No significant current goes in or out of either fan pin.
90% of quoted statistics are fictional
 

Offline Simon

  • Global Moderator
  • *****
  • Posts: 17830
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Controlling the speed of a DC motor via PWM
« Reply #40 on: July 17, 2023, 06:19:07 pm »
I'm still confused, why are you trying to speed control a 2 wire fan when you can get one with a PWM input?

I am speed controlling a much bigger 2 wire fan, this is brushed, and even though it will work with plain old PWM, I am making a basic buck converter. You seem to be finding the simplest problem and turning it into the biggest project. Fundamentally if you don't understand how these things work and therefore how to trick them into doing what you want because you are too lazy to use the right tool for the job you will waste a lot of time on something that is a non issue.
 

Offline engineheatTopic starter

  • Frequent Contributor
  • **
  • Posts: 268
  • Country: us
Re: Controlling the speed of a DC motor via PWM
« Reply #41 on: July 18, 2023, 01:23:45 am »
I'm still confused, why are you trying to speed control a 2 wire fan when you can get one with a PWM input?


I only recently got the 4 wire version. It was out of stock.
 

Offline engineheatTopic starter

  • Frequent Contributor
  • **
  • Posts: 268
  • Country: us
Re: Controlling the speed of a DC motor via PWM
« Reply #42 on: July 18, 2023, 01:36:29 am »
With all due respect, I don't think all of those transistors are necessary. The tach output is typically open collector, so all that's needed is to connect it directly to a GPIO pin with a pullup resistor to VCC. The PWM input can be driven directly by a GPIO pin. Supposedly, 5V logic is required, but they often work fine with 3.3V. No significant current goes in or out of either fan pin.

Like this?
 

Offline engineheatTopic starter

  • Frequent Contributor
  • **
  • Posts: 268
  • Country: us
Re: Controlling the speed of a DC motor via PWM
« Reply #43 on: July 18, 2023, 03:50:50 am »
Any idea on how to count the pulses using the Arduino to get the rpm?
I saw some solutions using the pulseIn() function but that seems to wait for the tach pin to change states so wouldn't work well if the rpm is slow or 0.

I can only think of waiting a fixed time period in the loop each time, and count how many times the tach pin changes state from HIGH to LOW.
 

Online pqass

  • Frequent Contributor
  • **
  • Posts: 731
  • Country: ca
Re: Controlling the speed of a DC motor via PWM
« Reply #44 on: July 18, 2023, 06:20:00 am »
Any idea on how to count the pulses using the Arduino to get the rpm?
I saw some solutions using the pulseIn() function but that seems to wait for the tach pin to change states so wouldn't work well if the rpm is slow or 0.

I can only think of waiting a fixed time period in the loop each time, and count how many times the tach pin changes state from HIGH to LOW.
Yes on the last statement. 

It can be done by counting the number of interrupts on Arduino pin 2 (wired to the fan tach output) then freezing interrupts, collecting the count so far and the time lapsed (millis()) since the last freeze. A rate can then be calculated by dividing one by the other.  Notice that the time lapsed doesn't have to be a fixed, precise amount every time. Any update rate you prefer can be the time interval since if more time passes, more counts are accumulated.

This is what reciprocal frequency counters do; giving high precision even at low frequencies.  If your fan speed is about 1000RPM (or <17RPS) then your Arduino (@16Mhz) will be able to keep up.

Something like the following [untested] code should work:
Code: [Select]
const byte interruptPin = 2;   // or 3 would work too
volatile int counts = 0;
unsigned long last = 0;

void setup() {
  pinMode(interruptPin, INPUT_PULLUP);    // tach output needs pullup to MCU Vcc
  attachInterrupt(digitalPinToInterrupt(interruptPin), incCount, FALLING);
}

void loop() {
  noInterrupts();    // stop the count and millis() clock for a brief moment
  unsigned long now = millis();
  float rate = (now - last) / counts;
  last = now;
  count = 0;
  interrupts();    // carry on counting
  // rate var is in milliseconds per revolution which can be converted to RPM
  // may need to divide counts var by n if the fan generates n counts per revolution.
  delay(1000);   // do other stuff or wait some brief period of time before calculating rate again
}

void incCount() {
  counts++;
}
« Last Edit: July 18, 2023, 06:54:28 am by pqass »
 

Offline Simon

  • Global Moderator
  • *****
  • Posts: 17830
  • Country: gb
  • Did that just blow up? No? might work after all !!
    • Simon's Electronics
Re: Controlling the speed of a DC motor via PWM
« Reply #45 on: July 18, 2023, 12:09:28 pm »
I don't know what the arduino offers these days in the software and it also depends on the MCU you are using. For your purposes pulses over a period of time will be better. I have used both methods and currently work on a machine where I time the pulse as I need this information quite quickly to regulate the speed. It's a case of looking at how long can you wait for the information and how long the pulse is at the slowest speed of interest and therefore at what speed do you assume 0.
 

Offline eugene

  • Frequent Contributor
  • **
  • Posts: 495
  • Country: us
Re: Controlling the speed of a DC motor via PWM
« Reply #46 on: July 18, 2023, 02:35:06 pm »
Like this?

Yes. I've controlled 4 wire fans that way. For sending the PWM to the fan, a circuit like the one Nominal Animal shows might be more robust depending on the fan and MCU. There's a risk that the fan contains a pullup to 5V on that pin, but if your MCU is not tolerant of 5V (some 3.3V MCUs are not) then the MCU might be damaged. The open collector/drain transistor prevents any issue.

As for the tach signal, I've been trying to figure out why Nominal Animal suggests such a complicated circuit. I finally realized that he's dutifully following a line in the Intel document that says "Motherboard will have a pull up to 12V..." Once he does that then he needs all of the other circuitry to accommodate the 12V. Well, I am virtually certain that that line is an error. It makes no sense unless the logic level of the motherboard is 12V. It's an open collector output and can be pulled up to any voltage desired. In this case, pulling it up to the logic level of the MCU makes the most sense, then all those transistors are completely unnecessary.
90% of quoted statistics are fictional
 

Online Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6319
  • Country: fi
    • My home page and email address
Re: Controlling the speed of a DC motor via PWM
« Reply #47 on: July 18, 2023, 05:34:45 pm »
As for the tach signal, I've been trying to figure out why Nominal Animal suggests such a complicated circuit.
If you trust all your fans have open collector tach outputs, and that their PWM input doesn't have a pull-up (or pull-up equivalent circut) to higher than your logic voltage, then you do not need the MOSFETs.  I don't trust the fans that much.

Instead, I use less than 0.50€ worth of components (from Mouser) per fan, to make sure that no matter what kind of circuit shenanigans the fan contains, my circuit will deal with it properly.  The MOSFET I chose has low Vgs(th) (it'll work even with 1.8V logic), and Vgs range is ±20V  (and Vds max is 60V).  This means that even if the fan were to short both PWM and TACH signals to 12V, my circuit doesn't mind, and nothing will break.

This is my logic: I don't want to tell others what the minimum circuit is that may or may not work with specific situations; instead, I want to show how to do it reliably, without risking your MCU.  I do the same with software, and honestly tell them that if they ever tell me they'll "add error checking afterwards, when they have more time", I'll throw them out, because that never gets done.  You either design for robust and secure implementation from the get go, or you are just throwing spaghetti at the wall and seeing what sticks.  Engineering, or guesswork.

Attached is a rendering of the 61.5mm × 15.2mm (2.42" × 0.6") board based on ATtiny85 in DIP-8, powered from 12VDC, that I designed today.  It can control two fans (using one potentiometer, on the 3-pin JST PH-2.0mm connector), using Timer1.  The capacitors are all 0805, and all resistors are 0805 (but the pads also support 0603).  The two support holes are 2.54mm or 0.1", for M2.5 or #2-56 or #3-48 nylon screws or supports.  Omit the MOSFETs, and you shorten the board by 5mm to 10mm (0.2" - 0.4").  Not worth it.
« Last Edit: July 18, 2023, 05:40:22 pm by Nominal Animal »
 

Offline eugene

  • Frequent Contributor
  • **
  • Posts: 495
  • Country: us
Re: Controlling the speed of a DC motor via PWM
« Reply #48 on: July 18, 2023, 09:08:48 pm »
As for the tach signal, I've been trying to figure out why Nominal Animal suggests such a complicated circuit.
If you trust all your fans have open collector tach outputs, and that their PWM input doesn't have a pull-up (or pull-up equivalent circut) to higher than your logic voltage, then you do not need the MOSFETs.  I don't trust the fans that much.

I get that. And whatever trust I have comes from the same place as yours: the Intel spec document. It clearly states that the output of the tach signal is open collector. It also states that the specified circuit to drive the PWM input should be open collector. I know there's huge risk in trying to second guess someone else's intentions, but I'm still going to guess that those specs were made to eliminate any issues with different logic levels. Likewise, the spec that there will be two pulses per rev in the tach signal is purely practical: the Hall sensors used for commutation in these little BLDC motors output two pulses per revolution. Intel just wanted to make it easy for fan manufacturers to make fans that work with a minimum of cost and complexity.

So, using an open collector/drain transistor makes good sense when designing a circuit that might work with fans from unknown sources. OTOH, it is my firm belief that the spec that "Motherboard will have a pull up to 12V" is an error. Possibly it was intended to say "Motherboard will have a pull up to no more than 12V." That tells the fan manufacturers that they can use transistors with max Vds of at least 12V and not have to worry. Specifying that every motherboard will have a pull up to 12V makes no sense at all.

I have no hard evidence, but I'm willing to bet lunch money for a week that spec as written is a mistake.

Quote
"Instead, I use less than 0.50€ worth of components (from Mouser) per fan, to make sure that no matter what kind of circuit shenanigans the fan contains, my circuit will deal with it properly. 

I get that too. And, obviously, if I were asked to design a commercial circuit where there was no way to know what fan might be attached, then I would always include the open collector drive for the PWM. OTOH, I would not bother with the complicated tach circuit and instead just trust that the fan has an open collector output on that pin as the Intel spec calls for.

But that's a 'serious' design with PCBs etc. In this case we're talking to a hobbyist who is likely prototyping on a wireless breadboard and possibly moving eventually to proto board. For them, it's not the cost of the components but the effort involved in building the circuit, and then troubleshooting it because they put it together wrong. The Intel document says that there will be a pull up on the PWM input to no more than 5.25V (IIRC) but they encourage new designs to pull up to 3.3V. In those cases, it will not damage the 5V MCU to drive the pin directly, thereby reducing circuit complexity greatly. However, I also suggested that your open drain circuit is more robust.

I still recommend to everyone that no more than a simple pull up is required on the tach signal.

Quote
This is my logic: I don't want to tell others what the minimum circuit is that may or may not work with specific situations; instead, I want to show how to do it reliably, without risking your MCU. 

I get that too. However I also recognize that designing to specific requirements is not a bad thing. If I ask a contractor about building a footbridge across a small ravine that will only need to support me and my dog, and the contractor comes back with a design that can support military vehicles, then I will look for a different contractor.
90% of quoted statistics are fictional
 

Online Nominal Animal

  • Super Contributor
  • ***
  • Posts: 6319
  • Country: fi
    • My home page and email address
Re: Controlling the speed of a DC motor via PWM
« Reply #49 on: July 19, 2023, 09:17:37 am »
I still recommend to everyone that no more than a simple pull up is required on the tach signal.
That is perfectly fine, and I understand your reasons for that.  I do not want or intend to change your opinion on that.

Thing is, on the electronics side, I am still just a hobbyist with not enough practical experience.  I have no reference or yard stick to compare to, to estimate what manufacturers can be expected to do.  Therefore, I have to err on the side of caution myself, especially since I intend to use not just the "high-end" Noctua fans, but also the cheap ones too.

What is up to argument, is whether I should have expressed the above qualifications with my suggestion.  I probably should have.
 
The following users thanked this post: eugene


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf