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
}
//
2
  • That is a very strange question to ask. What do you want to achieve ? Have you heard about the XY-problem ? xyproblem.info If I remember it correctly, the EmonLib samples at a higher frequency and returns the average. Why do you want 200 samples per second from a 50Hz or 60Hz signal ?
    – Jot
    Oct 6, 2017 at 11:46
  • You already asked the question with a current sensor: arduino.stackexchange.com/questions/45293/…
    – Jot
    Oct 6, 2017 at 11:51

2 Answers 2

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.