Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple pzem-004t, only one communicates when together, but work fine separately #135

Open
TheodorAraps opened this issue Dec 8, 2024 · 5 comments

Comments

@TheodorAraps
Copy link

TheodorAraps commented Dec 8, 2024

Hello, and thanks for this great library. I am trying to create a 3 phase meter using arduino leonardo based MCU and pzem-004t V3.0. For now i am just trying to make 2 of the pzem004t work in tha same modbus.
For some reason i only managed to make each one (individually connected) to work fine only through hardware serial, using Serial1 (witch are pins 0 and 1 of leonardo used for Tx and Rx). I managed to also set addresses of 0x01 and 0x02 for the 2 pzems using the change address code in your repo. From your code for multiple pzems i am using the Serial2 version (change it to serial1 for my case) and alsa changed baud rate to 9600 cause it doesn't work with 115200. Also some info about the pzems and the circuit:
One of the pzem004t (when connected individually) works only with 3v3 source of arduino and not th 5V which is super weird. It has 348K optocouplers. The other works with both 5V and 3v3 and has 139K optocouplers. So for my implementation i am using 3v3 so that both can work. Moreover, using the code i provide you, when i have only one pzem connected at a time (either one) it works fine and the data gets printed correctly. But when i connect both of them, only the pzem with the 139K optocouplers prints results. The other prints "Error reading voltage" (NaN values). Also when th commands are exectuted the lights on the pzems blink as they are supposed to, as if they are working correctly. So something is wrong with the modbus setup? What could be the problem?

IMG_20241208_200947
IMG_20241208_201016

The code i use is the following:

#include <PZEM004Tv30.h>

#define PZEM_SERIAL Serial1
#define NUM_PZEMS 2

PZEM004Tv30 pzems[NUM_PZEMS];

void setup() {
    Serial.begin(9600);

    // For each PZEM, initialize it
    for(int i = 0; i < NUM_PZEMS; i++)
    { 

        // Initialize the PZEMs with Hardware Serial1 on the default pins

        pzems[i] = PZEM004Tv30(Serial1, 0x01 + i);
    }
}

void loop() {
    for(int i = 0; i < NUM_PZEMS; i++){
        // Print the Address of the PZEM
        Serial.print("PZEM ");
        Serial.print(i);
        Serial.print(" - Address:");
        Serial.println(pzems[i].getAddress(), HEX);
        Serial.println(pzems[i].readAddress(), HEX);    
        Serial.println("===================");

        // Read the data from the sensor
        float voltage = pzems[i].voltage();
        float current = pzems[i].current();
        float power = pzems[i].power();
        float energy = pzems[i].energy();
        float frequency = pzems[i].frequency();
        float pf = pzems[i].pf();


        // Check if the data is valid
        if(isnan(voltage)){
            Serial.println("Error reading voltage");
        } else if (isnan(current)) {
            Serial.println("Error reading current");
        } else if (isnan(power)) {
            Serial.println("Error reading power");
        } else if (isnan(energy)) {
            Serial.println("Error reading energy");
        } else if (isnan(frequency)) {
            Serial.println("Error reading frequency");
        } else if (isnan(pf)) {
            Serial.println("Error reading power factor");
        } else {
            // Print the values to the Serial console
            Serial.print("Voltage: ");      Serial.print(voltage);      Serial.println("V");
            Serial.print("Current: ");      Serial.print(current);      Serial.println("A");
            Serial.print("Power: ");        Serial.print(power);        Serial.println("W");
            Serial.print("Energy: ");       Serial.print(energy,3);     Serial.println("kWh");
            Serial.print("Frequency: ");    Serial.print(frequency, 1); Serial.println("Hz");
            Serial.print("PF: ");           Serial.println(pf);

        }

        Serial.println("-------------------");
        Serial.println();
        delay(200);
    }

    Serial.println();
    delay(3000);
} 
@mandulaj
Copy link
Owner

It might be that when you connect them in parallel, the opto-coupler with lower input impedance just steals all the current and doesn't allow the other one to activate properly. This is the rough schematic:
image

R8,R10, R4 and R9 should be the same on all of the devices.... If one has lower resistnace, that opto coupler will turn on first and all others on the line won't glow.

Either you replace the resistors in order to match them, or you use transistors on the line to source the current for each PZEM individually or you put the PZEMs on different busses.

@TheodorAraps
Copy link
Author

TheodorAraps commented Dec 10, 2024

Thanks for the quick reply! Oh i see i was suspecting something like that. Could diodes also help, like in the image? I have order some so i will try it like this. If it doesn't work i will try some of your solutions.

39309054-7c33a9a4-493d-11e8-832e-70dac98c1bf6

@mandulaj
Copy link
Owner

I don't see how that would help anything... You are not eliminating one of the weaker PZEMs taking ownership of the bus. But try it and let me know if it works. 😄

@TheodorAraps
Copy link
Author

TheodorAraps commented Dec 11, 2024

As you expected, it didn't work 😅 waiting for transistors to arrive to try firstly this approach you suggested to regulate the current. I also ordered more pzems (as a last resort) from the same store and hope they are the same resistance... Generally i don't want to end up modifying the hardware so i will avoid this approach. Also i don't have another hardware serial pin on the arduino so i can't pute them in different busses, so i can't follow this approach either. Thanks for the amazing suggestions and i will let you know if i succeed with the transistors. 😊

@TheodorAraps
Copy link
Author

TheodorAraps commented Dec 13, 2024

I didn't succeed with the transistors. I used 2N2222A NPN (0.8A) transistors and the circuit i made is the following:
Messenger_creation_48096A95-792C-4233-B58C-C4F6B68F833B
Did i do something wrong?
Also you said that R4 R8 R9 R10 should be the same in all pzems. Both pzems have 102 (1 KOhm) resistors for R4 R8 R9 R10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants