Convert float to string pzem

hello all can you guys help me, to convert float to string? I will show the program below, thank you

#include <PZEM004Tv30.h>
#include <SoftwareSerial.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
float V1;
float C2;
float F3;

#if !defined(PZEM_RX_PIN) && !defined(PZEM_TX_PIN) //Arduino
#define PZEM_RX_PIN 2 // Pin 2 Rx Arduino ke Pin Tx Pzem
#define PZEM_TX_PIN 3 // Pin 3 Tx Arduino ke Pin Rx Pzem
#endif
#define RELAY_1 4

SoftwareSerial pzemSWSerial(PZEM_RX_PIN, PZEM_TX_PIN);
PZEM004Tv30 pzem(pzemSWSerial);
void setup() {

    Serial.begin(115200);
    pinMode(RELAY_1, OUTPUT); //Relay
    lcd.init();                      // initialize the lcd
    lcd.backlight();
    lcd.setCursor(0,0);
    lcd.print("anjai mabar"); 
    delay(1000);
    lcd.clear();
}

void loop(){
         
    Serial.print("Custom Address:");
    Serial.println(pzem.readAddress(), HEX);
        
    float voltage = pzem.voltage();
    if (!isnan(voltage)){
      V1 = voltage;
      Serial.print("Tegangan");Serial.print(V1);Serial.println("V");
    }else{
      V1 = 0.0;
      Serial.print("Tegangan");Serial.print(V1);Serial.println("V");
    }

    lcd.setCursor(0,1);lcd.print("Tegangan : ");
    lcd.setCursor(10,1);lcd.print(V1);
    lcd.setCursor(17,1);lcd.print("V");
    
    if(V1 < 200)
    {
    digitalWrite(4, LOW);
    lcd.setCursor(0,1);
    }else if (V1 > 201){
    digitalWrite(4, HIGH);
    }

    float current = pzem.current();
    if (!isnan(current)){
      C2 = current;
      Serial.print("Arus");Serial.print(C2);Serial.println("A");
    }else{
      C2 = 0.0;
      Serial.print("Arus");Serial.print(C2);Serial.println("A");
    }

    lcd.setCursor(0,2);lcd.print("Arus     : ");
    lcd.setCursor(10,2);lcd.print(C2);
    lcd.setCursor(17,2);lcd.print("A");

    float frequency = pzem.frequency();
    if (!isnan(frequency)){
      F3 = frequency;
      Serial.print("Frekuensi");Serial.print(F3);Serial.println("Hz");
    }else{
      F3 = 0.0;
      Serial.print("Frekuensi");Serial.print(F3);Serial.println("Hz");
    }

    lcd.setCursor(0,3);lcd.print("Frekuensi: ");
    lcd.setCursor(10,3);lcd.print(F3);
    lcd.setCursor(17,3); lcd.print("Hz");

    Serial.println();
    delay(2000);
}
  

You can Use the dtostr() function to convert your float to its ASCII decimal representation

But the real question is why do you need it at all?

This can be simplified to:

    V1 = pzem.voltage();
    if (isnan(V1))
      V1 = 0.0 ;
    Serial.print("Tegangan");
    Serial.print(V1);
    Serial.println("V");

Similar for current and frequency.

how to make lcd not display last data from pzem? if the source is removed? current position if the source is removed, then the arduino needs to be reset to display the number 0.

Follow the advice from @J-M-L . The second parameter of dtostrf() sets the minimum field width for the number. Using that will ensure that any previous data is always completely overwritten with the new data.

do you have any reference about dtostrf()? I'm having trouble making the program

Do you have Google?

If you have to reset the Arduino to get it to display zero then it sounds like pzem.voltage(), pzem.current(), and pzem.frequency() are not returning. That sounds like a problem in the library. You may have to change the library to implement a timeout is no data arrives from the serial connection.

dtostrf()

Maybe it's just a formatting problem on the LCD.

Example: if the voltage is 123.45, the LCD shows

123.45V

but then the sensor is disconnected, so the function returns NaN which is replaced with 0.00 and the display then updates to

0.00V5V

Resetting the Arduino would fix this because LCD.begin() probably clears all the text from the LCD.

Oh. If that's what they mean then just write spaces over the field:

    lcd.setCursor(0,1); lcd.print("Tegangan : ");
    lcd.setCursor(10,1); lcd.print("       ");
    lcd.setCursor(10,1); lcd.print(V1);
    lcd.setCursor(17,1); lcd.print("V");

Yes, that would work, but might also cause some flickering. Using dtostrf() would not flicker and would also ensure that the "." remains in the same place for every value.

is this how to use the dtostrf() function?

char chr[8];
void setup() {
}

void loop(){
        
    V1 = pzem.voltage();
    if (isnan(V1))
      V1 = 0.0 ;
    Serial.print("Tegangan");
    Serial.print(V1);
    Serial.println("V");
    dtostrf(V1, 7, 3, chr);
}

Yes, but it won't be much help until you change:
lcd.setCursor(10,1);lcd.print(V1);
to
lcd.setCursor(10,1);lcd.print(chr);

it works, and you know that I haven't changed V1 to chr. thanks

I've tried using the dtosrf() function, but why is it only the voltage that returns to 0? for current and frequency still display the last data?

    V1 = pzem.voltage();
    if (isnan(V1))
      V1 = 0.0 ;
    Serial.print("Tegangan");
    Serial.print(V1);
    Serial.println("V");
    dtostrf(V1, 5, 2, chr1);

    lcd.setCursor(0,1);lcd.print("Tegangan : ");
    lcd.setCursor(10,1);lcd.print(chr1);
    lcd.setCursor(17,1);lcd.print("V");

    C2 = pzem.current();
    if (isnan(C2))
      C2 = 0.0 ;
    Serial.print("Arus     :");
    Serial.print(C2);
    Serial.println("A");
    dtostrf(C2, 6, 2, chr1);

    lcd.setCursor(0,2);lcd.print("Arus     : ");
    lcd.setCursor(10,2);lcd.print(chr1);
    lcd.setCursor(17,2);lcd.print("A");


    F3 = pzem.frequency();
    if (isnan(F3))
      F3 = 0.0 ;
    Serial.print("Frekuensi:");
    Serial.print(F3);
    Serial.println("Hz");
    dtostrf(F3, 6, 2, chr1);

    lcd.setCursor(0,3);lcd.print("Frekuensi: ");
    lcd.setCursor(10,3);lcd.print(chr1);
    lcd.setCursor(17,3); lcd.print("Hz");

Did you forget to use dtostrf() in all the other LCD output?

it seems not, or is it true that the current and frequency can't be 0 when the source is unplugged?

We don't know enough about your project to be able to answer that.

Is chr1 long enough to hold the values of C2 and F3? Maybe try increasing the length of chr1.

As an example, if F3 == 30000 Hz then

dtostrf(F3, 6, 2, chr1);

will not work because only 3 digits are allowed to the left of the "."