How to Create a Simple Energy Meter Using PZEM004T & ESP8266 NodeMCU

In this tutorial we will learn how to use a circuit board with energy measurements capabilities, to acquire the most important values of an electrical installation. The ESP8266 will communicate with a PZEM004T board to get the values. If you need help on how to start with ESP8266 check the tutorial “ESP8266 NodeMCU – Blinking a LED”.

Parts Required

To build the global setup you must have the following parts:

Note: You do not need to have a NodeMCU board nor an ESP8266 to follow this tutorial. I am using this board because it is very famous and also because I will post future tutorials using wifi communications. In order to use other boards, you just have to change the pin number.

PZEM004T

Figure 1

In many electrical projects, engineer directly deals with measurements with few basic requirements like:

  • High galvanic isolation with;
  • Parameter display;
  • Direct communication with a computer;
  • Data acquisition and storage with subsequent viewing or copying to the computer.

PZEM-004T is a board, features all the above requirements and can measure the most important electrical variables, like:

  • Voltage (V);
  • Corrent (A);
  • Power (W);
  • Power factor (Cos_fi);
  • Reactive Power (VAr);
  • Total Energy consuption.

If you want to know more about the hardware specifications visit manufacturer datasheet.

Last but not least. This module serves all these basic requirements of measurement. You can only use the board to measure the values, but it has some limitations in the current (A) value. If you want to increase the max current (A) value, you must add a 33mm diameter current transformer coil (CT), like in figure 2. Although, it can only measure a few A (0-10) without the CT.

Figure 2

Serial Communication

The module is equipped with a TTL serial data communication interface via the serial port, where you can read and set the relevant parameters. Although, if you want a device with a USB or RS232 (such as computers) to communicate, then you need to be equipped with a different TTL adapter hardware board. Details can be found in Figure 3.

Figure 3

Important notes:

1. This module is suitable for indoor, not outdoor use.
2. the applied load should not exceed the rated power.
3. The wiring can not be wrong.
4. The board must be connected to AC mains because 5V DC only power the TTL logic.

Circuit

Figure 4

If you use the HI-LINK Power Supply Module, connect the Vcc +5V output to Vin pin, if not you should use another power supply with the same rates. The RX and TX from PZEM004T go to GPIO pin 4 (D2) and GPIO pin 5 (D1). The previous GPIO pins are not serial communications pins. Although, we will use them as such, though some code. Connect the current transformer (CT) and the AC live wires in parallel with the load. The wiring in the AC side must be respected as shown in Figure 4.

Danger: Turn off the AC main installation before connecting something. Make sure that everything is unplugged from the mains. An error can cause electric shock.

Coding

We will use a library from Git-Hub, that communicate with PZEM004T. If you do not know how to instal a library or use the library manager in Arduino IDE, you can check Installing Additional Arduino Libraries. Copy the main sketch below to your Arduino IDE project and save it.

If you want, you can use three PZEM004T boards and make a 3 phase energy meter. Just have to choose another 4 pins to make the serial communications and also include 2 more software serials in the code.

Note: NodeMCU only has one Serial Port. Because of that, we need to use software serial in order to prevent collision while downloading the code.

#include <PZEM004Tv30.h>

/* Use software serial for the PZEM
 * Pin 5 Rx (Connects to the Tx pin on the PZEM)
 * Pin 4 Tx (Connects to the Rx pin on the PZEM)
*/
PZEM004Tv30 pzem(5, 4);

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

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(2000);
}

4 thoughts on “How to Create a Simple Energy Meter Using PZEM004T & ESP8266 NodeMCU

  1. Parabéns Ricardo pelo seu trabalho!!
    Você chegou a desenvolver código para poder usar três placas PZEM004T e fazer um medidor de energia trifásico. Conforme você descreveu: “Basta escolher mais 4 pinos para fazer as comunicações seriais e também incluir mais 2 seriais de software no código.”
    Se sim, você pode disponibilizar?
    Desde já o agradeço e parabenizo novamente.
    Abraços
    Antonio Carlos Aguiar Gagno Jr (Vitória-ES-Brasil)
    Whatsapp +55 27 99893 5036

    1. Neste momento não consigo enviar, pois não o tenho disponível no local onde me encontro, mas estará disponível no site em breve.

      Cumprimentos

  2. PZEMSoftwareSerial:7:24: error: invalid conversion from ‘int’ to ‘HardwareSerial*’ [-fpermissive]
    PZEM004Tv30 pzem(11, 12);

Leave a Reply

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.