I am implementing current control on my synchronous buck converter using ARDUINO UNO. I designed my control via "Symmetrical Optimum" method and calculated the values for Kp and Ki (not Kd since i am implementing PI control). I am implementing the control using the Arduino's PID Library. I have the following two questions regarding this:

(P.S : it is my first time implementing control, so i might be asking some really basic things)

1) When using this PID library, if i only want to use the PI, i will pass the value "0" to the Kd parameter, and this will do the job, right ?

2) My Buck converter switching frequency is 62.5kHz, so i calculated my sampling time to be 16us from this. And i have used this value of sampling time i.e 16us in my PI design method to calculate the value of Kp and Ki. And also we are supposed to multiply this value (16us) with Ki while implementing the control in the micro-controller (this is done in SetSampleTime() function as well). Now i am supposed to pass this value of sampling time to the SetSampleTime() fucntion. But i am confused here, and the confusion is that i dont want my controller to be very fast (i.e work for 16us) as i will be charging a battery using this controller, so getting values (updating the controller) at 1 second interval may be also good enough. So is it okay for me to pass some other value of sampling time, like may be 100ms to the SetSampleTime instead of 16us ? I mean can i just pass this value in my current scenario ? or shall i again calculate the values of Kp and Ki using the 100ms as sampling frequency, and then only i can pass this value of sampling time(100ms) to my SetSampleTime() fucntion ?

To explain my confusion a bit more, the value of "Sample Time" is being used at two places in the PID library : one, it is being used to tell the controller when to update it self, and two, it is being multiplied by Ki parameter. So i am confused that i want to sample slowly than 16us, but i have calculated the Kp, and Ki using 16us as the sampling time. So what would be the right way to go if i want to sample slowly than 16us ? I hope i am able to make myself clear on the second point.

You helpful suggestions and comments would be appreciated.


  • \$\begingroup\$ I took a look on that PID library, people who wrote it, have no idea how to program a disctrete PID algorithm. Waste of time. \$\endgroup\$ Commented Nov 10, 2015 at 19:10

1 Answer 1


Try this:

// parameters are T0..sample time[s], Kp..gain, Ti..integrating time[s]
//At init ek1, uk1 = 0

// compute coeficients at init and at each parameter change 
q0 = Kp*(1 + T0/(2*Ti));
q1 = -Kp*(1 - T0/(2*Ti));

//execute this in ISR at every sample time - timer interrupt 
// u..output
ek1 = ek;
ek = setpoint - actual;

u = q0*ek + q1*ek1 + uk1;

if u > u_max then
   u = u_max;
elsif u < u_min then
   u = u_min;

uk1 = u;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.