I'm trying to create a digitally controlled voltage which I will end up using for a software controlled voltage divider for a high voltage circuit control around 600v.
For now I am trying to get it to work on 5v.
This is the general idea

I want precise output voltages so I want to negate any non-linearities in the transistor with digital feedback from a voltage sense IC or something.
So basically I'll tell the controller that I want x voltage on the output and if the voltage on the output reads more or less than that voltage then it will self correct.
This is the code I'm using right now
I'm using a potentiometer to adjust the reference voltage and then I'm converting the reverence voltage number into analogWrite voltage which is then read by analogRead at the output and the output is adjusted based on whether the output is lower or higher than the reference number.
It works but it is not stable, it fluctuates all over within a volt or more.
Is there some problem in the concept of my code or something?
I only started learning digital electronics and coding a couple days ago.
Moderators note: changed code tags to C
For now I am trying to get it to work on 5v.
This is the general idea

I want precise output voltages so I want to negate any non-linearities in the transistor with digital feedback from a voltage sense IC or something.
So basically I'll tell the controller that I want x voltage on the output and if the voltage on the output reads more or less than that voltage then it will self correct.
This is the code I'm using right now
C:
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(13,OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
//Power source for adjustable voltage
digitalWrite(13, HIGH);
// Reads potentiometer voltage to get a reference number used for intended output voltage
int reference = analogRead(A1);
// Takes analogRead value and turns it into the intended voltage reference number
float ReferenceNumber = reference * (5.0 / 1023.0);
// Translation from intended voltage number to analogWrite voltage
float output=ReferenceNumber*(225/5.0);
// output to transistor base
analogWrite(9,output);
// reads the output voltage for feedback control
int feedback = analogRead(A0);
// If the output voltage is greater or less than the intended voltage then the opto-isolator bias will be adjusted.
if (feedback*(255/1023.0) > output ) {
output = -1 ;}
if (feedback*(255/1023.0) < output ) {
output = +1 ;}
//Prints the output voltage
Serial.println(feedback* (5.0 / 1023.0));
}
It works but it is not stable, it fluctuates all over within a volt or more.
Is there some problem in the concept of my code or something?
I only started learning digital electronics and coding a couple days ago.
Moderators note: changed code tags to C
Last edited by a moderator: