The supply already has two buck converters inside the unit, both controlled by their feedback resistors, so it was only natural to try to create a programmable resistor.
ResolutionThe feedback resistors are in the potentiometer with ranges a few kiloohms. Since I'd like the output voltage to vary within 0 and 30V with a 50mV precision the needed resolution is 30/0.05 = 600 steps. That means a way to vary the output with 10 bits (1024 steps). A cheap 8-bit DAC (PCF8591) would provide a resolution of only ~120mV.
We should not confuse resolution and precision, but I will break the rules for now.
Why a DAC does not workThird, just a DAC is not sufficient as we need to have the feedback voltage adapt to the output voltage. We could feed the input through and ADC, sample it, calculate and output the correct DAC value but the latency would be ~1ms while the cheapest converter running at 100kHz is able to do it in 10 times faster. It would also keep the microcontroller busy a lot of the time with no time to draw the fancy graphics or respond to user commands.
Even if we choose that route, we would need to output a maximum of 1.25V (usual internal reference voltage) which means we would only use less than half the range. Yes, there are opamps - for scaling - or external DACs with reference voltage input but I'd like to keep this build minimal.
Digital potentiometerThere are quite a few of them on the market but they provide only 5-8 bits. Internally, they just switch a network of resistors. The also don't like high voltages on their input, generally going up only to 3.3 or 5V.
FET as a voltage controlled resistorA select number of MOSFETS have a bigger linear region than the usual ones, but let's assume we don't have access to those. Let's pick a typical MOSFET that you would use in an Arduino project and try to work with that.
I'll jump right to the simulation results and try to explain what's happening:
V1, V3 and B1 form a PWM signal with V3 as a triangle generator (carrier) and V1 as driving main sine wave (modulator). B1 is a formula-driven power supply.
V1 is at 50Hz so we give all the components (capacitors) time to follow the output and recover, V3 and B1 work at 33.33kHz, fast enough to not make an annoying sound and slow enough that an Arduino can do it.
Here's a simulation on how the three waveforms interact, the blue trace is the triangle waveform, the green trace is the sine wave (zoomed in), the red trace is the FET gate driving voltage:
A detail of the PWM output with the sine wave zoomed out, where the red trace represents the gate voltage:
The keen eye might have noticed that we are driving the gate with 15V, which is way above what an Arduino can handle. That's because FETs like to be driven hard and don't work properly otherwise. So there's one showstopper.
AnalysisLet's go back to the main simulation and I'll walk you through all the details.
The green trace is the modulating sine wave (at 50Hz) and the full width of the window shows once period of that (20ms). The top of the window represents 100%, the bottom part is 0% modulation.
The blue trace is the output voltage, sampled at the far right of the schematic from the top of C4.
The red trace calculates the virtual resistance of the circuit by using a resistor divider formula. That means that the far left vertical axis should corresponds to the value in kiloohms (since R6 is exactly 1k).
We can see that the linear region is small and asymmetric: 7 to 8.2 milliseconds on the rising edge and 12.8-13ms on the falling edge (being generous).
The usable range, with calibration, would be for a PWM between 50% and 5%, but only on the falling edge. On the rising edge there is a discontinuity until 15% after which it becomes usable up to 40%.
If you were to put this circuit into your own analog circuit those nonlinearities would introduce a lot of noise, certainly not something you'd want in a power supply.
CircuitThe rest of the circuit is fairly straightforward :
R1 is the needed resistor for driving the gate and R2 the corresponding bleeding resistor. C1 smooths a bit the driving waveform, not too much.
I've chosen V2 to be 10V and R6 to be 1k so that the voltage divider calculations yield readable results.
R8 was chosen to be 47k as it gives a better output range and yields the maximum linear region in my testing. Probably because the current through it matches the current of the transistor in the linear region, but don't quote me on that.
IRFZ46Z was chosen because it had a low Vds (usually signals low Vgs as well) and a high internal resistance. And the fact that it was built-in into LTSpice.
C4 smooths the output voltage so that it displays nicely in the simulation.
Part 2 - trying it with a real converter
To be honest, I did this next simulation before the transistor-only one, but the message seems the same.
The circuit is based on the ubiquitous LM2596 chip and the common IRF510 transistor.
It has the same signal generating components but this time the FET gate is driven at only 10V, for a bit more realism.
The text on the left is the chip's model for Spice, stolen from a Yahoo group.
This time a more realistic load of 10 ohms is used.
You can ignore R4 and C2, those are there only for graphing purposes.
The input voltage (V2) is 10V but the results are similar for 30V.
R2, R3 and R7 were tweaked to their death until they have the best response.
The principle: R2 and R3*+R7 form the voltage divider that feeds the feedback pin on the converter. The chip cuts power when the input voltage on the feedback pin is above 1.25V and raises it when it goes below. R3 is then gradually shorted by the MOSFET, proportional to the PWM percentage.
Of course, there might be heavy oscillations between the V3 frequency (33kHz) and the chip frequency (150kHz), but without going into pole compensation and
The green trace is the sinewave signal, the blue one is the voltage at R3 and C3.
We can see a big rise on startup when the output should actually be zero. Not sure if this is an artefact of the simulation or if it would happen in real life as well. I would bet it does. That pulse can be unhealthy for any device that's connected to it.
The pulse gets larger if the load is smaller or the input voltage is higher or the gate driving voltage is higher -that means lighter loads (like a microcontroller) would see the full input voltage (30V) for almost half a millisecond.
There are serious discontinuities at the 6ms and 14ms mark, but that's to be expected, we're doing this cheap.
What's not that great is the big spike at the 10ms mark. At this point the feedback voltage is zero volts which means the converter either has to restart or gives up trying to regulate. The overvoltage is measured to be almost 30%.
While not a great way to do experiments, simulation can tell you if things are going to be horribly wrong. Since it uses mostly ideal components it is also biased towards optimistic results - reality is almost always harsher.
I think there's no way to do this reliably with a minimum number of components. You'd have to give up accuracy (and safety!) or perform linearity calibrations or use some kind of additional feedback.
Forgot to mention that this kind of supply will not go lower than 1.25V.
You can use a multiplying DAC, but the complexity of that is out of scope for this article.
Or you can just use one of the many tried and tested microcontroller-based power supplies, but their complexity is an order of magnitude larger if you start to consider pre-regulators as well.
Even if you manage to build one fast and cheap you'd still have to test it only to find out it has problems with varying loads, noise or bad start-up performance. Or that it makes audible noises depending on load and output voltage. Or that in certain combinations it locks up and blows itself or your circuit under test.
To sum it upIf you are a beginner just buy a decent power supply - even a 30USD one is fine - and a decent soldering iron. These are the kind of projects you don't want to get started into unless you can deliver better than the commercial offers.
The Korad KA3005P supply is ~100EUR and offers USB control as well.
If you absolutely cannot afford any of these just scavenge an LM317 and add a potentiometer to it (no Arduino, please). It's still cheaper to just buy 5 of these.