0

I found this code below for the Arduino platform to obtain current measurements from the SCT013 sensor, and I am wondering how can I define the 'delay' or 'frequency' of my readings? I'd be looking for 200Hz.

Code:

//
#include "EmonLib.h"                   // Include Emon Library
EnergyMonitor emon1;                   // Create an instance

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

  emon1.current(5, 60);             // Current: input pin, calibration.
  //calibration is explained bellow
}

void loop()
{
  double Irms = emon1.calcIrms(1480);  // Calculate Irms only

  Serial.print(Irms*230.0);        // Apparent power
  Serial.print(" ");
  Serial.println(Irms);            // Irms
}
//
ratchet freak
  • 3,267
  • 1
  • 13
  • 12

2 Answers2

1

you skip reading when the last read is less than 1000/200 = 5ms ago.

unsigned long lastRead;

void loop()
{
   unsigned long currentMillis = millis();
   if(currentMillis - lastRead >= 5){

      double Irms = emon1.calcIrms(1480);  // Calculate Irms only

      Serial.print(Irms*230.0);        // Apparent power
      Serial.print(" ");
      Serial.println(Irms);            // Irms
      lastRead = currentMillis;
    }

}
ratchet freak
  • 3,267
  • 1
  • 13
  • 12
0

If you want a 200Hz sample rate this code doesn't work for you.

With this line: double Irms = emon1.calcIrms(1480); you take 1480 samples as fast as Arduino can.

If you want 200Hz sample rate, read analog ping every 5ms using millis() function. And when you have enough samples you can calculate the rms current https://en.wikipedia.org/wiki/Root_mean_square

This is an example code:

unsigned int sample_time = 0;
int readings[200];  //num samples 200
int i = 0;

setup() {
}

loop() {
    if (millis() - sample_time > 5){
        readings[i] = readAnalog(A0);
        i++;
        sample_time = millis();
    }
    if (i > 200)
         Caculate_Irms();
         i = 0;
}
jecrespo
  • 75
  • 1
  • 8