Go Down

### Topic: how to use ACS712 with Arduino ? (Read 9697 times)previous topic - next topic

#### Dileesha

##### May 26, 2013, 12:26 pm
Hi friends,

Im making a current measuring device with ACS712 and ARDUINO 328P IC.

I made a circuit, but I have some problems in reading the ACS712 hall effect sensor input. ( it produces an analog input )

Below is the code I used. ( i got it from internet )

Quote
void setup() {

Serial.begin(9600);
}

void loop() {

float average = 0;
for(int i = 0; i < 10; i++) {
average = average + (.0264 * analogRead(A3) -13.51) / 1000;
delay(100);
}
Serial.println(average);
}

Below is the result I got . It is not stable .

Quote
-0.00
-0.00
-0.00
-0.00
-0.00
-0.00
-0.00
0.00
0.00
0.00
0.00
0.00
0.00  // now, here I connected a load ( a 1000W water heater )
-0.01
-0.03
-0.03
-0.02
-0.02
-0.01
-0.01
-0.01
-0.01
-0.00
-0.00
-0.01
-0.01
-0.02
-0.03

#### Erdin

#1
##### May 26, 2013, 03:27 pmLast Edit: May 26, 2013, 03:31 pm by Erdin Reason: 1
Your calculation is a little confusing.
Could you add '(' and ')' around the multiplication, so it is clear that the multiplication is done first.

To solve your problem, go back one step.
Show the value of analogRead() on the serial monitor without the average.

I assume the heater is powered by the mains ? 110 or 240V AC ?
If so, you will read AC values, changing at 50Hz or 60Hz.
The average of a AC value will be zero.

You have to know what the numbers ".0264" and "-13.51" do, and explain that in the sketch. You can't write a good sketch if you don't know what those numbers are.
By the way, I have no idea what those numbers are, I would calculate the voltage of the analog input and use the sensitivity of the sensor in the sketch to calculate the current.

#### Dileesha

#2
##### May 27, 2013, 03:42 pm
@Erdin

Yes it's a 230V 50Hz mains line.

But I'm not really sure of what that -13.51 means, because I just got this from internet and just used.

Ok Erdin, just forget that -13.51. Just please tell me the way you follow if you want to measure the AC voltage using this sensor connected to arduino ?

The sensor(ACS712) is for -20 to 20A AC/DC measuring . And it shows 2.5V with no current flowing through the IC.

Thanks,
Dileesha.

#### Erdin

#3
##### May 27, 2013, 03:51 pm
The output is somewhere in the middle.
So 2.5V with no current is good.

To measure the current with the 50Hz or 60Hz mains requires a lot of programming if you want to do it right.
Take a look at this: http://openenergymonitor.org/emon/
You should find explanation about current measurement.

For a rough indication, you could find the minimum (negative current) and the maximum (positive current) during an interval (for example 100ms). They should be almost equeal to each other with an AC current and you could use that to calculate for example the rms value.
https://en.wikipedia.org/wiki/Alternating_current

If you want to know an AC current, you have to do a number of samples and you need some calculation.

#### MarkT

#4
##### May 28, 2013, 03:43 pm
Quote

But I'm not really sure of what that -13.51 means, because I just got this from internet and just used.

Its -0.0264 x 512

That line:
Code: [Select]
`    average = average + (.0264 * analogRead(A3) -13.51) / 1000;`
would be much clearer as
Code: [Select]
`    sum += 0.0264 * (analogRead(A3) - 512) ;`
and take mean after the loop by dividing by the loop count.

But of course averaging an AC waveform is pointless - you want the rms value, hence something like:

Code: [Select]
`   float sum = 0;  for (int i = 0; i < N; i++) {    float current = 0.0264 * (analogRead(A3) - 512) ;  // in amps I presume    sum += current * current ;  // sum squares    // no delay, make sure N large enough to cover many waveforms.  }  float average = sqrt (sum / N) ;  // root-mean of squares`
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#5
Thanks markt,

I will try.

#### Dileesha

#6
##### May 28, 2013, 05:59 pm
@Erdin

Thanks for the info.

#### Dileesha

#7
##### May 28, 2013, 06:49 pm
Ok well I then tried with emonLib from openenergymonitor.

Please note : I have connected this to 230V AC 50Hz domestic single phase mains line.

And now i get some outputs on my serial monitor. ( with no load connected )

Quote

DAY TIME                Vrms    Irms   PF      Apparent Pwer   Real Power
2013/5/28 18:0:11   245.2   0.3   -0.05   64.83                  -3.04
2013/5/28 18:0:12   239.9   0.2   -0.03   66.89                  -2.18
2013/5/28 18:0:17   239.7   0.3   -0.04   66.28                  -2.97
2013/5/28 18:0:18   240.6   0.3   -0.12   62.00                  -7.19
2013/5/28 18:0:19   240.0   0.2   -0.07   60.95                  -4.29
2013/5/28 18:0:23   240.6   0.2   -0.02   59.23                  -1.27
2013/5/28 18:0:23   239.0   0.3   -0.02   71.22                  -1.15
2013/5/28 18:0:24   240.7   0.2   -0.01   71.94                  -0.47
2013/5/28 18:0:29   240.1   0.3   -0.07   62.82                  -4.56
2013/5/28 18:0:29   239.9   0.3   -0.03   63.40                  -1.92
2013/5/28 18:0:30   239.5   0.3   -0.03   69.33                  -1.76
2013/5/28 18:0:34   239.7   0.3   -0.04   67.83                  -2.46

Within the main code, there is a calibration ( we can set anything we want as that constant ) value to be put. Also they ask us to put the phase difference . So i'm confused because I don't really know which value I should put as PHASE DIFFERENCE ?

Further I hope to use this system for resistive loads such as water heaters and also for inductive loads such as motors.

Thanks,
Dileesha

#### Dileesha

#8
##### May 28, 2013, 06:51 pm
below is a part of the code which includes that values

Quote
void setup()

Serial.begin(9600);
lcd.begin(16, 2);

emon1.voltage(2, 468.26, 0);  // Voltage: input pin, calibration, phase_shift
emon1.current(3, 7.4);       // Current: input pin, calibration.[/quote]

#### MarkT

#9
##### May 28, 2013, 07:21 pm
Have you read the comments in the eMon library - that ought to explain the calibration values (if not
then that's a bit poor).
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### Dileesha

#10
##### May 28, 2013, 09:15 pm
Hello

I read in an article that,

when a purely resistive load is connected, then the true power(P) = I2 * R
and the apparent power is same ( since App.Pwr = I2*Z )

when a purely reactive load is connected, then the true power (P) becomes ZERO (0) .

My problem is, now the above published serial monitor output results have been obtained with NO LOAD connected . But still how it gives a value for apparent power ?

And can real power and power factor be NEGATIVE ?

I also tested connecting a purely resistive WATER HEATER as a load. But still the real power remains below 1, but apparent power goes high up to like 900. Im confused ?

Thanks,
Dileesha.

#### Dileesha

#11
##### May 28, 2013, 09:20 pm
@MarkT

Yes I did . Even I have gone through their website many times . But they haven't explained how to select a value for phase shift. But in their default example they have put the phase shift as 1.7 . And the Voltage Calibration as 234.6 . It somehow works, but not accurate. That's why I thought of changing the phase shift value.

May be its not needed to change the phase shift value. But I'm searching for a way to make this reading accurate.

#### MarkT

#12
##### May 29, 2013, 12:27 am
I think phase shift compensates for the shift in the voltage transformer used to sense mains voltage.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up