Controlling an LM2596 Board with arduino

Hello! I'm trying to control an LM2596 based Buck Converter with an arduino without any success.
Here is the setup:
I removed the trim-pot from the converter board, connected a 1K resistor from the feedback pin to a buffered RC filter and with a voltage divider ran the output of the converter to an Analog input. I also added a 10uF cap at the power rails for stability. The load is just a regular blue LED.
Before adding the "255-output" line, I had no signal from the PWM pin, but after doing so, I have a ~50% duty cycle signal which doesn't change at all (even if I disconnect the load or add a new one).

Here is the code:

#include <AutoPID.h>
#define OUTPUT_MIN 0
#define OUTPUT_MAX 255
#define KP 0.12
#define KI 0.8
#define KD 0.003

double analogReadValue, setPoint, outputVal;
AutoPID myPID(&analogReadValue, &setPoint, &outputVal, OUTPUT_MIN, OUTPUT_MAX, KP, KI, KD);

void setup() {
myPID.setTimeStep(200);
Serial.begin(9600);

void loop() {
setPoint = 50;
analogReadValue = analogRead(A1);
Serial.println(analogRead(A1));
outputVal = OUTPUT_MAX - outputVal;
myPID.run();
analogWrite(6, outputVal);
Serial.println(outputVal);

I only changed the setpoint value (previously it was reading an analog value from a pot), added the Serial.print lines and cleaned it up a bit. Here is the original tutorial.

Later I also tried adding the trim pot, but I still can't control voltage with the microcontroller.

Is this method going to work out in the end or should I try to come up with something else?
This was going to be a part of my big project, so I can't really do anything else before I solve this.
I would love to hear your suggestions!

Please post a wiring diagram. Those poetry type wirings too often leads into misunderstandings.

Perhaps calling analogWrite() too frequently results in random PWM output. Adding a delay in between may help.

Adding delay()? This disgustful piece of code? Joking.

I didn't say delay(). There exist more ways of reducing a repetition rate.

A PID deserves a constant repetition rate or it won't work properly.

@DrDiettrich You are right, my mistake. What way do You suggest? Using millis()?

This would be an extremely dodgy way to control a voltage regulator; if the code crashes it would either turn the regulator fully on or completely off. Not a good idea! :roll_eyes:

And it is indeed complete nonsense, since the potentiometer in this case for the LM2596 is not setting a constant voltage, but is part of the feedback loop which is supposed to control the regulator.

All delays are based on a clock, maybe millis(). Beginners should understand the BlinkWithoutDelay example for repetitions and try to extend it for single shot action.

And as Uwe Paul pointed out the use of PWM is nonsense in this project. A digital pot under control of the Arduino may be usable, but only for setting the desired output voltage without any PID regulation.

So if I can't use PID here, aside from using a digital pot, is there another way?

It's not matter of what you can, but of what you need. No regulator is needed hence no PID. Set the digital pot once to the proper value and you are done.

Bit of an "XY Problem" here.

Why would you want to control an LM2596 based Buck Converter with an Arduino anyway? It does not look practical, so let's see what the real problem is.

I want to make a decent bench supply. I wanted it to be not very big in volume, to have more than one isolated channel and some other stuff.
To reduce the volume, I decided to make it more efficient than just a regular linear regulator. The plan is to lower the voltage coming from the transformer and first running it through a switch mode converter and then through the linear one; this way, the linear regulator doesn't waste as much power and doesn't need a gigantic heatsink.
I also considered not using the linear regulator, but I think that it needs too much capacitance at the output and it causes the supply to have a big delay when changing the voltage. Also, I couldn't find any parts locally that could allow me to do that.

I sorted out most things and am now ready to start prototyping.

Thank you guys for recommending me to use a digital pot. I didn't even know it existed.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.