This is the schematic for this Feedback example but with feedback. Make the connections and get the code from below.
The code is just an example with Feedback. We read the value from the potentiometer and also the output and depending on that, we create a fast PWM signal and apply that to the BJT and then to the MOSFET. In this way we control the output voltage.
/* This is an example code for a Flyback converter with Arduino *
* Subscribe: http://www.youtube.com/c/electronoobs
* Tutorial: http://www.electronoobs.com/eng_circuitos_tut38.php
*/
int potentiometer = A0;
int feedback = A1;
int PWM = 3;
int pwm = 0;
void setup() {
pinMode(potentiometer, INPUT);
pinMode(feedback, INPUT);
pinMode(PWM, OUTPUT);
TCCR2B = TCCR2B & B11111000 | B00000001; // pin 3 and 11 PWM frequency of 31372.55 Hz
}
void loop() {
float voltage = analogRead(potentiometer);
float output = analogRead(feedback);
if (voltage > output)
{
pwm = pwm-1;
pwm = constrain(pwm, 1, 254);
}
if (voltage < output)
{
pwm = pwm+1;
pwm = constrain(pwm, 1, 254);
}
analogWrite(PWM,pwm);
}