PID control

Hi, I’m new to Arduino. Sorry if there is/are any unclear stuffs/mistakes I made here. I’m trying to control a 3-phase inverter and is using PI controller to do that (varies the PWM). I’m reading current, ac voltage and dc voltage to my board (Arduino Mega2560).

I’m attempting to make a current PI control (see attached) like this.

The analog readings give value range in 0-1023 and my calculate kp = 2512 , ki = 2512 . So I try coding my PID like this:


PID dCurrent(&d, &d_out, &dref, kp, ki, 0, DIRECT); 
PID qCurrent(&q, &q_out, &qref, kp, ki, 0, DIRECT); 

void setup() {
  // put your setup code here, to run once:
  dref=100; (for current=0.5A)
  dCurrent.SetMode(AUTOMATIC);       //Turn CCD_PID on
  qCurrent.SetMode(AUTOMATIC);       //Turn CCQ_PID on

void loop() {




And my issue is here, the modulation ratio is given as (2*Vout/Vdc), I calculate the output value from the PID would be more than 1023 and my Vdc reading is in the range of 0-1023. And PID output is in the range of 0-255. How do I scale the PID output (giving the range in 0-1023) such that I could get the proportional modulation ratio?

Any helps would be appreciated and thanks for reading.

Current Control.PNG

Multiply by 1023.0/255.0 ?

In other words, multiply by about 4.0 ?

Thank you for your reply.

But my calculate kp=25.12 (=L*w=10e-3*0.1*2*pi*40000) & ki=2512 (=R*w=0.1*0.1*2*pi*40000), wont this give a value that exceed 1023 or 255 and PID output would only give me 255. How do I scale it proportionally? (should I scale the ki & kp or should I scale the output?)

I'm not able to translate the current control model into actual implementation.

Thanks a lot.