calibrating current sensor with arduino

Hello world,

I'm doing a project to measure AC current / voltage in the main line ( when an appliance is connected to a wall socket ). 230V AC main line.

And I'm using ALLEGRO ACS-758-LCB-050A current sensor for current measurement.
( the acs758 sensor produces an voltage output when a current is flowing through the IC ; sensitivity of the IC is 40mV/A )

But the measured voltage is not stable and fluctuating often .

When I turn the arduino ( ATMEGA328P ) ic on, with no load connected to the project ( so there is no current flowing through the current measuring IC ) then it gives a ADC value around 506 ( this varies in between 505 - 510 with no load connected ).

But when I connect a load ( then there is a current flowing through the IC ), the ADC read value start to fluctuate very fast. And the ADC value drops to 450 even . So it then fluctuate from 450 - 570 .

So my problem is, how this drops to 450 with a load connected ? ( without a load, the reading is stable between 505 - 510 ; so if there is a load connected probably this adc read value must be increased but must not drops below 505 as i know )

The code I used is below . Comments are highly appreciated ! XD

LOAD I USED = A WATER HEATER ( since I couldn't find any other high current appliances from around )

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int numReadings = 30;
float readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
float total = 0; // the running total
float average = 0; // the average

float currentValue = 0;

void setup()
{
Serial.begin(9600);
for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;
lcd.begin(16, 2);
}
void loop()
{
total= total - readings[index];
readings[index] = analogRead(A3); //Raw data reading
readings[index] = (readings[index]-510)*5/1024/0.04-0.04;//Data processing:510-raw data from analogRead when the input is 0; 5-5v; the first 0.04-0.04V/A(sensitivity); the second 0.04-offset val;
total= total + readings[index];
index = index + 1;
if (index >= numReadings)
index = 0;
average = total/numReadings; //Smoothing algorithm from (http://www.arduino.cc/en/Tutorial/Smoothing)
currentValue= average;
// Serial.println(currentValue);
// delay(30);

lcd.setCursor(0, 1);
lcd.print(analogRead(A3));
// Serial.println( readVcc(), DEC );
delay(200);
}

The sensor measures current in both direction.
Without current, the analogRead() is somewhere in the middle. The 505 to 510 are okay.
A negative current will lower that value, a positive current will increase that value.
You are measuring an AC current, so the current is going in both directions with 50Hz or 60Hz.

@Erdin

Thanks for the reply.

So could you please suggest me to solve this ? I mean i want some stable value to be shown in the LCD .

Let's say when its connected a AC LOAD that draws 5A of current ; what i want is to put that in LCD .

So far it's giving both negative and positive ( 450 to 570 ) which can't be a good result to be put on the LCD .

So could you please ?

You have to take a number of samples, and calculate the rms value.

First of all, you have to know the value at 0A. That could be 507. During about 100ms, take many samples and remember the minimum and maximum value. The difference between those and 507 is the current in one and the other direction. Use the average of the currents. Use that value to calculate the rms value.
The result is not accurate, but it is a start.

If you want to get the real rms value, you have to use all samples in the calculation.

Those hall current sensors are very sensitive for magnetic fields caused by current through wires. So you will get some inaccuracy anyway.

Thanks .

First of all i must say i'm a newbie to this arduino world, and also to programming.

BTW,

In my above mentioned code that is done ( 30 samples, and get the average of that ).

My main line is 230V 50Hz .

After seeing a post saying ' you must take samples about 10x the frequency ' ; i changed the sampling up to 500 ( 10x 50Hz ; 500 ).

Then what happens is, the ardunio IC can't handle that speed I guess, because it doesnt give out any output in SERIAL MONITOR or in LCD .

May be 4-Bit LCD speed is not enough to show outputs at this rate, but serial monitor should show as i feel. My problem is none of them works.

Anywy I will try to follow what you said, but please provide me if you find any links regarding calculating the rms voltage of a line with this sensor .

You may try to adapt code from this project:
http://coolarduino.wordpress.com/2013/01/09/audio-vu-meter/
For power monitoring minimum sampling rate 2 kHz, 4 should be o'k ( change 40 to 4)

@Magician

Thanks ! I will try now .

The average of an AC current is zero.
That's why you should use the minimum and maximum.

10x the frequency seems a little low. That is only 10 samples per sine wave.
During 100ms are 5 sine waves. That would be 50 samples.
Let's double that to 100 samples.

So you could take 100 samples, with a delay in between of 1ms.
During that you keep track of the minimum and maximum.

The rms value is the top value divided by the squareroot of 2.
Vrms = Vtop / 1.414
http://www.planetarduino.org/?cat=425
But only for a nice good sine wave !

Here is some information to calculate the real rms value with all samples: http://openenergymonitor.org/emon/buildingblocks/ac-power-arduino-maths
But that is something to implement later on.
This thread is also about it, but it is confusing for me, http://arduino.cc/forum/index.php/topic,133144.0.html

If the sketch is not working, please upload the complete sketch.

@Erdin

Many thanks for your effort and links .

I'm now going through those . I will let you know the result .

Thanks.

Hi.
I have the save problem that you had with ACS758 sensor.
I've read the coolarduino blog but it'don't find the solution :0 (I'm a newbie)
Can you you give me the code you use ?
Can you help me please?!!
Nico

Hi.
I have the save problem that you had with DFRobot 50A Current Sensor (AC/DC) - ACS758 sensor.

can you give me the skecth sourcecode?