ACS712 and AC current

Hi!

I’m trying to measure current with arduino Mega 2560 and ACS712 module.

First i just wanted to see a graph of input when nothing is connected so i used this code:

int sensorValue;

void setup()
{
  Serial.begin(9600);  // sets the serial port to 9600
}

void loop()
{
  sensorValue = analogRead(0);       // read analog input pin 0
  Serial.println((511-sensorValue), DEC);  // prints the value read
  delay(100);                        // wait 100ms for next reading
}

And i get this graph:

I’m a little bit confused because it’s a lot noiser that a chart from this post:

Also when I tried to measure current with this code:

#define CURRENT_SENSOR A0 // Analog input pin that sensor is attached to

 float amplitude_current;               //amplitude current
 float effective_value;       //effective current 

 void setup() 
 {
   Serial.begin(9600); 
   pins_init();
 }
  void loop() 
 {
   int sensor_max;
   sensor_max = getMaxValue();
   Serial.print("sensor_max = ");
   Serial.println(sensor_max);
   //the VCC on the Grove interface of the sensor is 5v
   amplitude_current=(float)(sensor_max-512)/1024*5/185*1000000;
   effective_value=amplitude_current/1.414;
   //minimum_current=1/1024*5/185*1000000/1.414=18.7(mA)
   //Only for sinusoidal alternating current
   Serial.println("The amplitude of the current is(in mA)");
   Serial.println(amplitude_current,1);//Only one number after the decimal point
   Serial.println("The effective value of the current is(in mA)");
   Serial.println(effective_value,1);
 }
 void pins_init()
 {
   pinMode(CURRENT_SENSOR, INPUT);
 }
 /*Function: Sample for 1000ms and get the maximum value from the S pin*/
 int getMaxValue()
 {
   int sensorValue;             //value read from the sensor
   int sensorMax = 0;
   uint32_t start_time = millis();
   while((millis()-start_time) < 1000)//sample for 1000ms
   {
       sensorValue = analogRead(CURRENT_SENSOR);
       if (sensorValue > sensorMax) 
       {
           /*record the maximum sensor value*/
           sensorMax = sensorValue;
       }
   }
   return sensorMax;
 }

Only line I changed was:

amplitude_current=(float)(sensor_max-516)/10245/1851000000;

512 to 516, because it’s average value that i get when nothing is connected. When i connect my charger to iPad, I get 200mA with this code, but my wattmeter show 100mA. This is how i connected ACS712:

What am I doing wrong? should I use another sensor to measure current?

Its a hall sensor, there is a lot of noise from a hall sensor.

Is this the 5A, 20A or 30A sensor?

I you are trying to measure 0.2A with a 20A or 30A hall sensor, then you will be down in the noise. With the 5A sensor you should see something, but you need to do low-pass filtering to reduce the noise with all of these sensors - but you need to set the breakpoint for the filter to just above mains frequency.

What value filter capacitor is on the breakout board you have?

BTW use floating point constants for floating point values, then you can’t get truncation to 16 bit values:

I’d change:

amplitude_current=(float)(sensor_max-516)/1024*5/185*1000000;

to:

amplitude_current=(float)(sensor_max-516)/1024.0 * 5.0 /185.0 * 1.0e6;

The 185mV value implies you have the 5A sensor - if that’s not the case it will
need to be changed.