ACS712 Current Sensor Module

I need to watch on/off state of my water pump ,I bay ACS712 20A Current Sensor Module and for test use AC 220v/40w lamp...
The value of reading sensor is 510 or 511 when lamp is off.. thats ok and i was thinking to write code when value is bigger or smaller of 510 or 511 thats state ON ...but my problem when Lamp is ON ... value on sensor oscilating 509,510,511,515...
I use average ,of 20 readings of sensor but still same problem... any sugestion for problem welcome

my code

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

for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0; // initialize all the readings to 0:

total= total - readings[index]; // subtract the last reading:
readings[index] = analogRead(analogInput1); // read from the sensor:
total= total + readings[index]; // add the reading to the total:
index = index + 1; // advance to the next position in the array:
if (index >= numReadings) // if we're at the end of the array...
index = 0; // ...wrap around to the beginning:
average = total / numReadings; // calculate the average:

The value of reading sensor is 510 or 511 when lamp is off.. thats ok

No, it's not. Current can never be negative, so the minimum reading when there is no current flowing is 0.

A 40W lamp at 220V will draw about 0.2A. I don't know what the sensitivity of the mysterious sensor you have is, but, you really should be testing it with something that draws more current.

PaulS:

The value of reading sensor is 510 or 511 when lamp is off.. thats ok

No, it's not. Current can never be negative, so the minimum reading when there is no current flowing is 0.

No, the output voltage range is 0 to Vcc of the sensor, where the zero current output voltage value is Vcc/.5, so for a +/- 5 amp sensor powered with 5vdc Vcc, the output voltage measurement range is -5 amps = 0 volts, zero current = 2.5vdc and +5 amps = +5vdc. So in a AC circuit there is most defiantly both negative and positive currents flowing as the voltage and current reverses every 8.333 milliseconds assuming a 60 hz AC circuit.

A 40W lamp at 220V will draw about 0.2A. I don't know what the sensitivity of the mysterious sensor you have is, but, you really should be testing it with something that draws more current.

While this type of sensor can measure both DC and AC currents, it's pretty difficult to actually derive the true RMS current value of the AC load current. Just taking a average of many readings of an ac current will result in a zero reading as AC has equal negative and positive values and will not give the a true reading. One might just keep track of the maximum positive (or negative value) and then convert the peak value to a RMS value, assuming a resistive load. If you are measuring the load current of a capacitive or inductive load then it gets even more complex.

Bottom line is that this sensor is great for measuring DC currents but AC currents require a lot of extra processing (either with external circuitry or code) of the measurement value.

Lefty

I use average ,of 20 readings of sensor but still same problem.

It's AC, the average will always be 0, or in the case of these sensors ~VCC/2.

Do as RetroLefty says, just record the positive readings, ie those >= 512 or whatever your "no current" reading is.


Rob

As was pointed out, reading the sensor value at a random time will return a random value.

Try something like this:

Set two variables, threshholdHigh and threshholdLow. You can experiment with the values, perhaps start with 500 and 525.

Read the sensor every 1.5 or 2 milliseconds

if, at any time, you get a value outside the threshhold value range you can assume that the pump is on.
A 1/4 cycle of 60 Hz (time between 0 current and max current) is about 4 ms, for 50 Hz, is about 5 ms

If, at any time, you get two (or three) readings between the threshhold values, you can assume it's off.

Have a look at Blink Without Delay for the best way to time your sensore readings.

I find another solution with diodes ... its ok for my... Thank for help..