trouble with acs712 (measuring ac current)

hi guys, i was new to this stuff firstly :smiley:
i've tried bunch of arduino module, and came across this acs712 module (30A) to monitoring my AC current.

here is my goal,

  1. i'm trying to monitoring AC current around 3-6 Amp (220v/50Hz) with acs712 and arduino uno
  2. set it to turning on fan when reach to 4 Amp with relay module

i've tried many code and modified some but still can't get a fine result, for example i was workng with this code which i get from http://henrysbench.capnfatz.com/henrys-bench/arduino-current-measurements/acs712-arduino-ac-current-tutorial/

/*
Measuring AC Current Using ACS712
*/
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; 
 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 maximum sensor value*/
           minValue = readValue;
       }
   }
   
   // Subtract min from max
   result = ((maxValue - minValue) * 5.0)/1024.0;
      
   return result;
 }

when nothing is connected i got reading from my arduino around 0,02-0,03 Amp.
and when i'm connected with 1,7Amp load (reading from my Amp clamp meter), i got around 0,74 Amp from my arduino

my question is acs712 accurate enough to read AC or DC current? or it is more suitable for DC current?
or should i get another current sensor for AC like ta-12-100 or any suggestion to read 3-6 Amp on AC?

:smiley:

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

Have you read the code and the comments in it or just copy/pasted it? That value must be 66 for the 30A module.

acs712 is good for AC. the sketch calculates the amperes from the voltages read from A0. so the calculation is wrong or your wiring

pylon:

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

Have you read the code and the comments in it or just copy/pasted it? That value must be 66 for the 30A module.

i'm just copied the code directly from the web and paste it here. but i'm using "66" in my arduino. sorry not to mention it :smiley:

and it's still showing incorrect accurancy base on my amp clamp meter

Juraj:
acs712 is good for AC. the sketch calculates the amperes from the voltages read from A0. so the calculation is wrong or your wiring

i'm doubt it would come from my wiring, as i'm already connected to pin A0.
is it have some problem with my 220v AC? assuming the code is working on 110v AC? idk though. i've trying for 6 hours :smiley:

lupamundur:
hi juraj, sorry for bothering you. i'm seek for help. i still can't figure it out with my acs712 to measuring current. i've using reading min and max data using this code, and i get min=510, max=512. which mean my acs712 isn't bad (i read in some web, saying it suppose around 512 when not connected)

#include <LiquidCrystal.h>

// coder haripinter, elangsakti.com
// ACS 712
const byte sensor = A0;

// Settingan  LCD RS E D4 D5 D6 D7
LiquidCrystal lcd(8,9,10,11,12,13);

int acs_ref_data = 512;
int acs_data = 0;
int acs_data_max = acs_ref_data;
int acs_data_min = acs_ref_data;
int acs_range = 0;

int LIMIT = 5;

unsigned long delay_now = 0;
unsigned long delay_last = 0;
unsigned long delay_limit = 1000;

void setup() {
    pinMode(sensor, INPUT);
    lcd.begin(16,2);
}

void loop() {
    sensor_listener();
}

void sensor_listener(){
    acs_data = analogRead(sensor);
    // max data
    if(acs_data > acs_data_max){
      acs_data_max = acs_data;
    }
    // min data
    if(acs_data < acs_data_min){
      acs_data_min = acs_data;
    }
   
    if(millis() - delay_last >= delay_limit){
      lcd.setCursor(0,0);
      lcd.print("Min: ");
      lcd.print(acs_data_min);
      lcd.print("  ");
     
      lcd.setCursor(0,1);
      lcd.print("Max: ");
      lcd.print(acs_data_max);
      lcd.print("  ");

lcd.setCursor(10,0);
      lcd.print("Stat:");
      lcd.setCursor(10,1);

// for sensor
      acs_range = acs_data_max-acs_data_min;
      if( (acs_data_min < acs_ref_data) &&
          (acs_data_max > acs_ref_data) &&
          (acs_range > LIMIT)){
        lcd.print("ON ");
      }else{
        lcd.print("OFF");
      }

delay_last += delay_limit;
      acs_data_min = acs_ref_data;
      acs_data_max = acs_ref_data;
    }
}




according to min and max data i get, it has no problem when there is no load. but when i applied 1,9A, i get min=486, max=536. and put it in calculation according to this link [link](http://henrysbench.capnfatz.com/henrys-bench/arduino-current-measurements/acs712-arduino-ac-current-tutorial/) which will be=


result = ((maxValue - minValue) * 5.0)/1024.0;
= ((536-486)*5.0)/1024.0
=0.244
VRMS = (Voltage/2.0) *0.707; 
= (0.244/2.0)*0.707
=0.0863
AmpsRMS = (VRMS * 1000)/mVperAmp;
= (0.0863*1000)/66 (using module for 30A)
=1.31 A (suppose to be 1.9A)

hopefully you can help me :D

you didn't change 5 to 3.3 (volts)

Juraj:
you didn't change 5 to 3.3 (volts)

thank you for replying, by 3.3 V are you mean i have to use voltage divider to lower voltage from 5V to 3.3V from acs712 output pin to arduino A0 pin?

sorry I confused threads. You have a 5 V board

? The ACS712 is a 5volt only device, and can only be used with 5volt Arduinos.

lupamundur:
when nothing is connected i got reading from my arduino around 0,02-0,03 Amp.

and when i'm connected with 1,7Amp load (reading from my Amp clamp meter), i got around 0,74 Amp from my arduino

You can't display 0.0x amp with a 30Amp hall sensor and Arduino's 10-bit A/D.
There are theoretically only 287 A/D values available to display that 30Amp, so almost a 0.1Amp resolution.
Print to one decimal place. Serial.print(AmpsRMS, 1);

Simple current sensors can be accurate, but only with a resistive load and after calibration.
int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module
66 might have to be changed slightly for the display to show the right current value.
Leo..

Wawa:
? The ACS712 is a 5volt only device, and can only be used with 5volt Arduinos.
You can't display 0.0x amp with a 30Amp hall sensor and Arduino's 10-bit A/D.
There are theoretically only 287 A/D values available to display that 30Amp, so almost a 0.1Amp resolution.
Print to one decimal place. Serial.print(AmpsRMS, 1);

Simple current sensors can be accurate, but only with a resistive load and after calibration.
int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module
66 might have to be changed slightly for the display to show the right current value.
Leo..

I've seen some of your post! Haha. You also post in some of acs712 case in this forum. I had searching in this forum so deep but still can't figure out how to make this acs712 a little bit accurate.

  1. I noticed it will not accurate even slightly 90%
  2. I've tried messing with 70,80,90,100 etc

Can you suggest me any method how to monitoring my ac current A/D by my arduino? I just need a little bit precise on 3-5 Amp to triggering my relay, please? :smiley:

The code from post#0 should work, despite that it takes a detour to get there.

You have picked a sensor with a rather high current rating, making it less suitable to display small currents.

It's wise to calibrate the sensor, e.g. with a 2000watt heater.
I see you live in a 220volt country, so it should display 2000/220 = ~9.1Amp.
Change the '66' value, one number at the time (and re-upload), until you get that current value.
Leo..

Edit: Note that the ACS712, and especially the board it's mounted on, is not really made for 220AC mains power.

Wawa:
? The ACS712 is a 5volt only device, and can only be used with 5volt Arduinos.

yes. I have it here and always after some time I start to plan how to use it with esp8266 or M0 and then I must 'pull the hand brake' in my mind because I (again) realize it has middle value at 2.5 V.

Wawa:
Note that the ACS712, and especially the board it's mounted on, is not really made for 220AC mains power.

It has Hall sensor near the screw terminal. the terminal is good fot thicker cable and the AC doesn't go to PCB I think. I tested it with 2000 W (9 Amp.)

@lupamundur, as I wrote in my first comment, the sensor doesn't measure Amps. it returns some values generated by Hall sensor and circuit on the module. and it is are read by ADC and the result of ADC depends on AREF. the formula is valid, but it needs some calibration value for this unknown factors.

Juraj:
It has Hall sensor near the screw terminal. the terminal is good fot thicker cable and the AC doesn't go to PCB I think. I tested it with 2000 W (9 Amp.)

Voltage is the problem, not current.
The sensor and trace distances on the boards might not comply with local 220/240volt safety regulations.
The ACS712 datasheet mentions 184volt AC max for double insulated (not grounded) devices.
Circuit boards must have a minimum distance between mains and low voltage parts/traces.
I think I have seen 8mm somewhere.
Current transformers (that clip over the insulated wire) should be much safer to use.
Leo..

Wawa:
The code from post#0 should work, despite that it takes a detour to get there.

You have picked a sensor with a rather high current rating, making it less suitable to display small currents.

It's wise to calibrate the sensor, e.g. with a 2000watt heater.
I see you live in a 220volt country, so it should display 2000/220 = ~9.1Amp.
Change the '66' value, one number at the time (and re-upload), until you get that current value.
Leo..

Edit: Note that the ACS712, and especially the board it's mounted on, is not really made for 220AC mains power.

after searching in google and trying to understand the formula of RMS and 0.707 i believe the code is working, and double check acs712 datasheet, it confirmed acs712 work with max voltage 184V, thank you for your help :smiley:

Juraj:
@lupamundur, as I wrote in my first comment, the sensor doesn't measure Amps. it returns some values generated by Hall sensor and circuit on the module. and it is are read by ADC and the result of ADC depends on AREF. the formula is valid, but it needs some calibration value for this unknown factors.

yeah you're right, but again acs712 doesn't seem to work on my 220V. even with calibration, i doubt it would have 70% accuracy.

Wawa:
Voltage is the problem, not current.
The sensor and trace distances on the boards might not comply with local 220/240volt safety regulations.
The ACS712 datasheet mentions 184volt AC max for double insulated (not grounded) devices.
Circuit boards must have a minimum distance between mains and low voltage parts/traces.
I think I have seen 8mm somewhere.
Current transformers (that clip over the insulated wire) should be much safer to use.
Leo..

i came to conclusion not using acs712 as ac current, maybe it suitable for my next dc current project. also i have considered using PZEM-004T With Coil 100A 80-260V or TA12-100. both will work with 220V AC, what do you guys think? :smiley:

I use Grove Electricity Sensor with TA12-200 for 9 A 230 V

I found this development board on my research and it claimed 32bit adc. Let say voltage around 110V, using this STM32 board and acs712 will this be more accurate? Please comment what you think

lupamundur:
I found this development board on my research and it claimed 32bit adc. Let say voltage around 110V, using this STM32 board and acs712 will this be more accurate? Please comment what you think

no. the problem is that you want measure under 3A with 30A module

Juraj:
no. the problem is that you want measure under 3A with 30A module

if i changed it to acs712 5A, it will work perfect right?

List ALL the items you are planning to use AND what you want to measure.
An STM32 (3.3volt micro) and and ACS712 (5volt sensor) can't be used together.
Don't think you can simply measure complex loads with simple code.
Only resistive loads will be 'accurate'.
Leo..

Wawa:
List ALL the items you are planning to use AND what you want to measure.
An STM32 (3.3volt micro) and and ACS712 (5volt sensor) can't be used together.
Don't think you can simply measure complex loads with simple code.
Only resistive loads will be 'accurate'.
Leo..

ummm...

first schematic :
let say i want to measure AC current under 5AC with 110V from main source.
and here's my list item:

  1. acs712 (5A)
  2. STM32, yes some pin only accept 3.3V. but IDK about 3.3 volt micro
    will it work? given that acs712 was in the right position (under 5A, 110v AC), and STM32 working on 32bit ADC. also will it work to measure DC current under 5A and under 100V?

second schematic :
measuring AC current under 5A with 220V from main source.
item list :

  1. acs712 (5A)
  2. STM32
    will it work?

it would be enough for me if it only have 80% accuracy. just wanted to know with my listed item will it be work or not. about the code, maybe i will have to spend some time searching again :smiley:

Wawa:
Only resistive loads will be 'accurate'.
Leo..

by resistive load, it means something that generate magnetic field right? i will placing it far from any magnetic field. actually the thing i want to measure is my air conditioner current, when it reach up to 4A i want it to turn on relay to pump water so heat from ourdoor unit get lower. lowering heat means lowering Amp too. :smiley:

Did you read the second sentence of post#17?
Forget about the STM32, or forget about the ACS712.

So you don't want to measure current, you only want to detect if the aircon is on.
Why don't you use a DS18B20 temp sensor in the air stream.
Much safer than fiddling with AC power.
Leo..