3

I have a Arduino which I would like to use to control a 12v PWM fan for speed control.

I know about the 0-3.3v to 0-5v logic level shifter, is there a similar option for 0-5v to 0-12v? Or is there a circuit using a IC that I need to make?

Thanks in advance for your help.

  • 1
    It your on about a 4 wire PC fan the control signal is ttl compatible not 12v. – Majenko Oct 12 '20 at 7:56
  • 1
    This unfortunately isn't for a PC fan, this for a large blower unit for air circulation. Thanks though – MrEditor97 Oct 12 '20 at 8:53
  • 1
    @MrEditor97 For future... If you ask some question that is not in common measurements like "yours big blower", You have to mention it in your question that your project is about large machine with higher power comsuption, otherwise do not expect relevant anwser – Sahasrar Oct 12 '20 at 21:04
  • 2
    Cheers, I will take your advice on board if I have another question! Thanks. I didn't have the serial number of the fan until today, now I have that I have checked the spec, and a low powered option is suitable as it only requires a PWM signal to control the speed the rest is done with its on-board controller - no more than a 0.5mA signal is required. So I believe a transistor method is suitable for this application – MrEditor97 Oct 13 '20 at 8:48
  • 1
    @copper.hat, see his latest comment just above yours. He states he's driving the PWM input only, so low-power, low-current PWM signals to an already-existing motor driver is all he needs it turns out. He's not driving the motor directly. – Gabriel Staples Oct 13 '20 at 16:18
6

I advice you to learn about basics of how to connect external components to MCU. Anwser to your question is simple transistor amplifier... So you can use bipolar transistor (NPN/PNP) or MOSFET... MOSFETs is better for switching like your case, switching via PWM.

If you switching inductive load with MOSFET like motors or coils, don't forget add antiparallel flyback diode. Otherwise your MOSFET will burn out. How to connect mosfet to arduino pin

If you need switching with higher frequency or load with more voltage or power I advice to use push-pull logic to drive mosfet gate.

Something like this:

enter image description here

  • 2
    If I was to use a MOSFET, is there a specific type you can recommend? Thanks for your helpful answer! – MrEditor97 Oct 12 '20 at 8:54
  • 2
    I use very often IRF540, but try to lookup logic-level MOSFET, It is very handy because gate voltage is 5V so it is compatible with MCUs... logic-level MOSFET are whole sieres of IRL mosfets... Take a look at this link circuitcrush.com/arduino/2017/06/02/… – Sahasrar Oct 12 '20 at 9:35
  • For lower currents (up to several amps vs dozens of amps), NPN BJT transistors, especially Darlington transistors such as the TIP120 NPN BJT 5 Amp continuous one here, are easier to use and drive than MOSFETs. – Gabriel Staples Oct 13 '20 at 4:41
  • How does this answer relate to driving a PWM input on a fan? The OP is not trying to drive the fan motor, just the PWM input. – copper.hat Oct 13 '20 at 16:30
  • 1
    A good design with a gate recharge/discharge circuitry. Have my upvote. – user69410 Oct 13 '20 at 17:44
2

As far as I can tell the Arduino does not have open collector outputs, so you can mimic one as follows. The following inverts the Arduino output signal, so some adjustment is needed there when generating the PWM signal.

Excuse the rather gross schematic. I can't find any scaling parameters.

The particular resistors values are not terribly important, mainly to limit current.

schematic

simulate this circuit – Schematic created using CircuitLab

  • This would totally work, but has the downside of being inverting, and the even bigger downside of passively pulling the output up to 12V through a large R1 resistor, which results in a very weak, rounded edge and slow rise-time. This limits the possible PWM frequency a lot. So, you have active drive to LOW, but passive pull up to HIGH. To solve this problem you have to add enough transistors that in the end you create a half H-bridge, which led me to my "TLDR" answer here: arduino.stackexchange.com/a/78569/7727: just buy a motor driver H-bridge from my "Option 2" section and be done. – Gabriel Staples Oct 13 '20 at 18:01
  • 1
    He is driving a fan not a Tesla. The stray capacitances here are negligible, you don't need microwave frequencies for fan PAM. – copper.hat Oct 13 '20 at 18:12
1

Use MAX232

Advantages of MAX232:

  • It has low rise/fall times. A good PWM needs to be sharp. Since MAX232 is designed for serial port communications, i.e., TTL to RS232 and RS232 to TTL, it has to perform reliably. Reliability ensures low rise/fall times.

  • It operates at low currents. The OP suggested that the fan only needs a PWM to drive, i.e., it has its internal current amplifiers.

  • It is cheap. Engineering solutions require to be no more expensive than it has to be, and MAX232 is half the price of L298.

Shortcomings of MAX232:

  • Since the computer serial port is used much less, the primary use of MAX232 is over. Therefore, it is not in all hobby kits and it might be harder to find MAX232. Mostly older engineers who used to play with PIC16F877A before ardunios become popular remembers such ICs.

Shortcomings of L298:

  • L298 is a current amplifier, not a voltage booster. Using it for a purpose other than its primary purpose is suboptimal.

  • L298 has a huge current output. It might exceed the threshold currents of the fan, and cause malfunction. It is very unwise to connect a 1A rated L298 to 0.5 mA rated signal pin.

  • L298 has a weird pin diagram. It has a ZIP package rather than the regular DIP to allow attachment of heatsinks, which increases the size of the circuit.

  • L298 has high rise/fall times. Its purpose is to generate large currents, not PWMs. Therefore, its rated rise/fall times are not only high, but also unreliable.

  • L298 requires four high current rated Schottky diodes. The cost of these diodes will be more than that of the L298 and they are very large, resulting in a much larger circuit area.

Do you really want to use an H-Bridge? Use L293D.

  • L293D is still a current amplifier but not an overkill like L298. Its current output is four times as low as L298 so chance of malfunction is reduced.

  • L293D has a DIP layout. Most hobby electronics application uses DIP sockets to mount the ICs later on and you can do it with L293D.

  • L293D has the flybacks diodes internally. So you neither need to buy nor place them on your circuit.

Do you want to design your own circuit? Use a gate/base recharge/discharge transistor.

Basically, all transistors have a gate/base capacitor which needs to be charged before the transistor switches to ON and discharged before it switches to OFF. Power amplifiers normally do not care about the rise/fall times, with a notable exception that comes to my mind is musical amplifiers.

What about a discharge resistor?

Replacing discharge transistor with a simple resistor works but it would increase your drain from the PWM source. It has a simplicity vs performance trade-off.

Commenting about relays

Relays usually have a higher switching time because their input module consists of a diode and output module consists of a transistor, i.e., you have add their reaction times together. This results in distortion in the PWM output. Furthermore, there is no need to use SSR in a low power operation, an opto-isolater would suffice.

Final comment about designing circuits

Circuits have many trade-offs. Cost, availability, complexity, size, performance are just to name a few. A good design needs to take everything into account. To rate my above suggestions:

  • MAX232: Cheap, simple, small, good performance, not available
  • L298: Expensive, complex, large, low performance, available
  • L293D: Cheap, simple, small, mediocre performance, available
  • Simple transistor: Cheap, simple, small, mediocre performance, available
  • Modified transistor with base recharge/discharge: Expensive, complex, large, good performance, available

So, I would rate L298 as the worst possible solution that actually works.

  • 1
    A few minor corrections are needed to make this answer upvotable: 1) BJT base has insignificant capacitance (but MOSFET Gate capacitance is significant). 2) PWM is still required when driving motors through an H-bridge or half-H-bridge, such as through an L293D or L298 motor driver, if you want throttling, as PWM is what throttles the motor to vary its speed, 3) amplification in motor drives refers to amplifying current, not voltage (voltage "amplification" is generally called "level shifting"). – Gabriel Staples Oct 13 '20 at 16:43
  • Also, to answer your question: I am not sure about this but you might not even have to use diodes of the L298 since your output voltage will always be unidirectional (or positive.) Yes: flyback diodes are always needed for any inductive load, to prevent the voltage spike which would otherwise occur any moment you turn off the load and the electromagnetic storage energy in the inductor collapses and drives a voltage spike. When throttling a motor, this occurs every PWM cycle, and when using pure ON with 100% duty cycle (therefore, no PWM), this voltage spike will still occur... – Gabriel Staples Oct 13 '20 at 16:50
  • ...when you turn OFF The motor. – Gabriel Staples Oct 13 '20 at 16:50
  • 1
    @GabrielStaples I hope my edits answer your concerns. – user69410 Oct 13 '20 at 17:25
0

You'd usually use a transistor to do that.

schematic

simulate this circuit – Schematic created using CircuitLab

SW1 is your microcontroller creating the PWM signal.

  • 2
    use a low side switch for a more reliable transistor turn on – jsotola Oct 12 '20 at 7:33
  • 1
    Unless the output power is to be used to drive something, it is a bad idea to use BJT. MOS is way more reliable with lower gate current and gate capacitance. – user69410 Oct 13 '20 at 13:27
  • 1
    Unless the output power is to be used to drive something, it is a bad idea to use BJT. MOS is way more reliable with lower gate current and gate capacitance. This is exactly the opposite of the truth. BJT doesn't have a Gate, nor does it have Gate capacitance. It has a Base, and the Base capacitance is so insignificant it isn't even mentioned on a BJT datasheet. MOSFETs, however, have very large Gate capacitances on their datasheets. The tradeoff is power and current. BJTs drive up to ~5A max (ex: TIP120) for the same physical size MOSFET which could drive 60A (ex: IRLB8721). – Gabriel Staples Oct 13 '20 at 17:07
  • 1
    @C.Koca, this is getting really painful. MOSFETs are typically used low power logic circuits? True, in part I'm sure because they don't bleed current. They are voltage-controlled, not current-controlled, so they don't require continual current flow to stay on, unlike BJTs. Because MOSFETs allow faster switching. I'm not sure this is true. hence lower gate capacitance very false. and lower power very true, because they (MOSFETs) are voltage-controlled, not current-controlled (BJTs are current-controlled, and hence continually bleed power just to stay on). – Gabriel Staples Oct 13 '20 at 20:52
  • 2
    @C.Koca ... so many wrong things in what you continuously post. Your arguments are too accademic, the suggested circuit in the npn/nmos version is just fine, at least in this application! – Sim Son Oct 14 '20 at 20:10
0

Note: my entire answer below assumes you need to drive a 2-wire brushed DC motor (or similar load, such as an audio speaker) directly, with real power and real current. That is what my entire answer is based on. However, after I wrote my answer, the OP posted this in a comment under his question (emphasis added):

I didn't have the serial number of the fan until today, now I have that I have checked the spec, and a low powered option is suitable as it only requires a PWM signal to control the speed the rest is done with its on-board controller - no more than a 0.5mA signal is required.

So, although my answers below will still work to provide an output PWM at, lets say, 12V, from an input PWM at 5V, they are intended to drive a 2-wire brushed DC motor directly with real power and real current. The fact that they can also provide an output PWM signal at 12V is incidental, but just as applicable.

Also, it is important to note that all my answers below are expected to be usable up to a few dozen kHz max PWM frequency, unless otherwise stated on the product datasheets--Ex: many high-end Pololu brand motor drivers (H-bridges) I recommend below can be driven with up to 100kHz PWM frequency since they use such high-quality and fast MOSFET Gate driver circuits with both high-power active Gate drive HIGH and active, high-power Gate drive LOW in their H-bridges.


TLDR;

Jump straight down to the section titled "Here's some H-bridges you can buy", in the "Option 2" section below. Buy one of those and be done.

Reminder: on any H-bridge, when you drive a PWM to the input at 5V with a certain duty cycle and frequency, an equivalent or almost-equivalent PWM output at voltage level Vsupply will occur on the H-bridge output. This is the exact purpose of the half-H-bridge. And, this, in addition to allowing bi-directionality of power, current, and voltage, is the exact purpose of the full H-bridge. PWM output freq from the H-bridge will be exactly identical to the input PWM, and PWM duty cycle will be almost identical, with output PWM waveform distortion increasing as PWM frequency is increased.

Option 1 of 3: [custom circuitry] The Electrical Engineer / "very curious hobbyist with lots of time" answer:

I've upvoted @Sahasrar's answer. If you haven't taken a look at it, you should. It fits squarely into this "Option 1" category.

However, in his first image, when controlling pin D7, the MOSFET gate is actively driven both HIGH and LOW with a max (initial) current of I = V/R = 5V / 100 Ohms = 0.05A = 50mA, which is good, but kind of weak (a good MOSFET gate driver is more like 500~1000mA drive current). This is also exceeding the 40mA max current rating of the Arduino pin, so I recommend you choose a slightly bigger resistor. The resistor here is to limit current to not damage your Arduino pin when charging and discharging the Gate capacitance of the MOSFET, which capacitance is significant enough for MOSFETS (but NOT BJT transistors), that it is even listed as a parameter in a MOSFET transistor datasheet (but not in a BJT transistor datasheet). WithOUT this Gate resistor, each time you drive the Gate HIGH or LOW, you'd otherwise have the equivalent of a momentary instantaneous short through the Arduino pin, which could damage the pin.

Also, you have to be sure to use a Logic-Level N-Channel MOSFET which can be driven with a gate voltage as low as 3.3V~5V instead of requiring more like 10V~12V.

So, with @Sahasrar's first circuit, the max PWM frequency you can use is probably a few dozen kHz.

If you need to drive higher PWM frequencies, the solution is to use the push-pull logic circuit which @Sahasrar shows next instead. But, you have to fill in the blanks: choosing parts, doing calculations, having advanced knowledge.

In both cases, you must also be sure to use a flyback diode to snub inductance-induced voltage spikes!

Option 2 of 3: [just buy an H-bridge DC motor driver] The normal hobbyist / time-constrained-individual answer:

So, the quick solution is to just buy a motor driver instead and be done!

You can use any H-bridge for a 1-channel (1 device) bi-directional drive, or for a 2-channel (2 separate devices) uni-directional drive.

Or, you can use a half H-bridge for a 1-channel uni-directional drive.

H-bridges are frequently called "brushed motor drivers". They handle all the fancy Electrical Engineering circuitry for you.

H-bridges are excellent for driving things like:

Here's some H-bridges you can buy:

When searching for these things, recommended search terms include "DC motor driver" or "h bridge". Even if you plan to drive an LED or speaker with it, these are still the correct search terms and parts. Just make sure what you buy accepts PWM input as the control signal is all, and that this PWM input means true PWM, NOT a servo "PWM" signal, which is very different.

Just buy one of these and be done:

  1. Cheap $1 L9110S H-bridge:
    1. Ebay search for "arduino h bridge"
    2. L9110 datasheet
      1. 2.5V to 12V supply
      2. 800mA max continuous current per channel
  2. Cheap $3 L298N H-bridge (much more powerful):
    1. Ebay search for "arduino h bridge L298"
    2. Example specs sheet: http://www.handsontec.com/dataspecs/L298N%20Motor%20Driver.pdf
      1. 3.2 to 40V supply
      2. 2A peak current
      3. 20W max power
  3. High-end, reliable, very-well-engineered DC motor drivers/H-bridges by Pololu robotics company:
    1. See the full list of motor drivers here! https://www.pololu.com/category/11/brushed-dc-motor-drivers
    2. Ex: $40 Pololu G2 High-Power Motor Driver 18v25
      1. 6.5V to 30V supply
      2. 1.8V, 3.3V, or 5V logic signals
      3. 25A max continuous current
      4. up to 100kHz PWM operation, because they have excellent MOSFET gate drivers to minimize MOSFET time in the ohmic region, and therefore MOSFET heating!
      5. If you're in a hurry and want high-quality, Pololu is an excellent choice!
      6. Tiny! 1.3" x 0.8", and no heat sink required, again, because of their excellent MOSFETS and MOSFET gate driver circuits.

Option 3 of 3: [Radio Control brushed ESC (Electronic Speed Controller)] (easiest of them all!)--preferred by people with RC vehicle experience and/or who need really high power

Important: since this option does NOT give you the low-level control over the PWM output directly, these controllers can NOT drive speakers for audio, whereas Option 1 and Option 2 drivers above can!

I'd be remissed if I didn't include this answer as well, since this is one of my specialties. This is the easiest by far! Its only downside is it gives you less fine-tuned control than controlling the low-level PWM output to the motors directly, as you can do with the motor drivers above, versus the motor controllers below. Another advantage of the hobbyist RC brushed ESCs below over the Pololu-type robotics motor drivers above is power and current: the above motor drivers from Pololu peak out at 25A continuous, for instance, whereas some RC brushed motor controllers, some less-powerful examples of which are shown below, can drive as much as 100~200A continuous, which is HUGE.

Just buy a Radio Control (RC) brushed ESC and feed it a servo PWM signal via the Arduino servo library, NOT a true PWM signal with analogWrite()!

You give it a servo signal, and it generates the low-level PWM to the motor automatically using its internal microcontroller and MOSFET driver circuitry, usually on the order of 8kHz~16kHz PWM output frequency. Many of these types of ESCs use the ATmega168 mcu internally.

For a single-direction ESC, such as for RC airplanes, a microsecond servo value between 700~1300us is 0% throttle, and a microsecond servo value between 1700~2100us is 100% throttle.

For a dual-direction ESC, such as for RC cars, with both forward and reverse, a microsecond value of ~1500us is 0% throttle, with ~2000us or so being 100% forward throttle and ~1000us or so being 100% reverse throttle.

Sample code:

#include <Servo.h> 

Servo brushed_motor;

void setup() 
{ 
  brushed_motor.attach(9); // pin 9

  // ~0% throttle (0% output PWM duty cycle), depending on ESC 
  // calibration, and assuming a forward-only ESC for RC airplanes
  brushed_motor.writeMicroseconds(1000); 

  delay(2000);
  
  // ~100% throttle (100% output PWM duty cycle), depending on ESC
  // calibration, and assuming a forward-only ESC for RC airplanes
  brushed_motor.writeMicroseconds(2000); 
} 

void loop() 
{
    // whatever you need here
} 

To calibrate one of these ESCs to whatever throttle values you want, just set it to 2000us (full throttle) BEFORE you turn it on (careful--just in case it decides to go full throttle instead), then power it up, and it will register that as "full throttle". Next, withOUT powering it off, set it to 1000us (0% throttle), and it will register that as 0% throttle. Now, it is calibrated to have 1000us be 0% throttle and 2000us to be 100% throttle. If using an RC car brushed ESC instead of an RC airplane brushed ESC, you may have to play with it, do some research, and read its manual to get what you want, since 1500us might be considered 0% throttle, with 2000us being 100% forward throttle and 1000us being 100% reverse throttle. So, do the research.

In either case, here's some brushed RC ESCs which can drive motors and blowers or whatever just fine too.

These are just a couple examples. RC ESCs like these are designed to be really high power for high-end RC vehicles which can go up to 25~100mph sometimes, and be quite large. Do some research. Brushed RC ESCs are sold in many places. HobbyKing has the best prices in the industry for these types of things, with generally good to very-good quality parts.

Related:

  1. [my answer] Switching a Solenoid Using Arduino's 5V Output?
    1. Here's my answer to another question which answers your question. In the diagram below, from this other answer, the relay + R2 is the load. Replace this load (relay + R2) with your motor as the load instead, and this is also a perfectly legitimate circuit to solve your problem. The 5V here in this circuit to the transistor base would be replaced by your 5V logic level PWM signal coming from your Arduino. I also go over calculations and how to size all components. Try a high-current TIP120 NPN BJT 5 Amp continuous transistor in place of the 2N3904 in my circuit, and be sure to redo the calculations to size your base resistor, R1.
    2. Note that this circuit allows your Arduino pin to actively drive the output both HIGH and LOW, but since its through this (relatively small) base resistor, it's probably only good for PWM frequencies up to a few dozen kHz or so. An oscilloscope used to look at the output would make this clear. Increase input PWM frequency until output PWM waveform distortion becomes significant.
    3. enter image description here
  • 1
    H bridge is a fancy word for couple of BJTs. You have an RC circuit at the base with a mediocre performing base discharge. Just check Fig 3. st.com/resource/en/datasheet/l298.pdf and check the rise fall times. It is about 4 times the ones of MAX232. For low frequency, it wouldn't make much difference but for high frequency PWM switching it would. For 10 kHz, your design will be off by 3%. For 100 kHz, it would be off by 30%. Besides, the comment suggests the fan itself has its current amplifier so L298 output would properly exceed the input current. – user69410 Oct 13 '20 at 14:56
  • 1
    H bridge is a fancy word for couple of BJTs. No, "H-bridge" is a very specific word to mean 4 transistors arranged in two complementary high-side + low-side pairs, allowing dual-driving 1 bidirectional 2-wire motor or speaker, or 2 separate unidirectional 2-wire motors or two single-driven speakers. An H-bridge can be designed with either MOSFETs or BJTs, so it implies nothing about being made with BJTs. Motors are driven usually with 8kHz to 16Khz PWM frequencies, rarely ever above 100kHz due to H-bridge limitations and motor inefficiencies/losses which increase with PWM frequency. – Gabriel Staples Oct 13 '20 at 15:08
  • 1
    H-bridge is a just four transistors arranged in two Darlingtons. When you have amplifier transistors but not base discharge transistors, you will face distortions in the voltage waveform. That 3% doesn't even include the results of heating. Do you know how MAX232 achieves such a result? Because it has discharge capacitors and lower base capacitance. I know what L298 does, it is in my answer as well. But it will not give a PWM output. – user69410 Oct 13 '20 at 15:14
  • 1
    @C.Koca, please quit commenting. Nearly every single comment you leave is at least partially incorrect, and you're sowing misinformation wherever you go. You say some truth, some misinformation, some truth, some misinformation, and are seriously confusing anyone reading your comments. Here's a Darlington: en.wikipedia.org/wiki/Darlington_transistor. It is a BJT which feeds the base of a BJT. Here's an H bridge: build-electronic-circuits.com/h-bridge. It has zero BJT Darlington pairs in it. And, an H-bridge can be made of MOSFETs OR BJTs. A Darlington refers to BJTs. – Gabriel Staples Oct 13 '20 at 15:27
  • 1
    You got me at Darlington but do you want to go personal? Alright, you don't even know about base discharge, slew rate and PWM and still trying to push L298 for a low power operation. Have you checked the comment by OP: and a low powered option is suitable as it only requires a PWM signal to control the speed the rest is done with its on-board controller - no more than a 0.5mA signal is required. H bridge do not have discharge transistor. Do you know what a discharge transistor? It is a PNP for an NPN or vice versa that only becomes ON when the power transistor is OFF. – user69410 Oct 13 '20 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.