Hello, my current project is to build a simple proportional feedback system using a voltage divider and potentiometer. I am new to Arduino and programming and am having a little trouble with this one. It seems like the arduino's PWM is incapable of producing negative voltage values, is this true? The system is completely unstable and oscillates between 0 and 5 volts. It seems like what's happening is that it "overshoots" the set value and cannot provide a negative voltage to return back to the set value, it seems like it only gets worse. Here is the code below. I have printed to an LCD both the value I am trying to set (which is 100/1023 volts), and the value in volts that the PWM is producing. Does anyone have any suggestions or tips? Thanks
float pwmValue;
float outputValue;
int Output = 9;
int Pwm = A0;
int V_Out = A5;
void setup(){
pinMode(9,OUTPUT);
pinMode(A0,INPUT);
pinMode(A5,INPUT);
Serial.begin(9600);
}
void loop(){
float Voltage_Out = analogRead(V_Out);
float Pwm_Voltage = analogRead(Pwm);
//read input value: range between (0,1023)
float setpoint = 100;
float gain = 5;
float err = gain*(Voltage_Out-setpoint);
float Feed_Output = err*255/1023;
//PWM can only ouput 255 different values
analogWrite(Output,Feed_Output);
//sensorValue = analogRead(A0);
//outputValue = map(sensorValue, 0, 1023, 0, 500);//scale voltage value to 0-5V
outputValue = Voltage_Out*5/1023;
pwmValue = Pwm_Voltage*5/1023;
Serial.write(0xFE);
Serial.write(0x51);
Serial.print("Voltage = ");
Serial.write(0xFE);
Serial.write(0x45);
Serial.write(0x0E);
Serial.print(" V");
Serial.write(0xFE);
Serial.write(0x45);
Serial.write(0x0A);
Serial.print(outputValue,2);
Serial.write(0xFE);
Serial.write(0x45);
Serial.write(0x40);
Serial.print("Voltage = ");
Serial.write(0xFE);
Serial.write(0x45);
Serial.write(0x4E);
Serial.print(" V");
Serial.write(0xFE);
Serial.write(0x45);
Serial.write(0x4A);
Serial.print(pwmValue,2);
delay(1000); // delay 1ms in between reads for stability
}