I got rid of the active control and it works. The pwm is stable and the current sense resistor voltage is stable . Will post photos after work today.
V=8.5V
V/2 =4.25V
pwm_cmd =216
5V/254= 0.01968503937007874015748031496063 V
216* 0.01968503937007874015748031496063V = 4.25 V
1- I admit I sort of missed the boat by forgetting the op amp was the control element and not the code.
2- The scope is accurate despite the fact that it is old. Everything we have seen can be explained by the code. Your original comment that the ramp wave has nothing to do with the PWM was correct when you didn't know I was doing unnecessary software correction with the IF statements. After removing that code the ramp wave (that everyone is calling "oscillation" when it is not) is gone. I think you have been using better scopes for so long you no longer trust the old ones which still work well within their limits. My scope is fine.
3- The circuit is stable once I removed the If statements. The current sense resistor voltage is rock solid, unlike the first video where it was jumping around. After removing the serial prints the voltage was much more stable but the pwm was still rapidly changing until I removed the IF statements.
4- I'm using Linear LT1215s and it is an excellent op amp.
5- What we were seeing before was not oscillation per se but the code incrementing and decrementing the pwm command and then overshooting because the serial print is tying up the processor.
6- The pwm is so stable now that I don't need a video. It would be pointless. A photo will suffice.
C = 216: (4.2519 V)
R = 2.2058 ohms
I = C/R = 4.2519 V/2.2058 ohms =1.927528 A
Here's the code with C =V/2 (| where V = 8.5 V, R = 2.2058 ohms)
int pwm_pin = 3;
int pwm_cmd = 216; //((8.5/2)/(5/254)
int voltage_cnts;
float voltage;
float current;
void setup()
{
// put your setup code here, to run once:
Serial.begin(115200);
pinMode(pwm_pin,OUTPUT);
}
void loop()
{
// put your main code here, to run repeatedly:
voltage_cnts = analogRead(A0);
voltage = voltage_cnts * 0.004887585532746823069403714565;
current = voltage/2;
Serial.println(current,7);
// delay(1);
analogWrite(pwm_pin,pwm_cmd);
}
Here's the photo of the PWM
Ch-1: Amp/Div = 2V/div
Ch-2: Amp/Div = 2V/div
Time/Div = 0.5 mS
Note: resistance of R obtained from bench power supply set to 6.0 V
Current displayed : 2.72 A => R = V/I = 6.0/2.72 = 2.2058
