Basic power supply design - Page 1
Forum section: electronics / projects

author: sachleen
author_rank: Contributor
timestamp: November 30, 2013, 08:26:41 am
content: Hi guys,So I'm making a LM317 based power supply to learn more about power supplies, op amps, and voltage & current monitoring. I have already made one following the typical application from the datasheet with panel meters for voltage and current but am now looking at a software controlled supply. I've seen Dave's power supply design videos and a bunch others online.Design DescriptionI will power it from a 12v wall adapter so the max output voltage will be around 10v. Max output current of the LM317 is 1.5A; I'll probably never go that high but I want to design for it. So I have a 7805 to power the ATmega328 and a display (not in the design yet). A low pass filter converts the PWM output to an analog voltage which gets amplified by a LM358. Because I only care for a max output of 10V the op amp has same value resistors for a gain of 2. This feeds into the adjust pin of the 317 and I get the same voltage on my output.I have a voltage divider setup with two 100k resistors to divide by 2 to bring the 317's output to a 0-5v for the ADC. I use this to display the output voltage as well as feedback to adjust it to match the desired voltage. I know the ATMega328 has a 10 bit ADC so the 5 volt range is divided into 1024 sections giving me a resolution of about 4.8mV.I also have 10 10ohm resistors in parallel to give me a 1ohm current sense resistor. Following P = I^2 * R => 1.5^2 * 1 = 2.25W so 10 1/4W resistors should be good enough for that. I calculate that the maximum voltage across the sense resistor will be V = I * R => 1.5 * 1 = 1.5V. To increase the resolution I have another op amp with a gain of 3.2 to give me a range of 0-4.8.I have it all working on the breadboard, although I had to use some resistor values based on what I currently have. The software shows the voltage and current fairly accurately although it definitely needs more testing and calibration.My QuestionsFirst,is there anything wrong with my understanding of how things are workingbased on my design description?My main concern is with protection... I don't think I need anything on the Vmeasured input but the most obvious thing to me is a short circuit on the outputs causes the full 10V to go into the analog input. I've read that I can use a ~5V zener diode but it begins to conduct slightly below that voltage, which will cause an error in the reading.What's a simple way to protect the analog inputs from overvoltage?Is it practical to have this setup with voltage control and current limiting in software?I have it all working but can only test with low currents right now. When the software sees Imeasured go above the set current limit it begins to bring down the voltage until the measured current below the current limit.I'd love to hear your suggestions on how to improve the design as well. I thought about getting two 12 bit ADCs but the resolution gained isn't worth the cost to me.

author: papo
author_rank: Regular Contributor
timestamp: November 30, 2013, 09:06:22 am
content: HeyA couple of comments:According to your schematic, the v rsense node is grounded. You probably want this to be "Vout-" or something but I guess this is just a typo.If this is a power supply for general use, you might want it to be robust. The LM317 is not very good at tolerating negative differentials across input-output, meaning that if the voltage at the output gets higher than the voltage at the input, the LM317 might break. A protection diode in parallel with the part can address this, see the datasheet.You are doing a low side current measurement. There are a couple of disadvantages compared to a high side current measurement. This is not to say that your design decision is bad per se, but you might want to be aware of these disadvantages.As you calculated, U3A should reach 4.8 V at 1.5 A with the chosen gain. However, the LM358's output cannot reach the positive supply rail (check the data sheet, the keyword here is "rail to rail opamp".), so your current measurement will be cropped below 1.5 A. You would potentially have a similar problem with U3B, but since the regulator's output voltage will be 1.2 V above Vadj, there is probably enough margin.As a consequence of the last sentence above though, your supply will not be able to go down to 0 V.Addressing the specific questions:Check whether U3A can tolerate the 10 V input differential at 5 V. If not, you could also add a clamping diode or do some other tricks. I would not go for the zener because of the reason you mentioned. You are right in being concerned about this one. How do you do short circuit protection right now? Just in software?You will probably get different opinions on your second question. Personally, I would clearly go for an analog control loop due to speed and because I am more familiar with that kind of stuff. Also I would constantly suffer from the (irrational?) fear that my code is so bad that it causes the micro to lock up and lose regulation. But that's just me.HTH, regardsMatt

author: filip_cro
author_rank: Regular Contributor
timestamp: November 30, 2013, 11:21:31 am
content: Resistors parallel is shorted. I can't see output filter (electrolytic capacitor). Vmeasured should be on output, after current measurement so you will measure true output voltage not output voltage + voltage drop. (1A*1OHM=1V error)PMW filter looks weak. Maybe putting 2 like in uSupply will work better.LM317, LM358 and LM7805 aren't relay precision components.

author: fcb
author_rank: Super Contributor
timestamp: November 30, 2013, 12:30:59 pm
content: Some pointers:Your output voltage regulation will suck, mainly due to the position of your current shunt, I would put the current shunt in the high-side supply to the 317 and then use an opamp in differential amplifier config to sense the current.Worth understanding that your 317 VOUT stays at 1.2v above the ADJ pin, so you can't go lower than 1.2v in your config.If you put a couple of diodes in the output of the 317 and incorporated the final output in the loop of your opamp, you'll get down or very near 0v.Also, your PWM filter is fine - if your frequency is high enough. I tend to put two smaller RC's in series, less expensive if you haven't got 4.7uF on your BOM.

author: dannyf
author_rank: Super Contributor
timestamp: November 30, 2013, 02:02:32 pm
content: QuoteThe design is generally right. "generally" in that it has a floating "ground" for the load - the ground for the load fluctuates with the current the load draws. This may not be an issue for your application.If it is an issue, you may consider high-side current sensing.I would also consider putting that capacitor on the mcu's input pin side.QuoteA serial resistor.QuoteGenerally, current limiting via software can be an issue: you aren't sensing the current all the time and the current spike in between each sense points in time may cause the device to fail (less of an issue here due to the protection built in).

author: sachleen
author_rank: Contributor
timestamp: November 30, 2013, 08:18:13 pm
content: I chose low side current measuring because it is easier and requires less components than high side. Following this fromTI's Getting Started with Current SensingQuoteI guess I don't really understand the major disadvantage of having low side sensing with a floating groundI need to get a rail to rail op amp OR adjust the gain so I get accurate readings. If I adjust the gain I lose resolution but I've read I can change the analog reference on the mcu to compensate for that. I think it'd be better to just get a more appropriate op amp.I do understand the supply cannot go below ~1.25V.Quote from: filip_cro on November 30, 2013, 11:21:31 amThat's a good point... I wonder why, in all the schematics I've seen, it's done the way I originally had it. For example,this onefromtuxgraphics.I ran a simulation on circuitlab and found the ripple voltage on the PWM filter to be about 29mV. (see attached) Is that too high for this application?Quote from: dannyf on November 30, 2013, 02:02:32 pmWhy is that? Every schematic I've seen online has the capacitor on the output side of the filter.Quote from: dannyf on November 30, 2013, 02:02:32 pmIs that not for overcurrent?

author: papo
author_rank: Regular Contributor
timestamp: November 30, 2013, 10:00:31 pm
content: HelloQuoteThere is a bunch of issues, here is one: Let's say you set your supply to deliver 5V. For this to happen, you need Vmeasured to sit at 454.5 mV or so, so that's your controller's setpoint. Now you draw 1 mA. You will get a drop of 1 Ohm * 1 mA = 1 mV across your sense resistors, and the voltage across the output terminals is 4.99 V (in theory), well within your ripple so that's fine. But if you draw 1 A, there is 1 V of drop across the sense resistors and you end up with only 4 V across the terminals. To address this, you would have to take into account Vrsense when computing the setpoint for Vmeasured. That's most likely what they are doing in the design you linked in; it works but it's not very elegant and adds complexity to your controller.The drawback of a high side measurement is of course that you somehow have to reject the common mode, so that adds complexity to your circuit. By the way, Dave has a pretty recent video on a Rigol power supply where the low side current measurement turns out to be an issue. In his case, the problem manifests itself because the supply has more than one outputs but this might still be interesting. Also, if you somehow manage to short your negative supply output with the circuit ground, you can have huge currents flowing but the drop across the shunt will be 0 V, so you won't detect that. Incidentally this is exactly how you mis-drew the schematic :-).QuoteAssume that for a particular gain, the max output of the amp would be 5 V. A 10 bit ADC would convert this to 1023, the largest measurement you can have. But because your amp cannot reach the 5 V rail, you set the gain such that the max output is 4 V. The ADC reads around 818 and you are losing 205 bits right there. To overcome this, you can adjust the reference to 4 V which means that the ADC reads 1024 at 4 V and you recover the full ADC range. In this case however you have to think about how to get these 4 V; a voltage divider will add errors, a precision reference might be overkill etc. So going for an rail to rail amp in this case seems to be sensible. Also keep in mind while choosing a part and/or calibrating that opamps have an offset voltage that may play tricks to you.QuoteNice! Sorry I did not realize that you had built the circuit already when you posted and just wanted to make sure that you were aware of that.QuoteThis really depends on the application and as far as I can see, your main goal with this is to learn about opamps and other circuitry. So it's really up to you to decide whether the ripple is attenuated sufficiently. Sometimes this is very hard to tell, especially for "generic" gear like a power supply. By the way, did you play with the PWM frequency to keep the ripple as low as possible?Referring to the discussion about input protection: You write that you want to protect the analog input. Are you referring to the microcontroller? In that case, you have to be super careful, because what I wrote before might suggest that your opamp output cannot reach/exceed the positive rail. That would be 5 V and it would appear to be safe in any case as long as the opamp can sustain the 10 V. However you have to be careful because under some conditions, some opamp outputs will indeed exceed the positive supply, without the opamp impedance goodness, though. I would try to figure out whether a clamping diode or a TVS is a good choice for protection here.

author: sachleen
author_rank: Contributor
timestamp: November 30, 2013, 10:36:51 pm
content: Quote from: papo on November 30, 2013, 10:00:31 pmThis can be addressed by moving the voltage divider for Vmeasured to the other side of the current sense, right? This way it'll always measure the potential difference between the two output terminals on the supply and the software will automatically adjust the output voltage as the drop across Rsense changes.You make a good point about shorting the negative supply output to ground. That would bypass Rsense and my current reading will be 0. I hadn't considered that...Quote from: papo on November 30, 2013, 10:00:31 pmYeah I find that generally a higher frequency results in lower ripple. I use a 980Hz square wave in my simulation.Quote from: papo on November 30, 2013, 10:00:31 pmWhen I wrote the original post I hadn't taken into consideration the op amp I had just added... so I would think the mcu pin is fine, but I'll have to research more on op amp output exceeding the positive supply.QuoteCan you explain this one a little bit, please.Looking at thedatasheet, I read "Differential Input Voltage Range Equal to the Power Supply Voltage" and on page 11, I seeQuoteIs that what you're referring to? The input differential is the difference between the negative and positive inputs, right? So in my case, if V+ is my DC input, my input voltage will never be higher than that since the 317's maximum output voltage is less than the DC input.

author: TMM
author_rank: Frequent Contributor
timestamp: December 01, 2013, 02:25:02 am
content: Quote from: sachleen on November 30, 2013, 08:18:13 pmThe ATMega has internal protection diodes between each input pin and VCC/GND which will clamp down the voltage at the input pin if it exceeds a diode drop above/below VCC/GND. The resistor prevents excess current through the diodes.

author: papo
author_rank: Regular Contributor
timestamp: December 01, 2013, 10:57:19 am
content: Quote from: sachleen on November 30, 2013, 10:36:51 pmNot sure if I understand this correctly. But if you do this, your shunt will not only measure the current flowing through the device attached to your supply, but also the current flowing though the voltage divider. That current might be sufficiently small to be neglected, though. I don't think you can measure the voltage difference you mentioned directly with the atmega as every ADC measurement will be referenced to ground. But I am not sure on that one, maybe there is a negative reference pin or something. Either way you'd have to do some calculations inside the controller.Quote from: sachleen on November 30, 2013, 10:36:51 pmSmaller ripple at higher frequencies makes sense. Maybe you can increase that frequency to reduce the ripple even further?Quote from: sachleen on November 30, 2013, 10:36:51 pmYes, the mcu is fine because of what TMM said. Have a look at page 75 of the atmega 328 datasheet. There is figure 13-1 showing two diodes. These are clamping diodes. I did not know the atmega had these internal diodes when I suggested to add them (together with a series resistor) yesterday.QuoteYes that's exactly what I was referring to and the differential voltage is what you said and you don't have to protect the part. That being said, there is still a couple of ways to break the supply by e.g. applying external voltage through the output *to* the supply. Depending on the application, I would add even more protection (think about connecting batteries to the supply, inductive kickback etc.) but that's again up to you. Summarizing, add the series resistor mentioned by TMM. I would also add an external clamping diode, but that's because I don't know the atmega very well. If people familiar with the part say that it's fine, go for what they say.

author: papo
author_rank: Regular Contributor
timestamp: December 01, 2013, 11:06:42 am
content: And I just saw that there is some ADC clamping discussion going on in this thread:https://www.eevblog.com/forum/projects/protecting-adc-input-with-diode-clamp/