How to control LM2596 buck-converter with microcontroller

Summary of How to control LM2596 buck-converter with microcontroller


Summary: The article explains how to adjust a buck converter (like LM2596) output by injecting an external voltage into its feedback pin via a resistor divider. A microcontroller can generate that external voltage using PWM, an RC low-pass filter, and a voltage-follower op amp. Stability of the microcontroller supply is critical because PWM amplitude affects the generated voltage. The article shows calculating the divider resistor when the lower resistor is 330 ohm to scale 5V PWM down to 1.25V, yielding about 990 ohm.

Parts used in the How to control LM2596 buck-converter with microcontroller:

  • LM2596 buck converter module
  • Voltage divider resistors (example: 330 ohm and ~990 ohm)
  • Trimmer resistor (on buck module for manual adjustment)
  • Microcontroller (generates PWM)
  • RC low-pass filter (to smooth PWM)
  • Operational amplifier (configured as voltage follower/buffer)
  • Stable power supply for the microcontroller
  • Connecting resistors to inject external voltage into feedback pin

What a buck module actually exposes

The LM2596 modules sold for a couple of dollars are a fixed-frequency (150 kHz) step-down regulator with an internal switch, an inductor, a Schottky diode and two capacitors. The feedback pin is held at 1.23 V, and the output voltage is set by the divider from Vout to that pin:

Vout = 1.23 V × (1 + R1 / R2)

On the module, one of those resistors is the trimpot. To control the output from a microcontroller you are not “sending a voltage” to the module — you are changing that divider ratio, either by replacing the pot with a digital potentiometer or by injecting current into the feedback node.

Option 1: a digital potentiometer in the divider

Replace the top resistor of the divider with a digital pot used as a rheostat (wiper tied to one end, the other end to the divider node). The output then follows the pot’s code.

  • Voltage limit. Most digital pots specify a maximum terminal voltage of 5 V, and some only 3.3 V. The top resistor sees close to Vout, so a 12 V output will destroy the pot. Feed the pot from a low-voltage node or use a pot rated for the voltage — check the datasheet, do not assume.
  • Resolution. A 100-step pot across the divider range gives about 1 % steps, which at 12 V is 120 mV per step. A 256-step part is better; a 32-step part is a toy.
  • End-to-end resistance tolerance. Digital pots are specified at ±20 % or worse, so the absolute voltage will not match the calculation. Calibrate in firmware against a measured output, or accept a few percent error.
  • Wiper current. Keep the divider current small (a few hundred microamps) — the wiper has a limited current rating and its resistance adds to the divider.

Option 2: PWM into the feedback node

This is the cheap trick: leave the divider alone, and inject a small current into the feedback pin through a resistor from a PWM pin, with an RC low-pass filter so the pin sees a steady voltage rather than a 490 Hz square wave.

MCU PWM ──[ R_f ]──┬── FB (1.23 V)
                   │
                 [ C ]  to ground
                   │
                  GND

The filtered PWM acts as a pull-up or pull-down on the feedback node and shifts the regulation point. Two effects make it awkward:

  • It shifts, not sets. The relationship between duty cycle and output voltage is not the clean divider equation, and it drifts with the LM2596’s own reference tolerance. You end up calibrating a curve.
  • Loop stability. The RC filter’s time constant interacts with the regulator’s compensation. Too little filtering and the output carries the PWM ripple; too much and the loop becomes sluggish and overshoots on load steps.

For a bench supply or a battery charger where you set a voltage and leave it, the digital pot is the cleaner design. For an experiment where you just need the output to move under software control, the PWM injection is fine — with the filter and a calibration table.

What the module will not do

  • It cannot boost. Below about 1.5 V above the input, the LM2596 drops out of regulation. A 12 V input cannot produce 12 V out.
  • It has no current limit you can set. The internal switch has cycle-by-cycle current limiting, but the module has no adjustable constant-current mode. Charging a battery or driving an LED string needs a different topology (a constant-current buck, or a current-sense loop).
  • It is not a precision instrument. The reference is ±4 % over temperature on the plain LM2596, with load and line regulation to match. If you need 1 % you are using the wrong part.
  • Its inductor and capacitors are the cheapest that work. Raising the output voltage raises the ripple; the stock 220 µH inductor and generic electrolytics are chosen for 5–12 V.

A practical recipe

  1. Measure the module’s existing divider: with the pot set mid-travel, note Vout and confirm it matches 1.23 × (1 + R1/R2). That tells you the pot’s actual value, not the label.
  2. Choose a digital pot with a terminal-voltage rating above your maximum Vout, 256 steps, and an SPI or I²C interface your MCU already uses.
  3. Keep the feedback trace short and away from the switch node; the FB pin is high impedance and picks up switching noise.
  4. Add a firmware calibration step: measure Vout at three codes and fit a straight line. Store the coefficients in EEPROM.
  5. Never step the output while the load is sensitive — the LM2596’s transient response to a divider change is a visible glitch. Ramp the code in small increments and let the output settle.

References

  • Texas Instruments, LM2596 SIMPLE SWITCHER power converter datasheet — 1.23 V feedback reference, switching frequency, current limit, dropout and component selection.
  • Microchip, MCP4xxx digital potentiometer family datasheet — terminal voltage limits, wiper current, end-to-end resistance tolerance, rheostat configuration.
  • Atmel/Microchip application notes on PWM-filtered DACs — ripple and settling trade-offs of an RC-filtered PWM into a high-impedance node.

The equations and limits above come from those documents. Specific module behaviour varies by clone; measure yours before trusting a calculated value.

Quick Solutions to Questions related to How to control LM2596 buck-converter with microcontroller:

  • How does the buck converter regulate its output?
    The buck converter adjusts output so the feedback pin voltage (via a divider) becomes about 1.25V; if feedback is higher output lowers and vice versa.
  • Can a microcontroller change the buck converter output?
    Yes, by generating an external voltage via PWM, smoothing it with an RC filter, and buffering with an op amp, then applying it to the feedback junction through a resistor.
  • What components generate the external voltage from the microcontroller?
    PWM from the microcontroller, an RC low-pass filter to smooth it, and an operational amplifier configured as a voltage follower to buffer it.
  • Does the microcontroller power supply matter?
    Yes, the microcontroller supply must be really stable because PWM amplitude depends on it and affects the buck converter output.
  • How do you choose the resistor values for the divider when using 5V PWM and 330 ohm lower resistor?
    Calculate the upper resistor so 5V is divided to 1.25V with the lower resistor 330 ohm; the article shows R ≈ 990 ohm.
  • Why is the adjustable resistor on the module still important?
    The module trimmer sets the converter maximum output and affects relative resistor values so the microcontroller can adjust the output range effectively.
  • What happens if the buck converter maximum output is set much higher than the controlled range?
    The adjustable resistor will be relatively high compared to the fixed 330 ohm, making it easier for the microcontroller-injected voltage to adjust the output down to low values.
  • How is the external voltage applied to the feedback pin?
    Through a resistor that forms a summing junction with the converter output divider so the feedback pin sees the summed voltage.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter
Scroll to Top