使用Arduino + EmonLib的三相电压监控器/相序

时间:2017-12-21 22:20:09

标签: arduino embedded microcontroller

我正在尝试学习/构建基于Arduino Mega的三相系统监控设备,这将允许用户监控220V / 50Hz三相系统电压和电流,并进行相序检测以获得正确的接线顺序( L1,l2,l3)。

我从engineer experiences开始关注这个项目,并在硬件部分取得了很好的效果(上面链接的原理图和电路)。
对于软件,我使用EmonLib进行电压和电流测量。我从Arduino得到了正确的读数,我想我不明白与在代码中添加定时器循环相关的部分。

这是我修改后的代码:

#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);
}

代码编译没有错误,但是当我从Arduino IDE打开串行监视器时,phase1();只能运行并显示一次。

2 个答案:

答案 0 :(得分:1)

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

请勿尝试读取功能外的引脚。它不会起作用。变量将初始化为0.这意味着您的代码将陷入while循环。

您需要做的是:

int s1;
int s2;
int s3;

[...]

void setup() {
  s1 = analogRead(A3);
  s2 = analogRead(A4);
  s3 = analogRead(A5);

答案 1 :(得分:0)

代码现在正在运行,这里是它的副本:

int m; float n;
int m1; float n1;
int m2; float n2;

void setup()
{
  pinMode(A0,INPUT); // L1
  pinMode(A1,INPUT); // L2
  pinMode(A2,INPUT); // L3
  pinMode(A3,INPUT); // A
  pinMode(A4,INPUT); // B
  pinMode(A5,INPUT); // C
  Serial.begin(9600);
}

void loop()
{
   m=analogRead(A0);// read analog values from pin A0 across capacitor
   n=(m* .304177);// converts analog value(x) into input ac supply value using this formula ( explained in woeking section)

   Serial.print("1   analaog input  " ) ; // specify name to the corresponding value to be printed
   Serial.print(m) ; // print input analog value on serial monitor
   Serial.print("   ac voltage  ") ; // specify name to the corresponding value to be printed
   Serial.print(n) ; // prints the ac value on Serial monitor
   Serial.println();
   delay(1000);

   m1=analogRead(A1);// read analog values from pin A0 across capacitor
   n1=(m1* .304177);// converts analog value(x) into input ac supply value using this formula ( explained in woeking section)

   Serial.print("2   analaog input  " ) ; // specify name to the corresponding value to be printed
   Serial.print(m1) ; // print input analog value on serial monitor
   Serial.print("   ac voltage  ") ; // specify name to the corresponding value to be printed
   Serial.print(n1) ; // prints the ac value on Serial monitor
   Serial.println();
   delay(1000);

   m2=analogRead(A2);// read analog values from pin A0 across capacitor
   n2=(m2* .304177);// converts analog value(x) into input ac supply value using this formula ( explained in woeking section)

   Serial.print("3   analaog input  " ) ; // specify name to the corresponding value to be printed
   Serial.print(m2) ; // print input analog value on serial monitor
   Serial.print("   ac voltage  ") ; // specify name to the corresponding value to be printed
   Serial.print(n2) ; // prints the ac value on Serial monitor
   Serial.println();
   delay(1000);
}