Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: C
Posted by: Massimo Zappino
Added: Dec 23, 2019 5:44 PM
Modified: Dec 23, 2019 5:54 PM
Views: 18
Tags: arduino
  1. #include <PZEM004Tv30.h>
  2.  
  3. PZEM004Tv30 pzem(11, 12);
  4. void setup() {
  5.   Serial.begin(115200);
  6. }
  7. void loop() {
  8.   float voltage = pzem.voltage();
  9.   if (voltage != NAN) {
  10.     Serial.print("Voltage: ");
  11.     Serial.print(voltage);
  12.     Serial.println("V");
  13.   } else {
  14.     Serial.println("Error reading voltage");
  15.   }
  16.   float current = pzem.current();
  17.   if (current != NAN) {
  18.     Serial.print("Current: ");
  19.     Serial.print(current);
  20.     Serial.println("A");
  21.   } else {
  22.     Serial.println("Error reading current");
  23.   }
  24.   float power = pzem.power();
  25.   if (current != NAN) {
  26.     Serial.print("Power: ");
  27.     Serial.print(power);
  28.     Serial.println("W");
  29.   } else {
  30.     Serial.println("Error reading power");
  31.   }
  32.   float energy = pzem.energy();
  33.   if (current != NAN) {
  34.     Serial.print("Energy: ");
  35.     Serial.print(energy, 3);
  36.     Serial.println("kWh");
  37.   } else {
  38.     Serial.println("Error reading energy");
  39.   }
  40.   float frequency = pzem.frequency();
  41.   if (current != NAN) {
  42.     Serial.print("Frequency: ");
  43.     Serial.print(frequency, 1);
  44.     Serial.println("Hz");
  45.   } else {
  46.     Serial.println("Error reading frequency");
  47.   }
  48.   float pf = pzem.pf();
  49.   if (current != NAN) {
  50.     Serial.print("PF: ");
  51.     Serial.println(pf);
  52.   } else {
  53.     Serial.println("Error reading power factor");
  54.   }
  55.   Serial.println();
  56.   delay(2000);
  57. }