AC Current Measurement using ACS712

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?

  1. What is AC Current?
  2. 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.

ACS712 Arduino Circuit
ACS712 Arduino Circuit for AC Current measurement

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.

AC Current measurement using ACS712
AC Current measurement using ACS712

12 thoughts on “AC Current Measurement using ACS712

  1. 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!

  2. 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

    1. 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)

    2. 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!

  3. 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?

    1. 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!

Leave a Reply