The ACS712 measures current in two directions. It means that if we sample fast enough and long enough, we sure to find the peak in one direction and the peak in another direction as the ACS712 have 5 μs output rise time in response to step input current. We are measuring AC current of 50Hz i.e. 20mSec per cycle and we get around 4000 Samples in one cycle.
With both peaks known, it is a matter of knowing the shape of the waveform to calculate the current. In the case of line or mains power, we know that waveform to be a SINE wave. Knowing that allows us to apply a basic electronic formula to yield a decent result.
RMS Current = root(2) * Peek Current
What you will learn?
-
What is AC Current?
-
How to measure AC Current using ACS712?
Circuit Connection for AC Current Measurement
Circuit for the AC Current Measurement is same as we used for DC current except the load and source are AC.
Arduino Code for AC Current Measurement
/* Measuring AC Current Using ACS712 www.circuits4you.com */ const int sensorIn = A0; int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module double Voltage = 0; double VRMS = 0; double AmpsRMS = 0; void setup(){ Serial.begin(9600); } void loop(){ Voltage = getVPP(); VRMS = (Voltage/2.0) *0.707; //root 2 is 0.707 AmpsRMS = (VRMS * 1000)/mVperAmp; Serial.print(AmpsRMS); Serial.println(" Amps RMS"); } float getVPP() { float result; int readValue; //value read from the sensor int maxValue = 0; // store max value here int minValue = 1024; // store min value here uint32_t start_time = millis(); while((millis()-start_time) < 1000) //sample for 1 Sec { readValue = analogRead(sensorIn); // see if you have a new maxValue if (readValue > maxValue) { /*record the maximum sensor value*/ maxValue = readValue; } if (readValue < minValue) { /*record the minimum sensor value*/ minValue = readValue; } } // Subtract min from max result = ((maxValue - minValue) * 5.0)/1024.0; return result; }
AC Current Measurement Result
Open Serial terminal to see the Current readings. Read more on DC current measurement.
When I power it (without having anything plugged in), I get current about 0,18-0,21 A instead of 0 A. I built the project on an Arduino pro mini 5 V, 16 MHz with a ACS712 30A powered by a 9 V battery. I changed the mVperAmp to 66, as described.
Any suggestions about that?
Thank you for the article!
How to add a function HOLD the data which will be activated by press button ?
Stop the loop on pressing of button
Shouldn’t that be:
int minValue = 1023; // store min value here (1023 instead of 1024)
//
//
//
result = ((maxValue – minValue) * 5.0)/1023.0; //(Also 1023 instead 1024)
Thanks for taking time to explain this.
Where I got lost is why are we tracking this code with :
uint32_t start_time = millis();
while((millis()-start_time) < 1000)
You passe the value of milli() function into a variable and subtracted the same value from it self Isn't that = 0?
Why not just say:
While( 0 maxValue)
{
/*record the maximum sensor value*/
maxValue = readValue;
}
if (readValue < minValue)
{
/*record the minimum sensor value*/
minValue = readValue;
}
}
// Subtract min from max
result = ((maxValue – minValue) * 5.0)/1024.0;
return result;
}
Please I am really a learner, I learn from professionals like you.
Please correct me where I am wrong.
Regards
Ajiri
Millis gives you time from controller started and it always running so you will never get zero
uint32_t start_time = millis(); //Current Time
while((millis()-start_time) < 1000) //Current Time - Previous Time (start_time time is always running forward)
Indeed correct, that gives 0, and 0 is less than 1000 is it?
while loop operates untill the expression becomes false. meaning to say the code block will be repeatedly executed untill such time that a second passed alredy. giving the expression a false value and exits the while loop and the arduino continues to the code.
Read more about “while loop”.
use google or any search engine.
Good luck!
Why are we measuring the maximum and minimum current values?
Because we are measuring AC current. AC current goes up and down to peak levels 60 times a second (60Hz) or sometimes 50Hz, it depends.
Read more about “AC current”
So, does your code need to be changed for 120v AC? When I run it on my setup–
ACS174, 30a
120v AC
40w lamp “ON”
I get this output–
0.39 Amps RMS
If it multiply known 120v by .39 I get 47W (not 40)
With the lamp turn off sketch output is .13 (15W)
Suggestions?
What is the maximum current this sensor can handle?
there are 5A, 20A and 30A module for this sensor.
That are the prescribed maximum ratings the sensor can handle.
theoretically speaking the max for 5A module is ~13.5A
20A module ~25A
30A module ~37.9A
I read somewhere that using the sensor for currents more than the prescribed can damage the sensor and ruin it.
Also, I read somewhere that he measured current over the prescribed maximum resulted in a nonlinear output. SO, that being said never use it over maximum because you can destroy the sensor, hurt yourself and also the output will also unreliable anyways.
PS.
Take extra care tinkering with high voltage and current!