RMS value from analog sensor

Hi everyone!
Sorry if I should posted this topic on other subject.

I’m trying to read an analog current sensor, SCT013, this sensor response with an analog signal that varies from 0-1V according to 0-30A, I’m trying to get the RMS value, applying a HIGH PASS filter in software, because I used a offset in hardware, in order to read the negative values… and then by software apply the HIGH PASS filter.

The problem that I have is that the values that I get are wrong… I know this because I manually verify the values with a multimeter.

So… I hope you guys can understand my problem and if you may give a hand I will really appreciate it!

Here goes my code

#include "math.h"

#define SAMPLES 6000//5556

int sampleSensor[SAMPLES];
int pinSensor = A0;
double sqI, sumI;
double I_RATIO = 0.805664;
double Irms, value;

//High Pass Filter
double RC = 0.00397;//1.0/(40*2*3.14);
double dt = 250000;//1.0/0.000004;
double alpha = 0.000000015;//RC/(RC+dt);
double filteredArray[SAMPLES];

void setup() {
  for(unsigned int i=0;i<SAMPLES;i++){
    sampleSensor[i] = 0;

void loop() {
  for(unsigned int n=0;n<SAMPLES;n++){
    sampleSensor[n] = analogRead(pinSensor);
    //sqI = sampleSensor[n]*sampleSensor[n];
    //sumI += sqI;
  filteredArray[0] = sampleSensor[0];
  for(unsigned int s=1;s<SAMPLES;s++){
    filteredArray[s] = alpha*(filteredArray[s-1]+sampleSensor[s]-sampleSensor[s-1]);
  for(unsigned int i=0;i<SAMPLES;i++){
    sqI = sq(filteredArray[i]);//*filteredArray[i];

  Irms = I_RATIO*sqrt(sumI/SAMPLES);
  value = (Irms - 9.53)/(61.16);
  SerialUSB.print("Irms: ");
  SerialUSB.print(" value: ");


Oh it’s true!, the value variable is because I did a linear regression according to the values that I get from the multimeter and the values that I get from the sensor.

Thank you!

arduino doesn't have enough memory, to get 6000 int.

Magician: arduino doesn't have enough memory, to get 6000 int.

To expand a bit more on this, every integer takes up 2 bytes of RAM, so 6000 integers would take up 12,000 bytes of RAM, which is a little under 12k. The 328p only has 2k of RAM, so if you need that many readings, you'll need to use an external RAM of some sort (there are a few i2c enabled ones on Digi, though it'll be slow) or a different platform.

Sorry, I forget mention that I’m using the Arduino DUE, so I don’t worry about memory.

I wander, where did you get your HPF:

  filteredArray[s] = alpha*(filteredArray[s-1]+sampleSensor[s]-sampleSensor[s-1]);

alpha seems incredibly low, 15 * 10^-9, with max arduino ADC value 4.095 *10^3