Search code examples
arduinoembeddedmicrocontroller

3-phase voltage monitor / Phase sequence using Arduino + EmonLib


I'm trying to learn/build a 3-phase system monitoring device based on Arduino Mega, which will allow the user to monitor 220V/50Hz 3-phase system voltage and current with phase sequence detection for the correct wiring order (L1, l2, l3).

I was following this project from engineer experiences and achieved good results on the hardware part (schematics and circuit from link above).
For the software I used EmonLib for voltage and current measurements. I got the correct readings from Arduino, I think I don't understand the part related to adding timers loop to the code.

This is my modified code:

#include "EmonLib.h" // Include Emon Library
EnergyMonitor emon0; // L1 Instance
EnergyMonitor emon1; // L2 Instance
EnergyMonitor emon2; // L3 Instance

int s1 = analogRead(A3);
int s2 = analogRead(A4);
int s3 = analogRead(A5);
unsigned int g=0;
int i; // For for loops
unsigned int a22;

void setup() {
  Serial.begin(9600);
  Serial.println("3 phase voltage");
  delay(1000);
  emon0.voltage(0, 225.5, 1.7); // Voltage: input pin, calibration, phase_shift
  emon1.voltage(1, 225.5, 1.7); // Voltage: input pin, calibration, phase_shift
  emon2.voltage(2, 225.5, 1.7); // Voltage: input pin, calibration, phase_shift
  emon0.current(6, 111.1); // Current: input pin, calibration.
  emon1.current(7, 111.1); // Current: input pin, calibration.
  emon2.current(8, 111.1); // Current: input pin, calibration.
}

//Code Start
//Function for angle calculations
int calculations() {
  unsigned int k=0;
  //To complete number of counts
  g=g+1;
  //To convert  into seconds
  float pf=(float)g/1000000;
  //To convert seconds into degrees
  pf=pf*50*360;//here frequency = 50hz
  k = pf;
  return k;
}

void tloop1() {
  while(1) {
    if (s2 != 0) {
      TCNT1=0;
      TCCR1B = 0x01; // Start timer1 at Fcpu/1
      break;
    } else {
      continue;
    }
  }

  while(1) {
    if (s3 != 0){
      TCCR1B = 0x00;//stop timer1
      g=TCNT1;//getting number of counts
      break;
    } else {
      continue;
    }
  }
}

void tloop2() {
  while(1) {
    if (s3 != 0){
      TCNT1=0;
      TCCR1B = 0x01; // Start timer1 at Fcpu/1
      break;
    } else {
      continue;
    }
  }

  while(1) {
    if (s1 != 0){
      TCCR1B = 0x00;//stop timer1
      g=TCNT1;//getting number of counts
      break;
    } else {
      continue;
    }
  }
}

float fmap(float x, float in_min, float in_max, float out_min, float out_max) {
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

void phase1() {
  unsigned int a1=0;
  unsigned int v1=0;
  emon0.calcVI(20,2000); // Calculate all. No.of wavelengths, time-out
  float L1supplyVoltage = emon0.Vrms; //extract Vrms into Variable
  float Irms0 = emon0.Irms; //extract Irms into Variable
  float cablibratedi0 = 0;
  Serial.print("phase 1: V = ");Serial.println(L1supplyVoltage); // Print out all variables
  cablibratedi0 = fmap(Irms0, 0.0, 1024.0, 0.0, 200.0);
  Serial.print("phase 1: A = ");Serial.println(cablibratedi0); // Print out all variables
  a1 = calculations();
  Serial.print("phase 1: D = ");Serial.println(a1);
  Serial.print("Line to Next Line Voltage:");Serial.println(L1supplyVoltage * 1.732);
}

void phase2() {
  tloop1();
  unsigned int a2=0;
  unsigned int v2=0;
  delay(20);
  emon1.calcVI(20,2000); // Calculate all. No.of wavelengths, time-out
  float L2supplyVoltage = emon1.Vrms; //extract Vrms into Variable
  float Irms1 = emon1.Irms; //extract Irms into Variable
  float cablibratedi1 = 0;
  Serial.print("phase 2: V = ");Serial.println(L2supplyVoltage); // Print out all variables
  cablibratedi1 = fmap(Irms1, 0.0, 1024.0, 0.0, 200.0);
  Serial.print("phase 2: A = ");Serial.println(cablibratedi1); // Print out all variables
  a2 = calculations(); 
  a22 = a2;
  Serial.print("phase 2: D = ");Serial.println(a2);
  Serial.print("Line to Next Line Voltage:");Serial.println(L2supplyVoltage * 1.732);
  delay(700);
}

void phase3() {
  tloop2();
  unsigned int a3=0;
  unsigned int v3=0;
  delay(20);
  emon2.calcVI(20,2000); // Calculate all. No.of wavelengths, time-out
  float L3supplyVoltage = emon2.Vrms; //extract Vrms into Variable
  float Irms2 = emon2.Irms; //extract Irms into Variable
  float cablibratedi2 = 0;
  Serial.print("phase 3: V = ");Serial.println(L3supplyVoltage);           // Print out all variables
  cablibratedi2 = fmap(Irms2, 0.0, 1024.0, 0.0, 200.0);
  Serial.print("phase 3: A = ");Serial.println(cablibratedi2); // Print out all variables
  a3 = calculations();
  a3 = a22 + a3;
  Serial.print("phase 3: D = ");Serial.println(a3);
  Serial.print("Line to Next Line Voltage:");Serial.println(L3supplyVoltage * 1.732);
}

void loop() {
  phase1();
  phase2();
  phase3();
  delay(1000);
}

Code compiles with no errors, but when I open serial monitor from Arduino IDE only phase1(); gets to run and displayed once.


Solution

  • int s1 = analogRead(A3);
    int s2 = analogRead(A4);
    int s3 = analogRead(A5);
    

    Don't try to read pins outside of functions. It won't work. The variables will be initialized as 0. Which means that your code will get stuck in a while loop.

    What you need to do is:

    int s1;
    int s2;
    int s3;
    
    [...]
    
    void setup() {
      s1 = analogRead(A3);
      s2 = analogRead(A4);
      s3 = analogRead(A5);