Pi 25KHz PWM Fan Driver - Page 1
Forum section: electronics / beginners

author: jewelie
author_rank: Regular Contributor
timestamp: August 06, 2020, 10:39:02 pm
content: Hi allMy goals:I want to have go at making high-frequency (25Khz) PWM fan driver for my Pi.To drive generic 5V 200mA 5V 2-pin BLDC fans (the type with an integrated controller.)I'm trying to make it with what I have rather than buying more stuff, if possible.The Pi in question works as a 24/7 multi-purpose server and I want to be able to pretty much build, test and forget it, fairly safelyBackgroundThere are lots of other Pi fan PWM driver circuits out there but they all seem to be low frequency PWM drivers without filtering etc, which doesn't seem optimal because:Low freq PWM = noisy fan.Unsurprisingly, the fan tends to try to synchronise with harmonics of the PWM speed (although that was quite fun to watch happening via the noise on the signal via my old CRT 'scope -- once I realised what that odd 'noise' on top of the signal was!)It would seem to be bad practise to be turning the power on and off like that, given the fan isn't passive and has a driver IC?For my first attempt I tried using a 2N3904 + 2N3906 pair to push-pull the inputs to a pair of BS170 MOSFETSs wired in parallel (to power share.) The driving signal as viewed on the oscilloscope seemed okay, but it turned out the fan itself didn't like being driven by a pure PWM signal above about about 1KHz.I tried adding a capacitor to try to filter the PWM output into DC, but that didn't work and I ended up with a very non-linear output, somewhat spoiling the purpose of a PWM signal. I now gather I need to use a DC-to-DC converter buck converter if I want to properly turn the PWM output into a DC voltage.I also discovered I could probably use just a single P2N2222A (the TO-92 plastic version of the 2N2222A)... which I it turns out I already had some of.SO! I've put together the attached circuit below on LTSpiceXVII and simulated it, and it looks like it ought to work.Circuit Notes:According to the simulations on LTSpice, everything seems to be well within the power, voltage and current specifications of components (except where I've stated otherwise in the questions below.)The inductance and capacitance values for the fan blob are just guesses for inductance and capacitance values, so I can vary them and see what effect they have on the circuit.Lbuck is a small inductor I hand-made for something else entirely (can't remember exactly what for) that measures approx 6mH according to my ATMega328P transistor tester, but I don't know for certain it's really 6mH.My questions:Does this circuit look reasonable?Is it worth giving a build and test on a breadboard yet?Is it safe to use an electrolytic capacitor in the buck converter like this, with the potentially partly inductive load?Is it okay to use the 1N4148 signal diode in this config, or do I need to accept using another more meaty and fast diode of some kind? (I've got 1N4007s, but would they be fast enough?)Are simulated instantaneous over-spec power peaks/transients something to worry about, e.g. in transistors, when the average power dissipation and voltage/current are well within specs? (Although this particular variant of circuit doesn't seem like it should have any, some of my previous circuit simulations did, hence the question.)The simulation suggests brief 1ms 1.2A current inrush and outrush transients when the 5V supply is turned on or off (but fine with the fan etc is turned on or off though.) Is this likely to be okay in reality?Is it likely safe, as in, unlikely to damage components or have a minor Chernobyl meltdown or set the Pi on fire? (it'll be running 24/7 but all components are proper CPC Farnell sourced bits bar the resistor and the handmade inductor.)Are there any obvious improvements I can make (without spending too much money -- preferably using something I might already have!)Thoughts?I can happily add some views of the LTSpice simulations (or real scope photos) if that would help?Warm regardsJulie

author: ledtester
author_rank: Super Contributor
timestamp: August 07, 2020, 01:06:06 am
content: At least for testing (and probably in production) I'd run the fans off a separate power supply.I'd also use a MOSFET instead of the 2N2222 -- they are meant for these kinds of applications and are less demanding on the Pi (in terms of current needed).(Just read that the Pi can supply at most 50 mA on its GPIO pins with a max of 16 mA per pin.)

author: jewelie
author_rank: Regular Contributor
timestamp: August 07, 2020, 02:28:58 pm
content: Thanks.Agreed about using external power perhaps, that solves a few ills. Might still use the Pi 5V when I make it properly, as I'm using an official Pi PSU, depending on what the load and transients look like in real life.Also agreed about reducing the Pi pin current draw. (Thinking about it, I'm actually already using the 3.3V power for something else too, and the 50mA limit applies to that also, so I might be better off using the 5V for that as well and level-shifting.)I've not got any MOSFETs that are suitable to just replace the 2N2222A as is, even the BS170's I have aren't properly on at 3.3V. So it sounds like it may be sensible for me to revert to the idea of 2N3904+2N3906 push-pull driving a pair of parallel BS170s?

author: ledtester
author_rank: Super Contributor
timestamp: August 08, 2020, 01:52:54 am
content: Quote from: jewelie on August 07, 2020, 02:28:58 pmThe BS170 has an RDS(on) of 5 ohms, which is really high for a switching MOSFET.There are an ample selection of better suited ones -- ones that admit at least 1A at a V_GS of 3V and have R_DS(on) in the < 100 milliohm range. You might have to go with an SMT part, like this one:https://www.mouser.com/datasheet/2/427/si2302cds-1764899.pdfThe first graph on page 3 of the datasheet will tell you at a V_GS of 3.3V it is capable of passing multiple amps.

author: jewelie
author_rank: Regular Contributor
timestamp: August 08, 2020, 03:03:44 am
content: Unfortunately I can't really afford to buy more components at the moment, hence the one of my listed goals being to use what I've already got.But... I did have a look through what I've already got, from back when I bought bits from a list of recommended items to have, which includes-https://uk.farnell.com/stmicroelectronics/stp16nf06l/mosfet-n-ch-60v-16a-to220/dp/9803068This looks suitable, to my eyes?

author: Whales
author_rank: Super Contributor
timestamp: August 08, 2020, 03:43:50 am
content: QuoteAlways!Build first, ask questions later. You can never make a perfect design on paper, real life is what matters.QuoteYes. Always over-rate the voltages of your electrolytic caps just incase, but remember that your fan is already designed to run directly off a 5V supply without adding too much in the way of spikes.QuoteMeaty: depends on the current draw of the fan. Best thing to do is try it and measure.Speed: 1n400x series are probably too slow for this (~30us recovery time?).If your current diode doesn't work then pretty much any shottky would be a good replacement. Lower Vdrop and less recovery.QuoteYes and no. By their nature switchmode power supplies are supposed to have high pulses, you have to make sure your parts can handle these. Albiet sometimes sims show unrealistically high pulses for almost zero time, if you are unsure which are which then post a picture of your sim graphs.QuotePerfectly fine for almost any diode. Eg for the 1n400x series:QuoteYes this design has a chernobyl state. What happens if your Pi stops outputting PWM and instead outputs high constantly?(Albiet your PTC resistor may help avoid this, I've not looked at its specs VS the 2n2222)QuoteInfiniteDepends on what you have.Better drive transistor: a low Vgs(on) mosfet. Ordinary fets may actually perform worse than your 2n2222 BJT at these low voltages (3.3V from raspi), so make sure to do before and after comparisons if you use whatever you have at hand.Lazier designs: a linear power supply will use a lot less parts and be simpler, but is less power efficient and (more importantly) isn't necessarily as fun, depending on what you want.Higher frequency: find the optimum operating point to improve efficiency. Your Pi, transistor and other parts will have limits. 25KHz is quite low for switchmode supplies, these days many run in 100's of KHz to even MHz.More complex designs: whole control loop with feedback. Automatic shutoff. Using a pre-existing switchmode chip or designing it yourself. Digital signal control from pi instead of direct PWM control. Infinite bananas.

author: ledtester
author_rank: Super Contributor
timestamp: August 08, 2020, 04:41:51 am
content: Quote from: jewelie on August 08, 2020, 03:03:44 amLooks like it should work.

author: jewelie
author_rank: Regular Contributor
timestamp: August 16, 2020, 01:03:05 am
content: Quote from: Whales on August 08, 2020, 03:43:50 amThank you for the encouragement.I do need to push myself to take "sensible" risks more too, as I've also always been averse to breaking things and therefore I can be a bit tooooo cautious at times.I've been pushing myself a bit more more than usual the last few weeks and trying to worry a bit less about making relatively-safe mistakes; i.e. where I'm using circuits powered only by fairly low voltages and a low power sources and otherwise isolated from anything else. In those weeks I've accidentally turned a few diodes briefly into LEDs, and then learnt more about the common failure modes of diodes.I also blew the lower current/cheap fuse on my cheap multimeter (I wasn't daft enough to do the low current readings on the old Fluke I was given, so at least avoided paying out on the "stupid tax" on that at least... those fuses are quite a bit more expensive!)QuoteThank you! I've spent a bit more time learning about Shottky diodes now and understand why they're the best thing to use for this circuit (and now I know what the SS14 marking means on a number of SMD boards I have.)I've eyed getting a stack of 1N5822s for my box of general useful parts, which look like they'd be really useful both for this circuit and others (the 1N5822 doesn't seem to be awfully different at these sorts of currents and voltages to the 1N5817.)Unfortunately I'm going to have to wait until a I've got a decent sized order to put in, as the minimum orders without handling fees have gone up across the board around all the usual places: e.g. CPC which use to be £8 minimum spend with free delivery, now has a £5 handling fee for orders under £21.For now the 1N4148 looks like it ought to work and be within specifications, so I can still try and experiment with those until I've got some 1N5822s. It's less efficient, but seems like it should only be heavily inefficient at lower end of the PWM -- and I'll only be using 50% to 100% on the PWM (or, of course, 0%) as these fans aren't really supposed to be run below 3V and they won't start under 2V anyway.QuoteThis has had me flumoxed!Even with the PTC resistor bypassed, I can't work out what the Chernobyl state is!Assuming.... 0% or 100% PWM = DC, so I can ignore the capacitors, imagine the inductors shorted, then I'm just running the fan via the transistor in a DC steady state, 200mA is easily within what the PN2222A can handle -- it looks like it would waste about 30mW. Hmmmm.In the simulation I've assumed a 25ohm resistive load as an approximation of the fan, but as it's a black box I guess could well be much higher when stopping or starting?QuoteYeah, I had a really interesting time virtual-experimenting with various circuits trying to find ways to MOSFET this!QuoteThat might be fun to try at some point, I've not actually messed with linear supplies as yet!QuoteThanks again so much! So many possibilities.Next version of the design coming up in a post below!

author: jewelie
author_rank: Regular Contributor
timestamp: August 16, 2020, 02:21:12 am
content: Quote from: ledtester on August 08, 2020, 04:41:51 amSo here's a go at the circuit-But I'm not sure it's fast enough, at least with a GPIO pin current limiting resistor of 2K -However, the simulation data doesn't seem to reveal an issue though-Given that I ought to get some Schottky diodes in for the parts box at some point, perhaps I ought to get a more appropriate MOSFET then?Or should I just drive it with a BJT rather than GPIO pin directly?(I'm trying to avoid just getting a MOSFET driver IC, otherwise I'd not really learn anything!)

author: jewelie
author_rank: Regular Contributor
timestamp: August 16, 2020, 04:24:56 am
content: Or this monster with a 2n3904 level shifting then 2n3904/2n3906 push-pull voltage follower driving the mosfet?I haven't simulated this in detail, it's far outside of my comfort zone, probably has some gotchas, and I'm not sure if I can use a pullup on the Pi GPIO like that, but the waveform looks pretty darned square?

author: magic
author_rank: Super Contributor
timestamp: August 16, 2020, 05:27:30 am
content: What about going back to 2N2222 and using a 2N3904 or 2N3906 to generate its base current and relieve the GPIO?BTW, in your configuration, the common mode voltage of the fan and its cables jumps up and down with the PWM waveform and will probably emit some amount of unnecessary noise/EMI. Consider switching on the high side with a PMOS/2N2907 (like in typical buck converters) or moving the coil to the low side.

author: Moter12
author_rank: Newbie
timestamp: August 16, 2020, 05:29:40 am
content: Nice Information.breakup shayari

author: Whales
author_rank: Super Contributor
timestamp: August 16, 2020, 06:30:19 am
content: Quote from: jewelie on August 16, 2020, 01:03:05 amYou're right, your circuit is fine. My apologies for sending you in circles. *blames someone else for chernobyl*

author: Whales
author_rank: Super Contributor
timestamp: August 16, 2020, 06:39:59 am
content: Quote from: jewelie on August 16, 2020, 04:24:56 amThat will indeed drive the fet a lot better.~4.8V of gate voltage will be too low for many fets, but it looks like you have chosen a low-threshold fet to avoid this problem.Youmightbe able to pullup to 5V on the Pi GPIO like that. Whilst the current into the Pi's GPIO pins will be limited, it may still lead to latch-up or damage, depending on what process the Pi's chip was made on & how they designed the IO drivers. I remember trying to look this up a while (year?) ago and not finding any solid information, so I'm not sure. A second stage of common-emitter amplifier will invert the signal again so you can use a pull-down resistor instead.The squareness of your switching waveformmightnow probably be dominated by the speed of your BJTs exiting saturation. Alas there could be a whole bunch of other things hitting their speed limits before then (including the Pi outputs), I'm not sure.EDIT: jewelie: please upload your images as attachments to your posts. All external image hosts eventually die, leaving forums full of topics with broken images a few years later. This heavily reduces the value of topics to any new readers. You don't have to change your <img> tags or anything, just attaching the images (as backups for readers) is more than enough.