ESP32 + PZEM-004T V3 communication problem

I have been trying to make a communication between the ESP32 and the PZEM-004T V3 sensor but unable to do it.
The PZEM-004T V3 is working fine on the Uno.
Please help me out.

Here is the code I am using

#include <PZEM004Tv30.h>
#include <HardwareSerial.h>

#define RXD2 16
#define TXD2 17




PZEM004Tv30 pzem(&Serial2);

void setup() {

  Serial.begin(115200);
  Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
  
}

void loop() {
    float voltage = pzem.voltage();
    if(!isnan(voltage)){
        Serial.print("Voltage: "); Serial.print(voltage); Serial.println("V");
    } else {
        Serial.println("Error reading voltage");
    }

    float current = pzem.current();
    if(!isnan(current)){
        Serial.print("Current: "); Serial.print(current); Serial.println("A");
    } else {
        Serial.println("Error reading current");
    }

    float power = pzem.power();
    if(!isnan(power)){
        Serial.print("Power: "); Serial.print(power); Serial.println("W");
    } else {
        Serial.println("Error reading power");
    }

    float energy = pzem.energy();
    if(!isnan(energy)){
        Serial.print("Energy: "); Serial.print(energy,3); Serial.println("kWh");
    } else {
        Serial.println("Error reading energy");
    }

    float frequency = pzem.frequency();
    if(!isnan(frequency)){
        Serial.print("Frequency: "); Serial.print(frequency, 1); Serial.println("Hz");
    } else {
        Serial.println("Error reading frequency");
    }

    float pf = pzem.pf();
    if(!isnan(pf)){
        Serial.print("PF: "); Serial.println(pf);
    } else {
        Serial.println("Error reading power factor");
    }

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

What voltage does the sensor work at and how are you powering it when using it with the ESP32 ?

What happens when you use it with the ESP32 ?

The Pzem-004t is completely isolated by 2 optoisolators CT817C.
I am powering it with 5V supply and pushing TX from the ESP32 to its RX @ 3.3v and using a voltage divider to receive the data from the module to the ESP32.

I can see that the RX led on the module is blinking faintly, but the TX is not.

Below is the attached schematic of the PZEM-004T V3

Have you tried swapping the RX TX cable?
Now I use PZEM 3 Modules

//----------------------------------------
#include <PZEM004Tv30.h>
#include <HardwareSerial.h>

//--- Phase A
#define RX2 14
#define TX2 12


PZEM004Tv30 pzem_A(&Serial2);

float voltage_A;
float current_A;
float power_A;
float energy_A;
float frequency_A;
float pf_A;

//-----------------------------------

//--- Phase B

#define RX1 13
#define TX1 15

PZEM004Tv30 pzem_B(&Serial1);

float voltage_B;
float current_B;
float power_B;
float energy_B;
float frequency_B;
float pf_B;

//-----------------------------------

//--- Phase C

//#define RX1 13
//#define TX1 15

PZEM004Tv30 pzem_C(&Serial);

float voltage_C;
float current_C;
float power_C;
float energy_C;
float frequency_C;
float pf_C;

//-----------------------------------
void setup()
{
Serial2.begin(9600, SERIAL_8N1, RX2, TX2);
  Serial1.begin(9600, SERIAL_8N1, RX1, TX1);
  Serial.begin(9600, SERIAL_8N1);
}

"C:\Users\User name\AppData\Local\Arduino15\packages\esp32\hardware\esp32\1.0.6\cores\esp32\HardwareSerial.cpp"

and change pin:

if(_uart_nr == 0 && rxPin < 0 && txPin < 0) {
rxPin = 16;
txPin = 17;
}
if(_uart_nr == 1 && rxPin < 0 && txPin < 0) {
rxPin = 13;
txPin = 15;
}
if(_uart_nr == 2 && rxPin < 0 && txPin < 0) {
rxPin = 12;
txPin = 14;
}

Has the OP tried to properly initialize the hardware serial port of the ESP32?

A search on the internet wail explain how to properly initialize the serial port of an ESP32.

Some clues!

#include <HardwareSerial.h>

HardwareSerial SerialTFMini( 1 );
HardwareSerial SerialBrain( 2 );
////// serial(1) = pin27=RX green, pin26=TX white
////// serial(2) = pin16=RXgreen , pin17=TX white
setup()
{
 Serial.begin( SerialDataBits );
  SerialBrain.begin( SerialDataBits );
  SerialTFMini.begin(  SerialDataBits, SERIAL_8N1, 27, 26 );

}

Yup using all 3 of the 4 serial ports.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.