Mathematical Calculation of Current with ACS712 20A

I am trying to find out what is the linear equation that the ACS712 20A Current Sensor uses. I have attached the code below.

/*
Measuring Current Using ACS712
*/
const int analogIn = A1;
int mVperAmp = 100; // use 100 for 20A Module and 66 for 30A Module
int RawValue= 0;
int ACSoffset = 2500; 
double Voltage = 0;
double Amps = 0;
void setup()
{
  Serial.begin(9600);
}

void loop(){
   

 
 RawValue = analogRead(analogIn);
 Voltage = (RawValue / 1024.0) * 5000; // Gets you mV
 Amps = ((Voltage - ACSoffset) / mVperAmp);
 
 
 Serial.print("/t Raw Value = " ); // shows pre-scaled value 
 Serial.print(RawValue); 
 Serial.print("\t mV = "); // shows the voltage measured 
 Serial.print(Voltage,3); // the '3' after voltage allows you to display 3 digits after decimal point
 Serial.print("\t Amps = "); // shows the current measured 
 Serial.println(Amps,3); // the '3' after voltage allows you to display 3 digits after decimal point
int sensorValue = A1;
sensorValue = analogRead(A1);      // read sensorOne value.
 
}

In one of my books I read that effective current = analog measurement /185 * 1000000/ 1.414
Which does not make sense from the code.

Does anyone have an idea about it?

int mVperAmp = 100; // use 100 for 20A Module and 66 for 30A ModuleThis is the all important conversion factor, which you obtain from the sensor data sheet. As the variable name suggests, it converts the sensor output reading in milliVolts to sensor current in Amperes.

As for the math, beware of unwanted truncation due to improper use of integer division.

I am trying to find out what is the linear equation that the ACS712 20A Current Sensor uses. I have attached the code below.

Let me start with an example: The LM35 Temperature Sensor data sheet has said the Linear Response Equation of this sensor is : T0C = 100*VDT; where, VDT is the DC voltage coming out of its signal pin when the ambient temperature is T0C. This response equation has been derived based on known 2-point responses which are also given in the data sheet. These 2 points are: A(1500C, 1500 mV); B(250C, 250 mV).

For the ACS712 sensor, the data sheet has not given any values regarding 2-point responses. The data sheet has only said that the sensor will make an output between 66mV to 185 mV when there is a load of 1A in the hot circuit. Therefore, it is responsibility of the user to derive the Response Equation of the ACS712 Sensor before it is used in a measurement circuit. The procedures are:

1. Connect 500 W Load (5x100W/220V Filament Bulbs) to the primary of the sensor. Measure the output voltage; it should be between 150mV to 420 mV. Record the voltage as VD1.

2. Connect 300 W Load (3x100W/220V Filament Bulbs) to the primary of the sensor. Measure the output voltage; it should be between 90mV to 252mV. Record the voltage as VD2.

3. Now,find the Response Equation (y = mx + c) as follows based on:
A(500, VD1), B(300, VD2), C(W, VD).

==> (VD1 - VD2)/(500 - 300) = (VD1 - VD)/(500 - W)
==> VD = k*W + C //This is the Linear response equation of ACS712 sensor.

Now, you have to model (tailor) it with respect to whatever Arduino you use. This is the job of expressing VDT in terms of the ADC value of the ATmega328P of Arduino UNO.

4. Take VREF of the ADC as 1.1V INTERNAL.

5. When input is 1.1V, the ADC value is 1024. Therefore, when the input is VD, the ADC value will be (1024/1.1)*VD. So, ADC = (1024/1.1)*VD. From here, we get: VD = (1.1/1024)*ADC. That is: VD = (1.1/1024)*analogRead(A0);.

6. The original response equation: VD = k*W + C
==> (1.1/1024)analogRead(A0) =kW + C ;
In this equation, all factors known; so, we can always compute the almost correct value of the present (arbitrary) load (W). Because, the voltage is also known(220V), we can find the current and display it on the monitor.

GolamMostafa:
Let me start with an example: The LM35 Temperature Sensor data sheet has said the Linear Response Equation of this sensor is : T0C = 100*VDT; where, VDT is the DC voltage coming out of its signal pin when the ambient temperature is T0C. This response equation has been derived based on known 2-point responses which are also given in the data sheet. These 2 points are: A(1500C, 1500 mV); B(250C, 250 mV).

For the ACS712 sensor, the data sheet has not given any values regarding 2-point responses. The data sheet has only said that the sensor will make an output between 66mV to 185 mV when there is a load of 1A in the hot circuit. Therefore, it is responsibility of the user to derive the Response Equation of the ACS712 Sensor before it is used in a measurement circuit. The procedures are:

1. Connect 500 W Load (5x100W/220V Filament Bulbs) to the primary of the sensor. Measure the output voltage; it should be between 150mV to 420 mV. Record the voltage as VD1.

2. Connect 300 W Load (3x100W/220V Filament Bulbs) to the primary of the sensor. Measure the output voltage; it should be between 90mV to 252mV. Record the voltage as VD2.

3. Now,find the Response Equation (y = mx + c) as follows based on:
A(500, VD1), B(300, VD2), C(W, VD).

==> (VD1 - VD2)/(500 - 300) = (VD1 - VD)/(500 - W)
==> VD = k*W + C //This is the Linear response equation of ACS712 sensor.

Now, you have to model (tailor) it with respect to whatever Arduino you use. This is the job of expressing VDT in terms of the ADC value of the ATmega328P of Arduino UNO.

4. Take VREF of the ADC as 1.1V INTERNAL.

5. When input is 1.1V, the ADC value is 1024. Therefore, when the input is VD, the ADC value will be (1024/1.1)*VD. So, ADC = (1024/1.1)*VD. From here, we get: VD = (1.1/1024)*ADC. That is: VD = (1.1/1024)*analogRead(A0);.

6. The original response equation: VD = k*W + C
==> (1.1/1024)analogRead(A0) =kW + C ;
In this equation, all factors known; so, we can always compute the almost correct value of the present (arbitrary) load (W). Because, the voltage is also known(220V), we can find the current and display it on the monitor.

Thanks for the very detailed step to step process.
I will implement it and see what I come up with.
I think the topic should be marked as solved.

Regards
Tsakitsan

tsakitsan:
I think the topic should be marked as solved.

Maybe.

tsakitsan:
I am trying to find out what is the linear equation that the ACS712 20A Current Sensor uses.

If you looked at the datasheet, you would see that, nominally, Vout = (0.1v/A * amps) + 2.5v...which is consistent with your code.

By the way, if you use the 1.1v internal reference as suggested in post #2, you will only read a very small portion of the range (-20A to -15A).

And if you are going to use the full range of this sensor (or even just half the range), I'd suggest calibrating it with a wider range of loads than 1.4A to 2.3A

And if you are going to use the full range of this sensor (or even just half the range), I'd suggest calibrating it with a wider range of loads than 1.4A to 2.3A

These two loads of 500W and 300W are test loads. The sensor is considered to be linear. Therefore, the equation derived based on 500 and 300W will always give correct reading for any load 0A - 20A. If the test loads are changed to 2200W (10A) and 4000W (20A), one will arrive at the slightly better response equation; but, there are difficulties to manage such kinds of large test loads in the personal Lab; it is also not recommended from safety point of view.

@ tsakitsan
Please report your equation and its responses with real two loads. I will compare your equation with mine one. What is your domestic voltage -- 220V or 110V? Mine one is 220V (nominal).

Voltage = (RawValue / 1024.0) * 5000; // Gets you mV

int mVperAmp = 100; // use 100 for 20A Module and 66 for 30A Module

Both lines are wrong if the Arduino supply is not exactly 5.000volt (which it rarely is).

ACSxxx sensors are ratiometric, and should be read as ratiometric sensors.
But most sketches you find on the net read them as voltage sensors,
which introduces errors if VCC is not exactly 5.000volt.
Leo..

Wawa:
Voltage = (RawValue / 1024.0) * 5000; // Gets you mV

int mVperAmp = 100; // use 100 for 20A Module and 66 for 30A Module

Both lines are wrong if the Arduino supply is not exactly 5.000volt (which it rarely is).

ACSxxx sensors are ratiometric, and should be read as ratiometric sensors.
But most sketches you find on the net read them as voltage sensors,
which introduces errors if VCC is not exactly 5.000volt.
Leo..

?

The following demonstrates that the OP's code should work fine (that is, it correctly deals with the ratiometric nature of the sensor):

At zero amps and 5v supply, Vout is 2.5v, which the ADC reads as 512, and the code correctly converts that to 2500 mV and 0 amps.

If the supply droops to 4.5v, Vout is 2.25v, which the ADC also reads as 512, since it is half of the reference voltage of 4.5v, and the code correctly converts that to 2500 mV and 0 amps.

At 20 amps and 5v supply, Vout is 4.5v, which the ADC reads as 922, and the code correctly converts that to 4500 mV and 20 amps.

If the supply droops to 4.5v, Vout is 4.05v (sense drops to 90 mV/A, so 0.09*20+2.25=4.05v), which the ADC also reads as 922 (since the ref voltage is lower by the same proportion), and the code correctly converts that to 4500 mV and 20 amps.

When the data sheet says that the response of ACS712 is: 66mV - 185V/1A current at the hor circuit, it means that the response varies sensor to sensor due to manufacturing tolerances. Therefore, the equation that is true for ACS712(1) that may not be true for ACS712(2). Do we have any better way of determining its gain and offset of the sensor without adopting the 2-point calibration philosophy?

@DaveEvans.
You’re correct.
I didn’t follow the rest of the maths through.
Leo…

Are you trying to calculate the RMS value of an AC current?

GolamMostafa:
When the data sheet says that the response of ACS712 is: 66mV - 185V/1A current at the hor circuit, it means that the response varies sensor to sensor due to manufacturing tolerances. Therefore, the equation that is true for ACS712(1) that may not be true for ACS712(2). Do we have any better way of determining its gain and offset of the sensor without adopting the 2-point calibration philosophy?

The 66 to 185 is for the full suite of sensors...the 5A, the 20A, and the 30A. If you read the datasheet more closely, you will see that the 20A has a sense of 100 mV/A +/- 4 mV/A and a total output error of +/- 1.5% over the full range -20A to +20A.

The Selection Guide (pasted below) of ACS712 data sheet says that the sensitivity value of 100mV/1A is a typical value, and it agrees what the data sheet has announced in the summary -- sensitivity is 66mV - 185mV/A (66+185/2 = 125). The ACS758 does not use the word typical; it clearly says that the sensor is factory calibrated and the sensitivity is this much for this much current in the hot circuit.

:confused:

Why are you averaging the sensitivity of the 5A sensor (185) and the 30A sensor (66)?

The OP is using the 20A sensor (100).

Capture0.JPG

Capture1.JPG

Capture0.JPG

Capture1.JPG

Why are you averaging the sensitivity of the 5A sensor (185) and the 30A sensor (66)?

The notion arrived from the features summary of the first page of data sheet. Now, your effort in the presentation of detailed information on the individual sensor has made me to understand that these sensors are indeed factory calibrated. Thank+ you so much to keep me educated.

from: what to use ACS712 or ACS758 for current mesurement - #7 by jremington - Sensors - Arduino Forum

All sensors can benefit from calibration.

The ACS712 can be purchased in three different versions, +/- 5, 20 and 30 Amperes full scale, each with different scale factors.

The factory calibration is supposed to be accurate to 1.5% of the full scale, but the user could improve that by careful calibration.

So if I am happy with the factory calibration I will not do any tests.

Even though initially the sensor seemed quite accurate, there was only 0.046 amps fluctuation both with no load (but closed circuit) and with load (if load was 2.5A there was a 0.046A fluctuation). Always compared to a multimeter.

Can somebody direct me to the equation I was looking for, initially?

See reply #4 and #7.