dc42:

Almost. You need to convert (adc_raw - adc_zero) to type ‘long’ before you square it, because the square may exceed the range of an int. You will need to declare currentac with type ‘long’ too. After taking the sqrt, you need to adjust it for the sensitivity etc. just as you do for DC.

i’ve left all untyped i know

typed code is below.

i’ve tried two ways, but it doesn’t seams to works.

test 1------------------

dc42:

subtract the zero-current value (512 or thereabouts) from the raw readings, square that, and add it to an accumulator variable. When you have accumulated enough samples, take the square root if the accumulator and divide by the number of readings.

```
double d = 0; long l = 0;
for(;;) {
uint16_t i = 0;
int16_t adc_raw = 0;
int16_t adc_zero = 512;
for(i=0; i<100; i++) {
adc_raw = adc_read(2);
l += pow(adc_raw - adc_zero, 2);
_delay_us(100);
}
d = (sqrt(l)/100) * 5 / (0.04 * 1024.0);
l = 0;
```

test 2------------------

dc42:

average the square of the current, and take the square root of the average

```
double c = 0, d = 0;
for(;;) {
uint16_t i = 0;
int16_t adc_raw = 0;
int16_t adc_zero = 512;
for(i=0; i<100; i++) {
adc_raw = adc_read(2);
adc_raw = adc_raw - adc_zero;
c += pow(adc_raw * 5 / (0.04 * 1024.0), 2);
_delay_us(100);
}
d = sqrt(c)/100;
c = 0;
... print c...
}
```

i’m testing a 6.4 Amp 220V 50hz device

both code i obtain almost the same value, which is 0.27Amp, but is not correct