mppt code based on perturb and observe algorithm

I am making a arduino mppt controller based on p&o . i have to generate a 10 khz square whose duty ratio changes by p&o algorithm. The problem is , i write this code but my duty ratio is not changing if voltage and current input changed and frequency of output wave that i got is 3khz if voltage and current is 0 and it reduces to around 50-60 hz when supply is on...what is wrong with this code.
code

 int duty = 75;
const int v = A0;
const int I = A1;
int vk = 0;
int ik = 0;
int p = 0;
int delP = 0;
int vprev = 0;
int p_prev = 0;
int delv = 0;
const int wave = 5;

void setup() {
  pinMode(wave,OUTPUT);
  
}

void loop() {
// put your main code here, to run repeatedly:
 vk = analogRead(v);
 ik = analogRead(I);
 
 vk=(5/1023)*vk;   //analog read will give value between 0 to 1023.
 ik=(5/1023)*ik;  
 p = vk*ik;

delP = p - p_prev; 
delv = vk-vprev;
if(delP!=0 || delv !=0)
{
  if (delP > 0){
    if (delv < 0){
     duty++;
    }else{
      duty--;
    }
 } else {
    if (delv<0){
     duty--;
    }else{
     duty++;
    }
    
  }
} 
else{
  duty = duty;    
  }

  
  
 digitalWrite(wave,HIGH);
 delayMicroseconds(duty);
 digitalWrite(wave,LOW);
 delayMicroseconds(100-duty);
 
   p_prev=p;    
   vprev=vk;   
      
}

Fig-3-Perturb-Observe-flowchart.png

1 Like
 vk = analogRead(v);
 ik = analogRead(I);

200us delay right there.

vk=(5/1023)*vk;

Zero times anything is zero.

subhash:
i have to generate a 10 khz square whose duty ratio changes by p&o algorithm.

For a 10kHz square wave I would use one of the hardware timers - I can't recall if they can do PWM at that frequency but if they can then you have a ready-made solution.

Study the relevant Atmega datasheet - you have not said which Arduino you are using.

...R

can u please send me your mppt arduino code?