Sensible to use a MCU for a switching (or even a linear) power supply? - Page 1
Forum section: electronics / general

author: peter-h
author_rank: Super Contributor
timestamp: July 30, 2025, 10:40:38 am
content: One could obviously implement a switcher, using a PWM output, an ADC, and a PID control loop in software.One could also implement a linear PSU with an ADC, DAC, and a PID loop in software.But you need to be pretty sure the thing won't crashHow often is this done? Do e.g. PC motherboards do it, for the feature where you can select the CPU VCC in the BIOS?Note I am not talking about using a DAC or GPIO to select the output voltage across a predefined range. That is better because the regulation is still done in hardware.

author: hfleming
author_rank: Regular Contributor
timestamp: July 30, 2025, 10:54:00 am
content: In a few of my projects I have done it. If I already have something like an Attiny in it, and I need another (normally higher) voltage rail, I would just use the Attiny and one of its timers and ADC’s .Quote from: peter-h on July 30, 2025, 10:40:38 am

author: tszaboo
author_rank: Super Contributor
timestamp: July 30, 2025, 11:28:12 am
content: Quote from: peter-h on July 30, 2025, 10:40:38 amThey use dedicated buck converter controllers from eg. Onsemi to do these kind of things. They connect with I2C and you can set up a bunch of things.You can make controllers like these from regular MCUs. But it's usually not worth it unless you have very unusual specifications. Because a PWM driver is usually more expensive than a stepdown converter. So the only way it makes sense if :- Current is high- Accuracy is important- You need specific CC/CV control or some other control that's not out of the box- Or you need bidirectional conversionThese are just some examples. Because you can get MC34063 buck converter from china for like 2 cents.

author: peter-h
author_rank: Super Contributor
timestamp: July 30, 2025, 12:11:44 pm
content: There are a couple of angles I can think of:1) Unusual control loop parameters. This is pretty common in industrial temperature controllers which use all kinds of cunning stuff, beyond the standard old PID maths, to deal with stuff like time lags in ovens. At least you modify the PID terms according to how close the feedback is to the setpoint. These are all done in software, and have been for many years. Often in C++2) Reliability. The software has to be 100% deterministic. No rare lockups or delays. Probably not under an RTOS with loads of muxes around

author: JPortici
author_rank: Super Contributor
timestamp: July 30, 2025, 12:46:08 pm
content: given there are MCUs specifically tailored for "digital power" (currently in production that i know of: TI Hercules, TI TMS320, Microchip dsPIC33EP GS / dsPIC33CK) they are very much a thing. The reasons are usually being able to achieve unconventional control loops, that can be also altered in real time without changing hardware components. Also monitoring and more flexibility on the architecture. Always wanted to find a niche project to make one, but never had the pleasure.However, when attending lecture a few years ago the consensus from uni professors was to use FPGAs, one of the reasons being that the hardware was deterministic (a peripheral can be deterministic too.) but it seemed to me it was more out of habit, not having people inside well versed in (modern) microcontroller designs.

author: SiliconWizard
author_rank: Super Contributor
timestamp: July 30, 2025, 01:34:45 pm
content: Well, unless you'd use a very basic and general-purpose MCU for this, and "bit-bang" everything, you're likely to be using a MCU with some dedicated peripheral that will take care of the low-level side of things to some degree - making it a partially "hardware" solution.You can of course have a cheap starting point with a RP2040 or RP2350. Thanks to its PIO, you can program a switching converter that will decently not rely on "software" to operate.You'll still have to configure it from software, of course. But that certainly beats a pure bit-banged approach.In any case, if using a MCU, even more so than if using a FPGA, adding "monitoring" that runs in a separate "unit" (can be interrupt-based, or a high-priority task if using a RTOS) is a good idea to avoid catastrophes.One of the easiest problems you can have when implementing a switching converter is to have it overshoot.

author: peter-h
author_rank: Super Contributor
timestamp: July 30, 2025, 02:03:48 pm
content: QuoteI am sure everybody living in an ivory tower will use FPGAs for everythingI used to do FPGA/ASIC design consultancy so I know a bit about this stuff, and it is a bloody expensive way to do anything, not to mention tools which tend to be horrible and not backward-compatible. Probably not trivial either to implement a PID function.QuoteUsing a PWM output of a timer is the obvious thing for a buck converter. The output power is proportional to the duty cycle, and the frequency is fixed to suit the LC output filter. Also it means if the CPU gets held up somewhere, if the load is constant then the output voltage won't change (much).I think if I was doing a PSU in an MCU I would not have it as an RTOS task but have it running as a state machine in a periodic ISR, say at 10kHz.

author: d-el
author_rank: Regular Contributor
timestamp: July 30, 2025, 02:23:03 pm
content: The soft-loop or in-loop mode is suitable for power supplies with a known load. For example, a LED driver. For designing, you need to have a good understanding of DSP.In motherboards, the voltage is regulated by a PMIC controlled by I2C. Inside the chip there is a simple DAC that sets the reference voltage of the error amplifier.For laboratory power supplies, a circuit with an analog power supply and a DAC/ADC is preferable.

author: peter-h
author_rank: Super Contributor
timestamp: July 30, 2025, 02:40:44 pm
content: OK; using a DAC to set the PSU Vref is trivial. Maybe that is what motherboards do.

author: JPortici
author_rank: Super Contributor
timestamp: July 30, 2025, 03:08:25 pm
content: Quote from: peter-h on July 30, 2025, 02:03:48 pmIt's not just PWM. It's PWM with hardware interlocks for detecting faults (like, the classic fault input so you connect your overcurrent comparators), provisions for high speed (if you wondered where the current trend of delay line based PWMs came from, they that can achieve picosecond resolution so you get high frequency pulses with meaningful bits of resolutions while the rest of the silicon runs at 1-200 MHz tops) and interconnections between peripherals, for example DACs that can be chained to create reference + slope compensation, then connected to comparators which in turn are connected directly to the PWM. PWM that can trigger the ADC with adjustable delay, many such things so effectively all the core has to do is run the control loop and set the couple of registers it needs so the peripherals do their magic.And of course it's going to run in a dedicated interrupt at the highest priority, the RTOS is for the rest of the system to manage communication and stuff, and to change the set points. All things that happen much less frequently.Naturally, nothing that can't be done with general purpose microcontrollers, but the "dedicated" ones usually have dedicated peripherals, perhaps math accelerators and instruction sets better suited to the tasks so you can achieve higher frequency for both PWM and control loop

author: nctnico
author_rank: Super Contributor
timestamp: July 30, 2025, 03:23:30 pm
content: You can build a switching PSU out of any microcontroller. It is simple as that. A PWM peripheral is nice to have but I've used an SPI interface as well. Typically you'll want protection against the inductor saturating so cycle-by-cycle current limiting is a good thing to have. A simple flip-flop does this for you if the microcontroller's hardware doesn't support it. Run the control loop from a (high priority) timer interrupt. Over the years I've build several projects which use microcontrollers to implement switching power supplies (including wireless power transfer).

author: tszaboo
author_rank: Super Contributor
timestamp: July 30, 2025, 03:29:51 pm
content: Quote from: peter-h on July 30, 2025, 02:03:48 pmI've used FPGAs to control make controllable power supplies*. There is a sensible requirement to do that.The power supply was for 50A and you could place 80 of them parallel for 4000A.*I was only making the analog board

author: mtwieg
author_rank: Frequent Contributor
timestamp: July 30, 2025, 05:13:29 pm
content: An embedded controller makes sense when you need to:1. Implement a novel control law (helps with some topologies like LLC)2. Generate specific waveforms (like a DC/AC inverter, or a welder)3. Be configurable, like adjustable voltage or current limits.4. Interface to some other embedded system via I2C, SMbus, etc (for configuration or telemetry)5. Support some sophisticated user interface (LCD or touch screen)Inverters and UPSs are one of the most common examples of embedded power, as they often require simple user interfaces, and have to generate specific waveforms.As for MCUs vs FPGAs, there are lots of options for both. Option the choice just comes down to your comfort level, and the specifics of the application. For example, for a user interface you definitely want some sort of CPU, either as an MCU or an FPGA with a hard/soft core.

author: NorthGuy
author_rank: Super Contributor
timestamp: July 30, 2025, 10:06:40 pm
content: Quote from: peter-h on July 30, 2025, 10:40:38 amYou can't. Instead, you use non-CPU resources for various protections, such as overcurrent, overvoltage, VDD loss etc. They will prevent problems if the CPU crashes.

author: ledtester
author_rank: Super Contributor
timestamp: July 31, 2025, 02:02:23 am
content: Here's is an attempt to use a micro (an AVR atmega) in the control loop of a linear supply:http://tuxgraphics.org/electronics/201005/bench-power-supply-v3.shtmlgithub:https://github.com/clpalmer/digitaldcpowerI think at one time you could get a kit for this on Aliexpress. I also remember there being a review of either this design or another MCU-in-the-loop design -- I'll try to find that.And there's this forum thread from 12 years ago:https://www.eevblog.com/forum/projects/arduino-controlled-power-supply/

author: peter-h
author_rank: Super Contributor
timestamp: July 31, 2025, 06:41:44 am
content: QuoteThen, the question is whether to have a watchdogIt must be a similar issue with motor drives (inverters) for 3 phase motors. You synthesise the sine waveforms, and everything else. It's a very well established business. You could also damage the motor if the CPU crashes.It must have all been done before, and many years ago.

author: nctnico
author_rank: Super Contributor
timestamp: July 31, 2025, 10:15:43 am
content: I'm researching a new project and came across this chip:https://www.st.com/en/power-management/st-onehp.htmlThis is a fully integrated HV DC/ DC converter for USB-PD charging solutions. All the control is done using an ARM Cortex-M0 cpu.

author: JoeyG
author_rank: Regular Contributor
timestamp: July 31, 2025, 10:25:57 am
content: plenty digital power supplies herehttps://www.microchip.com/en-us/solutions/technologies/power-conversion

author: Doctorandus_P
author_rank: Super Contributor
timestamp: July 31, 2025, 01:14:18 pm
content: I have bought and built the Tuxgraphics power supply, but it's not a very good power supply. But it is a nice educational tool, and it's simple enough that you can build it on a piece of vero board in an hour or two. Big disadvantage is that it is slow to react. Both on output voltage dips due to higher current, and the current limiting. The algorithms used in the uC are also quite simplistic, which degrades it further (unfortunately).What I like very much about the Tuxgraphic power supply is the hybrid DAC with both an R-2R and PWM for the lower bits.There is also a Chinese Kit that works a bit similarly. It looks a bit like the "Hiland" 2mA to 2A power supply, but it has two rotary encoders and a HD44780 display. And this also is a quite bad power supply. The Hiland kit (based on a 1970-ies magazine article) is a nicer power supply overall.Another approach is in the Sebulli power supply. The simple version uses an ATmega, and it has a switching (buck / boost) regulator that is fully controlled by the uC. I have not built it. But schematic ans source code is available. He has also built a more capable power supply with an STM32 later. But both of these generate setpoints from the uC, and have analog control loops to stabilize output voltage and current. The Bojansof power supply also looks quite usable, but it also only generates setpoints (filtered PWM) from the uC.Also: If you have a switching power supply that switches at for example 100kHz, then a uC has plenty of time to run a PID or other control loop in software for each PWM pulse. A bunch of the STM32 uC's have high resolution timers especially for this. Software loops are also common in BLDC (or other) motor controllers. And these motor controllers are also power supplies. They don't generate a "steady" DC output, but they do output plenty of power to make a motor run in a pre determined way. It is also common to do over current protection partially in software, or hardware inside the uC. For example sometimes the ADC has "watchdog" functions. If the ADC value exceeds a programmed value, the output PWM can be disabled very quickly.https://www.sebulli.com/psu_kit/index.php?lang=enhttps://github.com/BojanSof/Lab-Bench-PSU

author: peter-h
author_rank: Super Contributor
timestamp: August 01, 2025, 05:56:17 am
content: QuoteThat's a good one. On the 32F4xx you can interrupt from an ADC threshold. But only if you have the ADC reading continuously, AFAIK, which means programming a timer to trigger a DMA which reads the ADC into RAM.

author: SiliconWizard
author_rank: Super Contributor
timestamp: August 01, 2025, 12:13:55 pm
content: That's precisely some approach I would not be comfortable with.My prefered approach by far to implement a switching converter, be it when using MCUs or even FPGAs, is to monitor the output voltage with a comparator and a DAC output rather than from an ADC reading.

author: peter-h
author_rank: Super Contributor
timestamp: August 01, 2025, 12:40:16 pm
content: Doesn't this debate become "how likely is it that this piece of silicon in the MCU will fail"?It's like waiting on a UART TX holding reg status bit, with no timeut. Unless the silicon fails, no timeout is needed

author: ajb
author_rank: Super Contributor
timestamp: August 01, 2025, 05:03:43 pm
content: Quote from: JPortici on July 30, 2025, 12:46:08 pmEven outside the very specialized parts, many modern general-purpose MCUs have a lot of the hardware peripherals you'd need for something like this. If you've got a DAC channel or two, a couple of analog comparators, a timer unit with fault inputs, and an asynchronous event system to tie them all together, you can run an inner hysteretic control loop entirely in hardware and then apply whatever outer loop + logic you want in software on top of that. Just watch out for the analog/timing specs on the peripherals.

author: BadeBhaiya
author_rank: Regular Contributor
timestamp: August 01, 2025, 06:54:58 pm
content: This is my favourite topic in all of power electronics. I have dabbled a bit on the side as a hobby with software controlled power supplies (switching only though).IMO its quite uncommon to have an MCU be used for a power supply unless its something very simple, where regulation required isn't very crucial. The reason is, most MCU are very ill suited to supply a decent regulation, basically anything that could be easily expected from even the cheapest Chinese PSU ASICs is a difficult feat for the average 50MHz to 100MHz MCU to achieve. Plus software is good at being flexible but its difficult to get hardware like reliability. The other more specific reason being, clock speed and hardware peripherals.Assuming you have an MCU with a hardware PWM peripheral (really difficult to do it without a hardware timer if you aren't counting actual instructions. Don't.) and you decided a switching frequency of say 1MHz, you will need an MCU with the minimum clock speed of at least 50x that. In that time, you need to sample the ADC (voltage and/or current, or both), calculate the error, check for any faults, adjust the output duty/frequency. That's a lot to do, even when you only have a single output. Think about it this way, you have a MAXIMUM of only 50 CPU cycles to do all that, and then update the duty cycle (which also takes a cycle). And that is when the duty cycle is 100% (almost never).As others mentioned there are MCU which are specifically designed for power supply driving. They usually have a lot of hardware peripherals (timers, ADCs, DACs, comparators, etc) and they have special features which can let you "chain" together peripherals so that software doesn't need to do much to manage the loop.Finally I have seen a few reasons why someone would have an MCU handling the switching at all.1. Its an uncommon topology which cannot be made with an existing IC on the market2. Some interesting, unique feature is needed (programmable soft-start with highly capacitive loads? Custom modes with different switching frequencies etc to save power? Loop tuned to power a very specific, optimised load)3. Expensive hardware that needs more safety features than you can count

author: SiliconWizard
author_rank: Super Contributor
timestamp: August 01, 2025, 07:02:49 pm
content: Quote from: peter-h on August 01, 2025, 12:40:16 pmNot what I was personally on about, though. Even though a MCU can be a complex piece of silicon that can theoretically have more failure points than other simpler devices, to me this was more "hardware vs. software", which in practice, whether we like it or not, makes a big difference in terms of overall reliability.

author: peter-h
author_rank: Super Contributor
timestamp: August 01, 2025, 07:41:46 pm
content: QuoteI don't see why.QuoteWell maybe that's why! Why 1MHz? The only time you use 1MHz is to make a very small PSU. If you are making something where you have more space, you would go for say 100kHz. Lower losses all around.Now if doing a linear power supply, with a software control loop, a 100kHz interrupt running the loop would be pretty fast. I am no good at maths but AIUI it would produce a pole (or zero?) at 100kHz and you would have to make sure the output capacitor is big enoughSame with a switcher actually; the output filter would need to be big enough.Fundamentally you need to get the loop gain below 1 before the phase shift goes beyond 180 degrees. In general, unless after special transient performance, one achieves this with a big enough capacitor around the error amplifierAnd with a big enough output capacitor.QuoteSure; even just on component count this must be true.

author: NorthGuy
author_rank: Super Contributor
timestamp: August 01, 2025, 07:59:06 pm
content: Quote from: BadeBhaiya on August 01, 2025, 06:54:58 pmYou may not even need any software. I have built a DCM boost controller in PIC18F24Q71 which doesn't use CPU at all (except when you need to change settings). Used one comparator, one DAC, two timers, and a logic cell. All this within the MCU chip connected and working without any CPU participation. CPU is used for other things. Depending on the desired voltage and load current, it produces variable switching rate, may go up to 6 MHz.

author: uer166
author_rank: Super Contributor
timestamp: August 01, 2025, 09:42:07 pm
content: It's done pretty much all the time in production power supplies and various SMPSs in:Automotive (onboard charger, DCDC)EV chargers to many 100s of kWServer PSUsGeneral purpose PSUsSolar MPPT/charger controllers (those are usually buck or boost with CV input control loop)The general trend is the higher power you have, the more likely you are to use a DSMPS instead of an integrated controller for a myriad of reasons. Usually purpose built-ish MCUs are used for that, the lowest cost and most common is TI TMS320 and friends, used from the cheapest shitty DSPMSs where they might cost $1-2 a piece, to monster 50+ kW LLC converters and 600+ kW EV drive inverters that use much more advanced versions in the $20-40 a piece. You use onboard MCU hardware, from regular peripherals like normal timers or HRTIMs on STM32G4, to specialized "CLA" (Control Law Accelerators) on some of the TI chips.If something is a very common use case such as a motherboard CPU core supply, multiphase controller ASICs already exist simply due to volume and higher integration, but there's no reason why you can't make one using an MCU.It's usually good to include HW protection, but not always necessary.

author: uer166
author_rank: Super Contributor
timestamp: August 01, 2025, 09:44:59 pm
content: Quote from: peter-h on July 30, 2025, 12:11:44 pmReliability is not the same as "no delays" though. You engineer your DSMPS to have deadlines be met to a defined degree, but that's trivial nowadays with modern ARM MCUs with NVICs and what-have-you. RTOS is totally fine as long as it's designed correctly and you actually know what you're doing, though I've never bothered since having a 100kHz rate ISR do the work is much easier.

author: uer166
author_rank: Super Contributor
timestamp: August 01, 2025, 09:49:05 pm
content: Quote from: peter-h on July 31, 2025, 06:41:44 amThat last part is an understatement! There are 4 million EVs on the road in USA without major issues with their 3 phase inverter drives. It's doubly harder with PMSM (or BLDC, same thing), since loss of control means loss of field weakening and massive over-voltage on windings, resulting in potentially locked up wheels and/or fire.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: August 02, 2025, 07:43:26 pm
content: 3-phase VFD's are common for at least 40 years. The very old may have had analog sine wave and triangle wave generators for the PWM waveforms, but that has all been changed to uC firmware for many years.But I assume that when these are used in an industrial setting, there will always be some lockout mechanism that protects the motor even if the complete motor controller burns out. Could be something as simple as extra fuses or a PKZM.

author: BadeBhaiya
author_rank: Regular Contributor
timestamp: August 03, 2025, 08:35:02 am
content: You mentioned PIC18F24Q71 and logic cell.Is that one of the new PICs with soft FPGA like capabilities? Never thought I'd see someone using them so soon

author: ali_asadzadeh
author_rank: Super Contributor
timestamp: August 03, 2025, 11:34:02 am
content: QuoteNice input, If it's possible to open source some of the projects it would be very interesting.Also there is tool called biricha which can be used for MCU's like STM32 which is free of chargehttps://biricha.com/tools-and-downloads/st/It calculate the control loop parameters for you, though I have not used it yet. I think since ch32V parts like ch32v203 from WCH-IC is very similar to stm32 and they are faster @ a lower price point, it totally makes sense to use them, I think we can use biricha for them too.

author: DiTBho
author_rank: Super Contributor
timestamp: August 03, 2025, 01:38:26 pm
content: Quote from: SiliconWizard on July 30, 2025, 01:34:45 pmYup. This require fine-hw protections.Anyway, there are "supervisor" chips, created specifically for managing power supply.See Maxim catalogs.

author: NorthGuy
author_rank: Super Contributor
timestamp: August 03, 2025, 05:21:52 pm
content: Quote from: BadeBhaiya on August 03, 2025, 08:35:02 amNo, this one only has logic cells - you can configure them as gates, flops etc. I used one of them as an AND gate to mask out timer starts when an existing pulse is active. I selected the chip because it had 3 DACs, 2 Op Amps, and automatic ADC scanner, all in 28-pin 4x4 mm package with $1 cost.