AVR Controlled LM317 - Page 1
Forum section: electronics / projects

author: themacman33
author_rank: Contributor
timestamp: April 23, 2014, 09:30:28 pm
content: I need to make a portable, low part power supply based on the LM317. I have worked it out where I can change R2 with NPN transistors (2n2222) from an output on an ATMEGA328. The schematic is the basic premise of it. It is powered from a laptop brick at 19VDC.I tested this and it just gave me 6V, with some fluctuations as it changed. Why is this?Should I use MOSFETS?I know I should put current-limiting resistors in series with the base, I just forgot to draw it in.Thanks!-Mac

author: Bored@Work
author_rank: Super Contributor
timestamp: April 23, 2014, 09:44:48 pm
content: The classic way to do this is:- ATMEGA, or any MCU, outputting a PWM signal- An RC filter to convert the PWM signal to a reference voltage- A (typically rather old) opamp to buffer and amplify the reference voltage. Rather old because of the relative large supply voltage it must work with, and the modern ones often can't stand large supply voltages. Also see the next item, too. But one including the negative supply rail in the input and output is beneficial.- The opamp driving the ADJ pin of the LM317. To do so the opamp needs to be capable of sinking (not sourcing) the current from the resistor between the OUT and ADJ pin - typically 10 mA.Potential issues with your current circuit:- The 19V laptop power supply might generate a lot of noise. You might need to filter it.- No resistors in the connection from the AVR to the base of the transistors- The individual resistors are on the wrong side of the transistors, move them from emitter to collector- You require resistor values and accuracy unlikely to be easily and cheaply available- The 480 Ohm resistor between OUT and ADJ is too large for a general purpose LM317 circuit with no other guaranteed load. Change it to 120 Ohm.

author: retrolefty
author_rank: Super Contributor
timestamp: April 23, 2014, 09:45:14 pm
content: Would the resistors be better placed in the collector rather then emitter?

author: dannyf
author_rank: Super Contributor
timestamp: April 23, 2014, 09:48:42 pm
content: QuoteMOSFETs would work better here - use a logic level mosfet.Also, put the resistors to the collector / drain instead - if you continue to use a bjt, put a base resistor there too. 1k - 10k would work.

author: FrankenPC
author_rank: Unknown
timestamp: April 23, 2014, 10:38:05 pm
content: The 317's adjust pin is in the micro amp range according to the datasheet.I wonder if you could use one of these? Digital potentiometer. The 0-10K end-to-end model with a resolution of 256 positions. Uses SPI. They have others with different interface options.http://www.analog.com/en/digital-to-analog-converters/digital-potentiometers/ad5160/products/product.htmlIt looks like they're only about $2USD each. Hmmm...Makes me want to try this.

author: mariush
author_rank: Super Contributor
timestamp: April 23, 2014, 10:58:12 pm
content: Microchip has some cheaper digital potentiometers, with anything between 32 taps and 256 taps and common sizes (10k , 50k, 100k etc)... for example see here 4in1 IC for $1.6:http://www.digikey.com/product-detail/en/MCP4451-103E%2FST/MCP4451-103E%2FST-ND/2601449You have to be careful a bit because there's something in the datasheets about maximum voltage on terminals for resistors up to the input voltage, which is up to about 5.5vSo not really sure you can use them in a sort of voltage divider configuration with high voltages.

author: Bored@Work
author_rank: Super Contributor
timestamp: April 23, 2014, 11:02:42 pm
content: Quote from: FrankenPC on April 23, 2014, 10:38:05 pmIt is typical pointless to use them. Low voltage and +/-20% resistor tolerance. Digital pots are a band aid. If you find yourself using one you probably messed something up.

author: FrankenPC
author_rank: Unknown
timestamp: April 23, 2014, 11:12:58 pm
content: Quote from: Bored@Work on April 23, 2014, 11:02:42 pmYeah. I see that. There are plenty of PWM -> op-amp solutions out there anyway.

author: David Hess
author_rank: Super Contributor
timestamp: April 24, 2014, 05:33:45 am
content: It will work fine if you move the resistors from the emitter to the collector and add base resistors although as shown, the output voltage should rise if only one of the transistors is turned on.

author: Richard Head
author_rank: Frequent Contributor
timestamp: April 24, 2014, 05:53:07 am
content: Digital pots may not be appropriate in this application but they are definately very useful in many other applications.The 20% tolerance can be managed if correctly designed. Very often the pot is only ever adjusted once and the tolerance is simply trimmed out. Digital pots allow the calibration to be performed by the ATE so no physical pot need to be adjusted.Incidently, I have used a similar arrangement with bipolar transistors switching voltage divider resistors and haven't had a problem with them, however definately locate the resistors in the collector lead. MOSFETS will also work (maybe even better) but are much more expensive than a BC817 or similar. Try a BSN20.Dick

author: dannyf
author_rank: Super Contributor
timestamp: April 24, 2014, 10:59:33 am
content: QuoteThat creates a host of other issues that may not be desirable here.

author: themacman33
author_rank: Contributor
timestamp: April 24, 2014, 09:46:15 pm
content: Which of these two schematics looks best? "B" is preferred because I'm more of a hardware guy, and "A" is more programming-based.Feedback?

author: dannyf
author_rank: Super Contributor
timestamp: April 24, 2014, 10:03:54 pm
content: QuoteDepending on what you mean by "best":1) A is unnecessarily complex; open loop; but offers stepless control of the output;2) B is closed loop; but has only 2^n steps.

author: retrolefty
author_rank: Super Contributor
timestamp: April 24, 2014, 11:54:32 pm
content: Quote from: dannyf on April 24, 2014, 10:03:54 pmWell 'A', being driven by a PWM signal, will still be using discrete steps, just with a larger 2^n value.

author: TerminalJack505
author_rank: Super Contributor
timestamp: April 25, 2014, 01:44:08 am
content: Here's a circuit I've played around with before. Vinwould be your filtered PWM.The negative supply isn't necessary but if you have it handy then you can adjust the output down to 0V. You can likely get by with just a -5V supply even. Without the negative supply the output can only be adjusted down to 1.25V.I'm not sure if R1 is necessary or not. I seem to remember looking at the schematic for the LM317 and thinking it was necessary but I'm not so sure now.Note the 10mA current sink. As B@W pointed out the LM317 requires a minimum output current to maintain regulation.

author: David Hess
author_rank: Super Contributor
timestamp: April 25, 2014, 02:50:28 am
content: Quote from: TerminalJack505 on April 25, 2014, 01:44:08 amThis circuit is a small step from the standard high stability regulator with remote sense using an LM317 (or any positive 3 terminal regulator), operational amplifier, and self biased reference. I have used it to get low noise, low temperature coefficient, and load and line regulation better than 1 microvolt from 0 to 1.5 amps. Using a monolithic regulator also makes it short circuit protected and practically blow out proof.R1 and R2 are only needed if you want the LM317 output to be more than 1.25 volts above the operational amplifier output. With R1 equal to 10k, they basically do nothing.R1 can replace the 10mA current sink which generates the minimum load if necessary. Since 1.25 volts will always be across it, the current is 1.25/resistance so if R1 is 125 ohms, the extra current sink is not needed. One reason not to do this is that the 10 milliamps goes directly into the operational amplifier output. I usually include a PNP transistor to unload the output of the operational amplifier in which case the extra 10 milliamps serves to bias the transistor and never makes it to the operational amplifier neatly solving two problems.

author: Bored@Work
author_rank: Super Contributor
timestamp: April 25, 2014, 05:46:47 am
content: Quote from: themacman33 on April 24, 2014, 09:46:15 pmA) Lacks a resistor between OUT and ADJ pin. Then this is where the current the opamp needs to sink comes from. The MOSFET is unnecessary, instead the buffer should get some gain to increase the MCU's output voltage. Didn't check the RC filter, but the cap seems low.More trickery is possible, e.g. powering the opamp from a negative rail so its output can be taken to -1.25V. With this a power supply can be build with the output starting at 0V, not at 1.25V. Requires to configure the opamp as a difference amplifier with a small fixed offset voltage at its input.B) The gate resistors are not necessary if the MCU can provide sufficient current, even in the worst case of switching all pins at once.QuoteAll reasonably modern MCUs have a PWM output module. It just needs to be set up once, then it does its thing on its own. Changing the duty cycle is typically just writing a single value to a register.QuotePrototype both. Debug and measure both. Twice the fun. Then build a third one. Three times the fun. Somewhere in between build an adjustable load. More fun.

author: Richard Head
author_rank: Frequent Contributor
timestamp: April 25, 2014, 06:05:22 am
content: The first circuit won't work. The reason being that there is an internal error amp and control loop in the LM317 that expects feedback from the output voltage.What you have with the PWM and filter/buffer is open loop with no feedback of the controlled parameter (output voltage).If you desire stepless control using PWM then toss the LM317 and use something like a TIP 122 as a series pass element fed by an op-amp. Then use the filtered PWM signal as your reference input to the non-invering input and the inverting input for your voltage feedback, via a resistor divider of course. That is essentially a programmable power supply. I have omitted the control loop compensation toy simplify the desription a little but it will be needed. A single pole placed at about 10khz will probably be fine.I'de draw a schematic for you but I don't know how to attach it!Dick

author: Lizerd
author_rank: Contributor
timestamp: April 25, 2014, 06:47:33 am
content: QuoteThe LM317 do not need external feedback, the output voltage are always (when it can be) 1.25v higher then the adjust pin.So the A solution works fine with LM317 because it uses a internal feedback, but with other regulators that use external feedback it do not work !!I know this because a have done the A solution and it works just fine.But its better to set a gain to the oamp, because if you like to be able to adjust to 20v and the MCU uses 5v logic you have to have a gain of 4 on the oamp.Check this block diagram of a LM317, you can see it uses a internal feedback oampand i have made a test setup based on Solution A so i do know it works

author: Richard Head
author_rank: Frequent Contributor
timestamp: April 25, 2014, 06:57:49 am
content: Ok, perhaps it will work to a degree, but certainly not closed loop as one desires. As soon as you load the output the voltage will drop as there is no feedback.I generally try to use semiconductors as they were intended to be used when designed.

author: Lizerd
author_rank: Contributor
timestamp: April 25, 2014, 07:17:56 am
content: But if you check the LM317 block diagram, you can see it uses a internal feedback oamp.So it works even if i put a heavy load on it

author: Richard Head
author_rank: Frequent Contributor
timestamp: April 25, 2014, 08:56:10 am
content: LizerdApologies, I see it has the feedback internally. Also, the filtered PWM signal will simply add to the internal reference and the error amplifier will see the sum of the two.I have never used an LM317 in this way but it seems fine.Of course, the reference voltage to the micro must be stable so the PWM supply doesn't vary.Dick

author: Lizerd
author_rank: Contributor
timestamp: April 25, 2014, 09:28:10 am
content: Dick: Sorry if i sounded harsh, was not on purpose.Yes the noise from the PWM * gain will add on the output voltage.But if you use a really big slow RC filter between the PWM and input on the oamp you will be fine.

author: Richard Head
author_rank: Frequent Contributor
timestamp: April 25, 2014, 10:55:08 am
content: LizardNo problem.The method works well and I've done it on a couple of occasions.With my applications I've had to place a 4001 CMOS between the micro and the LPF. The supply voltage to the 4001 is adjustable thereby making the PWM amplitude adjustable over a small range. This is used to trim the output voltage at a given duty cycle. Maybe in future I'll try a software trim.Dick

author: dannyf
author_rank: Super Contributor
timestamp: April 25, 2014, 11:20:32 am
content: QuoteA simpler approach for B is to tie the resistors directly to the output pins. Output a '0' put that resistor in the divider; Float that pin takes the resistor out of the divider.However, it does put some limitations on the output voltage.

author: Richard Head
author_rank: Frequent Contributor
timestamp: April 29, 2014, 11:57:11 am
content: I have recently used a 10-bit DAC instead of a PWM. It works like a dream with no calibration required. Unfortunately the DAC is damn expensive. Luckily this application can afford it.I wonder if they"ll ever have onboard DAC's in microcontrollers? I love using them, so precise.

author: dannyf
author_rank: Super Contributor
timestamp: April 29, 2014, 12:22:49 pm
content: QuoteWonder no more - tons of them out there.

author: Richard Head
author_rank: Frequent Contributor
timestamp: April 29, 2014, 01:37:51 pm
content: dannyfNow you've got me looking at the TI micros with onboard DAC's. I never knew!

author: dannyf
author_rank: Super Contributor
timestamp: April 29, 2014, 02:01:38 pm
content: Not just TI, other folks too: Freescale is top notch when it comes mixed signal mcus (Silabs great on ADC). STM32F3 is another option. Even some of the lowly PICs now have DAC (or Vref generator with an output pin).

author: miguelvp
author_rank: Super Contributor
timestamp: April 30, 2014, 05:48:28 am
content: And Cypress, don't forget about them, even with programmable analog blocks.

author: Lizerd
author_rank: Contributor
timestamp: April 30, 2014, 06:04:44 am
content: Almost all mid-high end STM32 has 12bit DAC.Even quite a lot of the low-ends have 12bit DACupdate on my lab psu, read more atwww.lizerd.se/?p=893

author: themacman33
author_rank: Contributor
timestamp: August 10, 2014, 04:52:49 pm
content: After a brief break, I have decided to just use a lm2576-adj with i2c from an AVR to control a SMPS. I find that it will be easier to implement, lower noise, and more accurate.Lizerd, that looks like a really neat project, I'll take a look at it!

author: dannyf
author_rank: Super Contributor
timestamp: August 10, 2014, 10:26:03 pm
content: QuoteNicely done.QuoteThe same principle applies to a linear regulator-based power supply. Switchers do have the advantage of being more efficient, making them more compact.