Unstable duty ratio PWM on pin 5 & 6 @ 62.5kHz

I trying to generate a PWM at 62.5kHz,the code which im using is as follows:

the duty ratio keeps varying for a given/fixed input on the analog pin 2

can some one please tell me why this might be happening??

#include <PID_AutoTune_v0.h>
#include <PID_v1.h>
#include <math.h>

const byte pwPin1 = 5;
const byte pwPin2 = 6;

int analogPin2 = 3;
int analogPin1 = 1;

//double pidPin = 5;

float v1 = 0;
float boostDuty = 0;
float buckDuty = 0;

//double Input1 = 0.0;
//double Output1 = 0.0;
//double Setpoint1 = 778.0;

//double aggKp1 = 0.1, aggKi1 = 5, aggKd1 = 0;              // settiing aggressive values of Kp Ki Kd for PID1
//double consKp1 = 0.01, consKi1 = 2, consKd1 = 0;    // settiing conservative values of Kp Ki Kd for PID1

//PID myPID1(&Input1, &Output1, &Setpoint1, consKp1, consKi1, consKd1, DIRECT);        //PID

void setup()
{
  pinMode(pwPin1, OUTPUT);
  pinMode(pwPin2, OUTPUT);
  //Setpoint1 = 778;                                        // set point for PID1
  //myPID1.SetMode(AUTOMATIC);                                // turn ON PID1
  TCCR0B = TCCR0B & B11111000 | B00000001;
}

void loop()
{
 
  //Input1 = analogRead(pidPin);
  //double gap1 = abs(Setpoint1 - Input1);                 //distance away from setpoint

  //if (gap1 < 10)
  //{
  // myPID1.SetTunings(consKp1, consKi1, consKd1);
  //}
  //else
  //{
  //myPID1.SetTunings(aggKp1, aggKi1, aggKd1);
  //}

  v1 = analogRead(analogPin2);
  float voltage = v1 * (5 / 1023);

  while (v1 > 1.5 && v1 < 2.5)
  {
    //Input1 = analogRead(pidPin);
    v1 = analogRead(analogPin2);
    boostDuty = 1 - (v1 / 3.8);//calculate boost  duty ratio
    //myPID1.Compute();
    analogWrite( 5, (boostDuty *100));
    if (v1 == 3.7)
    { break;
    }
  }

}

//v1 = analogRead(analogPin2);
//float voltage = v1 * (5 / 1023);
//while (v1 <= 1.5)
//{
// buckDuty = 511 / 804;
//analogWrite( 5, (buckDuty));
//}
int analogPin2 = 3;

LOL

I added in a serial print:

    Serial.println (boostDuty * 100);

Results:

47.37
-215.79
21.05
47.37
-347.37
47.37
-321.05
47.37
-268.42
47.37
-321.05
47.37
-268.42
47.37
-321.05
47.37
-163.16
47.37
-347.37

I'm a little concerned about those negative numbers. Perhaps you should fix that?

First -

the duty ratio keeps varying for a given/fixed input on the analog pin 2

What do you mean keeps varying, by how much?

Second - you never expect get a fixed value from an input on an ADC. The best you can ever expect to get is +/- 1

Third -

    v1 = analogRead(analogPin2);
    boostDuty = 1 - (v1 / 3.8);//calculate boost  duty ratio
    //myPID1.Compute();
    analogWrite( 5, (boostDuty *100));

is junk! There is almost no chance of boostDuty being in the range 0-255 (hint - most of the time it will be -ve and thats just for starters)

Mark

  v1 = analogRead(analogPin2);
  float voltage = v1 * (5 / 1023);

  while (v1 > 1.5 && v1 < 2.5)
  {

Do you mean:

  while (voltage > 1.5 && voltage < 2.5)

  float voltage = v1 * (5 / 1023);

(5/1023) will be zero. Any reading times zero will be zero. Perhaps:

  float voltage = v1 * (5.0 / 1023);
int analogPin2 = 3;

This is such a silly name, it is hard to know where to start.

the duty ratio keeps varying for a given/fixed input on the analog pin 2

Even you are confused. Perhaps you mean "on analog pin 3"?

Or maybe "on analog pin 3, which I am calling analogPin2, just to confuse myself".

Here's a tip: Give it a proper (meaningful) name. Like:

const int controlPotPin = 3;

That's assuming you have a control potentiometer on pin 3.

duty ratio varies between 30% & 90%

im new to programming any good books you can suggest to learn programming?

Hi,
What is the application for this sketch?
What do you want it to do?

Tom..... :slight_smile:

hi Tom!

The sketch is control a bi-directional dc dc converter, the converter input if fed from a 2.5V super capacitor, the converter input voltage varies from 2.5V to 1.5V (as cap. discharges) with a constant output voltage of 3.8V @900mA (boost mode, in closed loop with Arduino PID function ).

Buck mode - the converter output voltage is 2.5V which is used to charge the super cap. (in reverse direction), input voltage is 8V.

The switching freq. is 62.5khz..., im using pin 5 & 6 to select the buck and boost mode...this is part of my mini project...and i need some help with the coding...

:slight_smile: Emmanuel

Nick/Mark/Tom

Thanks for the corrections the code is working...

Emmanuel