This is the code used with my digital power supply. You have the schematic below. You will need the .tft file for the Nextion display as well. You won't need any extra libraries. Just download or copy the code from below and uplaode it to the Arduino.
/* Code for digital power supply made with Arduino and Nextion display
* Tutorial webpage: https://www.electronoobs.com/eng_arduino_tut64.php
* Schematic: https://www.electronoobs.com/eng_arduino_tut64_sch1.php
* Part list: https://www.electronoobs.com/eng_arduino_tut64_parts1.php
*/
int pwm = 3;
int current_in = A0;
int feedback = A2;
int output_enable_pin = 4;
int PWM_value = 1;
float Volt_set = 0; //Variables for the ones voltage values
float Volt_ten_set = 0; //Variables for the tenths voltage values
float Volt_hun_set = 0; //Variables for the hundredths voltage values
float Curr_set = 0; //Variables for the ones current values
float Curr_ten_set = 0; //Variables for the tenths current values
float Curr_hun_set = 0; //Variables for the hundredths current values
float Voltage_set = 5.0; //Here we save the final voltage value (teh sum of all 3 votage variables above)
float Current_set = 0.5; //Here we save the final current value (teh sum of all 3 current variables above)
int Real_Voltage = 0; //Variable for the real read of the voltage output
int Real_Current = 0; //Variable for the real read of the current value
float Real_Power = 0; //The power will be current*voltage
bool output_enabeled = false; //This will show if the output is enabeled or not
unsigned long previousMillis = 0;
int Delay = 1000;
////////////////////////////////////////////////////////////////////////////////////////////////////////////
//ADC sampling variables
double Current_adc;
int adc = 0;
#define ADC_MULTISAMPLING 2
#define ADC_MULTISAMPLING_SAMPLES (1 << ADC_MULTISAMPLING)
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup() {
Serial.begin(9600); //Or change to 115200 if your NEXTION display works at that speed
pinMode(feedback,INPUT);
pinMode(current_in,INPUT);
pinMode(pwm,OUTPUT);
pinMode(output_enable_pin,OUTPUT);
digitalWrite(output_enable_pin,LOW); //output relay turned OFF when we start
TCCR2B = TCCR2B & B11111000 | B00000001; // pin 3 and 11 PWM frequency of 31372.55 Hz
Serial.print("cle 5,255"); //Send instruction to clear the waveform
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
void loop() {
if(Serial.available())
{
String Received = Serial.readString(); //get the received string from the display
//Serial.println(Received);
if(Received[0] == 'b') /*In the display code, when the back button is pressed I send all the variables
But first I send the "b" character. That's why, when we detect a "b", then we store each forth character as a float value.
why each forth? because after each sent value, the uartt will write 3 full bytes as seen before*/
{
Volt_set = float(Received[1]); //First, than the 5th, than the 9th etc...
Volt_ten_set = float(Received[5]);
Volt_hun_set = float(Received[9]);
Curr_set = float(Received[13]);
Curr_ten_set = float(Received[17]);
Curr_hun_set = float(Received[21]);
Voltage_set = Volt_set + (Volt_ten_set/10) + (Volt_hun_set/100); //We summ all parts to get the total set voltage value
Current_set = Curr_set + (Curr_ten_set/10) + (Curr_hun_set/100); //We summ all parts to get the total set current value
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
Serial.print(Voltage_set); Serial.print(" "); Serial.println(Current_set); //Uncomment this for serial debug
}
if(Received[1] == 'f') //if we receive an f, then the output will turn off
{
output_enabeled = false;
//Serial.print("Output: ");
//Serial.println(output_enabeled);
}
if(Received[1] == 'n') //if we receive an n, then the output will turn on
{
output_enabeled = true;
//Serial.print("Output: ");
//Serial.println(output_enabeled);
}
}//end of if serial
if(output_enabeled)
{
digitalWrite(output_enable_pin,HIGH); //output relay turned ON
}
if(!output_enabeled)
{
digitalWrite(output_enable_pin,LOW); //output relay turned ON
}
float feedback_read = analogRead(feedback);
feedback_read = (feedback_read / (204.8 * 0.175438)); //204.8 x 0.175438
int feedback_read_send = int(feedback_read * 100);
float current_read = read_current();
current_read = current_read / 204.8;
int current_read_send = int(current_read * 100);
Real_Power = feedback_read * current_read;
int Real_Power_send = int(Real_Power * 100);
if( current_read > Current_set)
{
PWM_value = PWM_value+1;
PWM_value = constrain(PWM_value, 1, 254);
}
else
{
if (Voltage_set > feedback_read)
{
PWM_value = PWM_value-1;
PWM_value = constrain(PWM_value, 1, 254);
}
if (Voltage_set < feedback_read)
{
PWM_value = PWM_value+1;
PWM_value = constrain(PWM_value, 1, 254);
}
}//end of else
analogWrite(pwm,PWM_value);
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= Delay)
{
previousMillis += Delay;
Serial.print("Real_Voltage.val="); //The variable we will change on nthe screen, in this case the voltage value
Serial.print(feedback_read_send); //Send the voltage value
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.print("Real_Current.val="); //The variable we will change on nthe screen, in this case the current value
Serial.print(current_read_send); //Send the current value
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
Serial.print("Real_Power.val="); //The variable we will change on nthe screen, in this case the power value
Serial.print(Real_Power_send); //Send the power value
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
value_to_waveform(feedback_read_send/13); //use this function to send data to the waveform
}
}
float read_current()
{
// Filter the ADC by multisampling with the values defined at the beginning
adc = 0;
for (int i = 0; i < ADC_MULTISAMPLING_SAMPLES; ++i)
adc += analogRead(current_in);
adc = adc >> ADC_MULTISAMPLING;
double adcread = adc;
Current_adc += (adcread - Current_adc) * 0.05;
return(Current_adc);
}
//function that sends data to the waveform
void value_to_waveform(int val_wave)
{
String Tosend = "add ";
Tosend += 2;
Tosend += ",";
Tosend += 0;
Tosend += ",";
Tosend += val_wave;
Serial.print(Tosend);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}