Advertisement
hwthinker

PZEM-004T Arduino Uno and Mega

Sep 28th, 2019
1,889
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //PZEM untuk Arduino Uno dan Arduino Mega
  2. // library https://github.com/mandulaj/PZEM-004T-v30
  3. // modified by HwThinker
  4.  
  5. #include <SoftwareSerial.h>
  6. #include <PZEM004Tv30.h>
  7.  
  8. //PZEM004Tv30 pzem(10,11);   /// Software Serial pin 10 (RX) & 11 (TX) for arduino uno
  9. PZEM004Tv30 pzem(&Serial2);  // (RX)PZEM004Tv30 -> Tx2Arduino ; (TX)PZEM004Tv30 -> Rx2ArduinoMega for arduio mega2560
  10.  
  11. void setup() {
  12.   Serial.begin(115200);
  13. }
  14.  
  15. void loop() {
  16.     float voltage = pzem.voltage();
  17.     if(voltage != NAN){
  18.         Serial.print("Tegangan: "); Serial.print(voltage); Serial.println("V");
  19.     } else {
  20.         Serial.println("Error reading voltage");
  21.     }
  22.  
  23.     float current = pzem.current();
  24.     if(current != NAN){
  25.         Serial.print("Arus: "); Serial.print(current); Serial.println("A");
  26.     } else {
  27.         Serial.println("Error reading current");
  28.     }
  29.  
  30.     float power = pzem.power();
  31.     if(current != NAN){
  32.         Serial.print("Daya: "); Serial.print(power); Serial.println("W");
  33.     } else {
  34.         Serial.println("Error reading power");
  35.     }
  36.  
  37.     float energy = pzem.energy();
  38.     if(current != NAN){
  39.         Serial.print("Energi: "); Serial.print(energy,3); Serial.println("kWh");
  40.     } else {
  41.         Serial.println("Error reading energy");
  42.     }
  43.  
  44.     float frequency = pzem.frequency();
  45.     if(current != NAN){
  46.         Serial.print("Frekuensi: "); Serial.print(frequency, 1); Serial.println("Hz");
  47.     } else {
  48.         Serial.println("Error reading frequency");
  49.     }
  50.  
  51.     float pf = pzem.pf();
  52.     if(current != NAN){
  53.         Serial.print("PF: "); Serial.println(pf);
  54.     } else {
  55.         Serial.println("Error reading power factor");
  56.     }
  57.  
  58.     Serial.println();
  59.     delay(2000);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement