Author Topic: Microcontroller Controlled Switching Mode Power Supply  (Read 13308 times)

0 Members and 1 Guest are viewing this topic.

Offline gcp6ca

  • Contributor
  • Posts: 17
  • Country: 00
Microcontroller Controlled Switching Mode Power Supply
« on: November 26, 2011, 11:32:33 pm »
A few months ago built a buck synchronous converter to learn about them using a PIC18F14K50 (USB controlled) - schematic attached. This was when I first started working with MOSFETs and was learning about switching mode power supplies so the parts I selected were based on what I had and experiments (no oscilloscope - multimeter only). After some experience I know that my high side MOSFET configuration is wrong and I was able to drive the MOSFETs by combining a charge pump and a MOSFET driver that accepts higher voltage. I have tried bootstrapping half-bridge drivers but I blew up a few with out much success (expensive). However it would work well with light loads - less than 1A - (as far as the multimeter let me know) since it stayed within a 10mV of my setpoint.

My logic was simply to increment the PWM duty by 0.1% if the output was less than the setpoint (with some tolerance) and vice-versa. Then a month ago I briefly learned about PID controllers in university and I found some code online that can be implemented in a microcontroller. I also got an oscilloscope and saw that my buck converter responds slowly (dependant on setpoint).

My question is how can I model the buck converter in Simulink so that I can try to tune a PID controller in a simulation? I also learned about Nyquist sampling theorem (atleast 2x of actual frequency) so if the PWM is at 100kHz, does the sampling have to be at least 200kHz? If you have any other suggestions on improving the project, then please let me know. I want to make it work at 1A then try it at 5A so one of my objectives was to use the smallest value inductor so it can pass higher currents and I have lots of those that I pulled from old power supplies and motherboards.

I may not some of the basic that you might assume since I am a mechanical engineering student (that loves electronics).

Edit:
I completed the power supply and posted the project here:
http://www.libstock.com/projects/view/247/dc-dc-pid-controlled-step-down-power-supply-with-usb-hid-interface-mikrobasic-mikroc-mplab-c18
« Last Edit: January 15, 2012, 01:16:54 am by gcp6ca »
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 9619
  • Country: us
Re: Microcontroller Controlled Switching Mode Power Supply
« Reply #1 on: November 27, 2011, 12:41:47 am »
The 100 kHz frequency of the PWM is going to represent noise in the system relative to the output voltage you are trying to regulate. When you put a feedback controller in the loop like a PID controller you definitely don't want to sample the noise, you want to sample or measure the value you are trying to control. So sampling at 200 kHz would be a bad mistake. You will want to sample much more slowly than 100 kHz and also make sure there is some noise filtering on the measurement. Your objective is to get a good signal to noise ratio on the measured value you feed to the PID controller.

As to modelling, the pragmatic engineer might derive a simplified linear model of the system and use that to tune the control loop. One way to get a linear model is to place the converter in open loop mode and record the response to step changes on the input (for example by using a digital oscilloscope, or by using the circuit simulator equivalent). Once you know how the output responds to step changes on the input you can use mathematical techniques to fit a model to the system (for example second order plus dead time). With such a model you can now tune your PID controller and you will likely find that tuning works well on the real system.

Maybe this sounds a bit advanced for now, but if it doesn't immediately make sense keep it in mind because it will come together eventually.

The reason I am not addressing your Simulink question directly is that I am not familiar with that software. There is nothing wrong with tuning a PID controller interactively with a software model of the system you are controlling, as long as you understand any limitations of the model compared to real life.
I'm not an EE--what am I doing here?
 

Offline gcp6ca

  • Contributor
  • Posts: 17
  • Country: 00
Re: Microcontroller Controlled Switching Mode Power Supply
« Reply #2 on: November 27, 2011, 02:01:09 am »
Thanks for your explanation and I think I mostly understand what you said. Since the output should ideally be DC signal it would be 0Hz and anything above would be noise (theoretically). This means I need a low pass filter to the input to keep the signal as close to 0Hz as possible. I am measuring the output from the inductor so the large electrolytic capacitor (ripple control) also acts as a low-pass filter but would adding ceramic capacitor in parallel help the measured signal to noise ratio even further (there must be a reason why linear regulators use small ceramic capacitors in parallel)? If my output signal is very noisy but the MCU is getting a cleaner signal through a low-pass filter, does that mean the solutions lies hardware (increase capacitor, inductor or frequency) and not in software?

Would my sampling rate/PID cycle depend on my desired performance of the system? If want my converter to respond (like settling time) within 1ms (1kHz) then should my sampling frequency be between 2-10kHz?

For the model when I said Simulink, I was thinking about a block diagram. At university I learned to create a block diagram for a motor then add a PD and PI controller for position and velocity control. I thought that method may help but your suggestion is to treat the relation controller input/output as a black box and try to get a mathematical relationship between them and I like it. I am bit confused to what I would input and what I would measure. You said step inputs (that's all I need it to do), but did you mean to set a PWM duty and treat it as a step input? To actually measure the output, do you mean to make a model by using natural frequency and damping ratio that I would calculate from settling time and maximum overshoot? After I select the final frequency, inductor and capacitor would I be measuring the response to different amplitude steps or different loads? It would be great if you can provide some examples.

Can you provide some examples of limitations of the simulation? All I can think of are that there is a minimum and maximum voltage possible, PWM duty can can only be set up 0.1%, the model is not perfect but it would still be close which is good enough for me. Is their anything else major that I have not considered?
 

alm

  • Guest
Re: Microcontroller Controlled Switching Mode Power Supply
« Reply #3 on: November 27, 2011, 02:11:11 am »
Limiting the bandwidth of the control loop will also make the transient response worse, so you probably need to find a good balance. Damping the hell out of it will reduce your response to input and load transients, eg. a 1kHz 10% to 100% load square wave. Designing a filter that specifically rejects the PWM frequency and its harmonics sounds like the best solution to me.
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 9619
  • Country: us
Re: Microcontroller Controlled Switching Mode Power Supply
« Reply #4 on: November 27, 2011, 03:12:47 am »
Thanks for your explanation and I think I mostly understand what you said. Since the output should ideally be DC signal it would be 0Hz and anything above would be noise (theoretically). This means I need a low pass filter to the input to keep the signal as close to 0Hz as possible. I am measuring the output from the inductor so the large electrolytic capacitor (ripple control) also acts as a low-pass filter but would adding ceramic capacitor in parallel help the measured signal to noise ratio even further (there must be a reason why linear regulators use small ceramic capacitors in parallel)? If my output signal is very noisy but the MCU is getting a cleaner signal through a low-pass filter, does that mean the solutions lies hardware (increase capacitor, inductor or frequency) and not in software?

It's actually possible to use a software filter on the measured value of a PID controller. In fact many industrial controllers do this as a matter of course. (This simplest software filter is a first order lag, and this is what you will typically find. The nice thing about a first order lag is it only has one tuning parameter, the time constant. If you make a more complex filter it will have more parameters to tune and the average user will quickly give up on it.)

The reason for ceramic capacitors is different. They are used because large electrolytic capacitors don't work well at high frequencies. (I think the reason is the big roll of coiled up metal foil inside them. At higher frequencies the inductance of this coil starts to cancel out the capacitance.) Many practical circuits automatically parallel up a large electrolytic for low frequencies and a small ceramic for high frequencies.

Quote
Would my sampling rate/PID cycle depend on my desired performance of the system? If want my converter to respond (like settling time) within 1ms (1kHz) then should my sampling frequency be between 2-10kHz?

Yes, absolutely. Here I have no idea what kind of response a typical power supply is expected to have. I don't know whether the response to load changes (or supply voltage changes) is supposed to settle out in 1 ms, 10 ms or 100 ms in typical bench supplies. Maybe someone could educate me (please)?

Quote
For the model when I said Simulink, I was thinking about a block diagram. At university I learned to create a block diagram for a motor then add a PD and PI controller for position and velocity control. I thought that method may help but your suggestion is to treat the relation controller input/output as a black box and try to get a mathematical relationship between them and I like it. I am bit confused to what I would input and what I would measure. You said step inputs (that's all I need it to do), but did you mean to set a PWM duty and treat it as a step input? To actually measure the output, do you mean to make a model by using natural frequency and damping ratio that I would calculate from settling time and maximum overshoot? After I select the final frequency, inductor and capacitor would I be measuring the response to different amplitude steps or different loads? It would be great if you can provide some examples.

Your control system needs to respond to changes in load and also to changes in supply voltage. So one step change you could make is a step change in the supply voltage. You could for example apply a low frequency square wave to the input and record the output voltage in response. You would do this with no feedback control at a fixed PWM frequency, so the output voltage would go up and down in response to the input voltage. Another test would be to make step changes in load current while keeping the supply voltage fixed. Once again the output voltage will go up and down in response and you can record this waveform.

In every case where you do such a test you will have a step change input (up or down) and a curvy response on the output. The most pleasing and simplest response will look like a first order lag, but complex systems might not behave that way. Once you have your step response you can for example load it into Excel and fit model parameters to best fit an idealized simple model. (In order of complexity, simple models are: 1. pure first order; 2. first order plus dead time; 3. second order plus dead time.) If you have model parameters for a simple open loop response you can often find PID tuning guidelines in books. It's much more fun, however, to adjust the parameters by hand and watch what happens using a simulation.

Quote
Can you provide some examples of limitations of the simulation? All I can think of are that there is a minimum and maximum voltage possible, PWM duty can can only be set up 0.1%, the model is not perfect but it would still be close which is good enough for me. Is their anything else major that I have not considered?

Simulations always make assumptions, and always are only an approximation of real life. Old and wise electrical engineers know the only way to test whether a circuit is really going to work as expected is to build it and see. All sorts of things can get in the way, from components having non-ideal behaviours to parasitic inductances and capacitances to unplanned feedback loops and oscillation.
I'm not an EE--what am I doing here?
 

Offline gcp6ca

  • Contributor
  • Posts: 17
  • Country: 00
Re: Microcontroller Controlled Switching Mode Power Supply
« Reply #5 on: November 27, 2011, 03:42:25 am »
Limiting the bandwidth of the control loop will also make the transient response worse, so you probably need to find a good balance. Damping the hell out of it will reduce your response to input and load transients, eg. a 1kHz 10% to 100% load square wave. Designing a filter that specifically rejects the PWM frequency and its harmonics sounds like the best solution to me.

I did not understand "its harmonics sounds like the best solution to me". Were you referring to the multiples of the frequency? It seems like the optimal solution be to have a low-pass filter (such as 50kHz for 100kHz PWM) and run the cycles as fast as possible which would be between 20-50kHz for PIC18F14K50 (very slow ADC).

It's actually possible to use a software filter on the measured value of a PID controller. In fact many industrial controllers do this as a matter of course. (This simplest software filter is a first order lag, and this is what you will typically find. The nice thing about a first order lag is it only has one tuning parameter, the time constant. If you make a more complex filter it will have more parameters to tune and the average user will quickly give up on it.)

I have seen the filter that you are talking about in Simulink but I am not sure how I would add it in code for a PIC - I guess I will have to search around. I never considered a software low-pass filter but it will be so much easier to experiment if I can figure it out.

Your explanation on modeling the plant (converter response) was very helpful. I never trust simulations either but I tried to experiment with PID gains and I got no where since I had no idea what I was doing. I am not trying to make a professional power supply, I just want to learn to make a good PID controller by making it respond better than what it does now.

Also I find it odd that the verification always asks "What is 84 divided by 2?".
 

alm

  • Guest
Re: Microcontroller Controlled Switching Mode Power Supply
« Reply #6 on: November 27, 2011, 04:03:51 am »
I did not understand "its harmonics sounds like the best solution to me". Were you referring to the multiples of the frequency? It seems like the optimal solution be to have a low-pass filter (such as 50kHz for 100kHz PWM) and run the cycles as fast as possible which would be between 20-50kHz for PIC18F14K50 (very slow ADC).
Yes, I was referring to the fact that the PWM signal does not consist of a single frequency, so just a 100 kHz notch filter for example would not work. If settling time is not important, you'll probably find that choosing a lower cut-off frequency results in a more stable control loop.
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 9619
  • Country: us
Re: Microcontroller Controlled Switching Mode Power Supply
« Reply #7 on: November 27, 2011, 04:47:09 am »
Your explanation on modeling the plant (converter response) was very helpful. I never trust simulations either but I tried to experiment with PID gains and I got no where since I had no idea what I was doing. I am not trying to make a professional power supply, I just want to learn to make a good PID controller by making it respond better than what it does now.
There are some rather simple rules of thumb when tuning PID controllers.

The first rule is to leave the derivative action at zero.

The second is to figure out how much capacitance the system has. If the system has more capacitance you will need more P and less I in the tuning. If the system has low or zero capacitance, your optimum tuning will have plenty of I and not so much P.

The third rule is to leave D at zero, unless you really need it. If you have a control response with P and I that still isn't good enough (meaning that you either have a slow reponse with little overshoot, or a fast response with too much overshoot) then maybe you need some D. You slowly increase the derivative action until you get the best response you can, remembering that increasing D allows you to increase I a bit, but if you increase D too much your loop will go hopelessly unstable.

Probably 95% of PID controllers out there are PI controllers. The "D" in PID is for Desperate, when other attempts at tuning have failed.
I'm not an EE--what am I doing here?
 

Offline NiHaoMike

  • Super Contributor
  • ***
  • Posts: 5849
  • Country: us
  • "Don't turn it on - Take it apart!"
    • Facebook Page
Re: Microcontroller Controlled Switching Mode Power Supply
« Reply #8 on: November 27, 2011, 05:17:19 am »
Try using a dsPIC designed for controlling power converters. Just ask Megan Cornner or Brittany Benzaia and you'll understand how a dsPIC makes easy work (as far as control goes, that is) out of even really demanding motor drive inverters.
Cryptocurrency has taught me to love math and at the same time be baffled by it.

Cryptocurrency lesson 0: Altcoins and Bitcoin are not the same thing.
 

Offline gcp6ca

  • Contributor
  • Posts: 17
  • Country: 00
Re: Microcontroller Controlled Switching Mode Power Supply
« Reply #9 on: January 15, 2012, 01:15:12 am »
After I implemented the PID Controller in software I only used a proportional gain which help my response quite a bit but it was overshooting. To reduce the overshoot I added derivative gain and after playing around with the gain a little bit I got the response time to be around the same but only about 2-3% overshoot (compared to 15-25%).

If anyone is interested I posted my project here:
http://www.libstock.com/projects/view/247/dc-dc-pid-controlled-step-down-power-supply-with-usb-hid-interface-mikrobasic-mikroc-mplab-c18
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf