# Switching regulator using MCUs

One of the most useful ability of MCUs is generating PWM signal. For example ESP32 duty resolution can be lowered down to 1 bit in which case the maximum frequency is 40 MHz.

With such fast clock speed a 200-500KHz and 10-12 bit resolution is more than enough to build a switching buck or boost converter:

simulate this circuit – Schematic created using CircuitLab

• Is there a down-side in using of MCUs PWM to build a switching regulator? (am I going to face any problem?)

• What advantages switching regulator ICs (like LM2596 or XL6009 etc) have over this method other than simplicity and ease of use?

• One downside is that if your software fails it could damage the circuit board. – user253751 Dec 23 '19 at 14:12
• One of the most useful ability of MCUs is generating PWM signal. I think most MCU's are implemented for way different reasons than providing PWM. – Huisman Dec 23 '19 at 14:23
• @user253751 - that's true for a lot of MCUs all of the time (e.g. incorrectly setting PLLs). You are correct that this adds an additional failure mode however. – TLW Dec 24 '19 at 19:43
• @Huisman It is a useful ability of MCUs, and depending on your application, it can be one of the most useful. – user253751 Dec 25 '19 at 21:28
• @Huisman I didn't "generalize" it saying it's the best feature, I said: one of the most useful ability of MCUs is... one of many abilities... – ElectronSurf Dec 25 '19 at 22:10

10 bits at 200kHz requires a 200MHz peripheral clock, and 12 bits at 0.5MHz requires a 2GHz peripheral clock unless you have a fancy enhanced resolution PWM peripheral.

So lower PWM frequency means more expensive, heavier larger inductor. It’s difficult to get the MCU to regulate its own power. “Soft” firmware disruptions or bugs can cause physical damage.

Some processor bandwidth and resources are consumed by the (interrupt driven) control loop unless your chip has a dedicated processor for the purpose. That may increase latency for servicing lower priority interrupts or compromise the regulator performance.

Microchip has marketed versions of their PICs with peripherals optimized for SMPS control.. if Olin was still here he could tell you a lot more about actually using them, personally I tend to err on the conservative side.

There are also some small very low power MCUs that actually contain an entire switching regulator (except the inductor) to allow 1.5V operation.

There are a lot of similarities between a motor controller implemented by a DSP or microcontroller and a switching power supply so the existing peripherals often aimed at motor control could be used for a SMPS (although the frequency would typically be very low compared to a modern SMPS chip). Might be useful for special applications such as a polyphase low EMI power supply.

• ST and TI also have ARMs targetted towards the same thing. What they all have in common is ps resolution PWM modules and built-in fast comparators. – DKNguyen Dec 23 '19 at 14:32
• I actually consulted with a company that was using a DSP chip in the control loop of a super-duper efficient switching converter. I think it was converting tens of kW. So it can be done. IIRC, the DSP chip itself was powered by a separate itty bitty switcher, though. – TimWescott Dec 21 '20 at 4:06

Some points:

• An MCU definitely requires a gate driver and power transistors (your examples have built in power transistors, though some SMPS controller may need a driver and external transistors as well)
• A switching regulator IC is specifically built and tailored for the tasks, whereas a MCU in general is built to be able to do a lot of other tasks as well.
• An MCU requires code and code can contain bugs, deadlocks, etc.
• Using an MCU allows you to tailor the SMPS to your task despite being a general purpose device not tailored to the task. – DKNguyen Dec 23 '19 at 14:35
• - One important issue comes to mind, but I post it as comment because I cannot prove it (yet): Regarding safety regulations, it is way harder to prove software is safe than to prove a mechanical system or pure analog electrical system is safe. – Huisman Dec 23 '19 at 14:49

The main issue is you have to make your control loop run fast enough with low latency but it has been done. Look up digitally controlled/programmed regulators.

Variability in execution time from loop to loop will also cause harmonics and noise. It's less of a problem for something executed in hardware like an FPGA or ASIC since every cycle takes the same amount of time and is more deterministic cycle-to-cycle. They also react faster (less latency) to external events and more predictably compared to software.

I would not recommend using a microcontroller to make a regulated power supply for the reasons outlined in other answers for small scale production. The time cost to make resilient software will not compensate for any reduction in BOM costs. This situation may change if you're making lots of a product (as things often do).

However, it can be useful to use the PWM output to generate a low current unregulated auxiliary supply. For example, you might want a negative rail for biasing, or a high voltage for neon displays etc.

This is still prone to software errors but without a control loop the complexity is significantly reduced, and you can generate the supply in the background after you've set up the PWM system and leave the microcontroller's cycles free for other uses.

Is there a down-side in using of MCUs PWM to build a switching regulator? (am I going to face any problem?)

You now have a bootstrapping problem. Most of the time you'd power your MCU... from a switching regulator.

So either you have a separate regulator for the MCU (in which case why not just use that regulator directly instead?), or you have dedicated circuitry for bootstrapping (in which case why not use that circuitry always?).

You also now have a regulation quality issue. The control loop of output voltage -> ADC -> MCU code -> DAC -> mosfet is slow. Yes, the DAC may be able to switch at 80Mhz, but the time to do a reasonably-accurate ADC measurement, process the output in software, and re-program the DAC duty cycle in response will be much slower on a typical MCU.

This all being said, this can be a valid approach in certain cases. The above kind of hints to when it can be useful:

If you already have an MCU, and you're trying to add a new voltage rail that can be powered on later, and it's not easy to generate the new voltage rail from an existing one, and regulation quality isn't too much of an issue... then yes, this can be useful.

I tried and realized that it's actually quite complicated.

To generate a fast PWM signal is easy, but due to the capacitance of MOSFET gate, the faster you drive the MOSFET, the higher the current you will need, the GPIO pins of MCU can only sink/source ~ 30 mA, which can only drive power MOSFET to about 10kHz or less. You will need large inductor when switching at <10kHz and likely not efficient. Modern cheap switching regulator can do 1MHz+ and only cost $0.2 with MOSFET built-in. Another issue is the gateway voltage required to drive the power MOSFET, most MOSFET need 10V+, so you can't use GPIO pins directly, you will need another MOSFET or a transistor, and 2 resistors. Now you are using more parts than a switching regulator, less efficient and probably cost more. • Or a single gate driver. – Hearth Dec 21 '20 at 3:51 • Have you tried using a mosfet driver? – ElectronSurf Dec 21 '20 at 7:57 You can use MCU as PWM controller but then you will need MOSFET driver. Now question is why would you go difficult way when there is time proved approach -- specialized ICs? Add a few components and you have time tested solution at minimal cost lower than cost of MCU! https://www.ebay.com/itm/MP1584EN-Ultra-Small-DC-DC-3A-Power-Step-Down-Adjustable-Module-Buck-Converter/133251283426 https://www.ebay.com/itm/10-PCS-Mini-MP1584EN-DC-DC-Buck-Converter-3A-Power-Adjustable-Step-Down-Mod-D4M3/263829265998 I wonder can you beat$0.74US with a microcontroller? Then you should try.