ACS712 20A

Hello, I have a problem with the ACS712 current sensor.
I created a smart power tap and I want to calculate the current consumption with the current sensor.
I found a lot of code on Internet and this is what I use :

// Libraries required for Wifi connection
#include <Wire.h>
#include <UnoWiFiDevEd.h>

float amplitude_current;
float effective_value;
float effective_voltage = 110; // Set voltage to 230V (Europe) or 110V (US)
float effective_power;
float zero_sensor;
float power;

void setup()  { //Initialization loop
    pinMode(13,OUTPUT); //Signals to the Arduino that connection 13 must be able to send current
    Wifi.begin(); //Initializes the network settings of the Wi-Fi library
    Wifi.println("Web Server is ok"); //Display when the web server has started
    zero_sensor = getSensorValue(A0);
    Serial.begin(9600);
}
void loop() { //Infinite loop

    while(Wifi.available())  { //as long as wifi reading is available
      readUrl(Wifi); //call to the readUrl function
    }
    
    delay(50); //50ms pause

    // Perform power measurement
  float sensor_value = getSensorValue(A0);
    
  // Convert to current
  amplitude_current = (float)(sensor_value-zero_sensor)/1024*5/100*1000000;
  effective_value = amplitude_current/1.414;
  effective_power = abs(effective_value*effective_voltage/1000);

Serial.print("\n amplitude current = "); 
Serial.print(amplitude_current,2);  
Serial.print("\t effective_value = ");   
Serial.print(effective_value,2);   
Serial.print("\t Power (W) = "); 
Serial.print(effective_power,2);  
delay(1000);

}

// Get the reading from the current sensor
float getSensorValue(int pin)
{
  int sensorValue;
  float avgSensor = 0;
  int nb_measurements = 1000;
  for (int i = 0; i < nb_measurements; i++) {
    sensorValue = analogRead(pin);
    avgSensor = avgSensor + float(sensorValue);
  }    
  avgSensor = avgSensor/float(nb_measurements);
  return avgSensor;
}

However, I got strange values. For example, whatever the device connected to the SPT, the power is always approximatively :

amplitude current = -1.76 effective_value = -1.24 Power (W) = 0.14
amplitude current = 0.24 effective_value = 0.17 Power (W) = 0.02
amplitude current = -1.61 effective_value = -1.14 Power (W) = 0.13
amplitude current = -59.91 effective_value = -42.37 Power (W) = 4.66
amplitude current = -56.54 effective_value = -39.99 Power (W) = 4.40
amplitude current = -52.10 effective_value = -36.85 Power (W) = 4.05
amplitude current = -69.43 effective_value = -49.10 Power (W) = 5.40
amplitude current = -69.34 effective_value = -49.03 Power (W) = 5.39
amplitude current = -53.32 effective_value = -37.71 Power (W) = 4.15
amplitude current = -50.34 effective_value = -35.60 Power (W) = 3.92
amplitude current = -55.76 effective_value = -39.44 Power (W) = 4.34
amplitude current = -41.60 effective_value = -29.42 Power (W) = 3.24
amplitude current = -58.25 effective_value = -41.20 Power (W) = 4.53
amplitude current = -55.13 effective_value = -38.99 Power (W) = 4.29
amplitude current = -59.18 effective_value = -41.85 Power (W) = 4.60
amplitude current = -57.23 effective_value = -40.47 Power (W) = 4.45

Could someone help me pls. You can find attached the schematich of my project. Thanks.

What is IP- connected to? It needs to be connected to something. Also, note that you are measuring AC current (redundant - I know, lol) and you will get all sorts of values for current (both positive and negative).

Hello and thanks for reply,
Here you can find pictures of my project :
https://drive.google.com/open?id=0BylxxJeKtzdycXVCZkpPZjlENkk
Yes i know, but my values are incorrect and how can i keep only positive values ?

Mouk:
Yes i know, but my values are incorrect and how can i keep only positive values ?

No, I don't think you do know. The only way to get only positive measurements is to measure DC current. Why do you want only positive values?

Also, how do you know the values are incorrect?

BTW, are you taking measurements with a load connected to the mains?

One last thing: is that black rocker switch a SPDT?

Are by any chance you trying to measure AC by averaging 1000 readings? That's only going
to work for DC

Also what is the 100 and 1000000 for in this line?

  amplitude_current = (float)(sensor_value-zero_sensor)/1024*5/100*1000000;

You calculate the voltage difference from the zero point in volts, then divide by 10000 in
an inefficient way. You should be dividing by the sensor sensitivity to get the current. The
sensitivity of the 20A device is 0.1V/A:

  amplitude_current = (float)(sensor_value-zero_sensor)/1024*5/0.1;  // current in amps

Hello and thanks for reply,
here is my new program :

// Libraries required for Wifi connection
#include <Wire.h>
#include <UnoWiFiDevEd.h>


void setup()  { //Initialization loop
    pinMode(13,OUTPUT); //Signals to the Arduino that connection 13 must be able to send current
    Wifi.begin(); //Initializes the network settings of the Wi-Fi library
    Wifi.println("Web Server is ok"); //Display when the web server has started
    Serial.begin(9600);
}
void loop() { //Infinite loop
/*
    while(Wifi.available())  { //as long as wifi reading is available
      readUrl(Wifi); //call to the readUrl function
    }*/
    
    delay(50); //50ms pause

  // Perform power measurement

  float amplitude_current = getCurrentValue(A0);
    
  Serial.print("\n amplitude current = "); 
  Serial.print(amplitude_current,2);  

  delay(50);

}


float getCurrentValue(int pin) {
     
    int absVal;
    long int absSum = 0;
    float current;
     
    for ( int i = 0 ; i < 20000 ; i++ ) {
         
        absVal = analogRead(pin);
        absVal = ( absVal - 512 );
        absVal = ( ( absVal >= 0 ) ? ( absVal ) : ( -absVal ) );
         
        absSum = ( absSum + absVal );
         
        delayMicroseconds(50);
         
    }
     
    current = ( (float)absSum );
    current = ( current / 20000.0 );                // Division par le nombre d'éléments
    current = ( ( current * 5.0 ) / 1024.0 );       // Conversion en volt (ADC 10bits=1024pts, ref 5.0V)
    current = ( current * 10.0 );                   // Conversion en ampères (ACS712-20A: 100mV/A=10A/V)
     
    return current;
}

However, I still have incorrect values and I dont understand why.

I know my values are incorrect because I have a wattmeter with me.
For example, when my pc is connected to the device I should have approximatively:
100V | 0.30~0.50Amp | ~30Watt
Could someone help me pls ? Thanks

Mouk:
However, I still have incorrect values and I dont understand why.

I'm going to say this ... one ... last ... time: It's because you are measuring AC current

Your code is for DC current and DC power ONLY. In order to calculate real power in watts for an AC circuit, you need to use the following formulas (in order from top to bottom):

Vrms = Vpeak /sqrt(2);
Irms = Ipeak/sqrt(2);
S = Vrms * Irms;
pf = cos(360fdeltaT)
P = S * pf

Where Vpeak is the peak voltage, Ipeak is the peak current, Vrms is the RMS voltage, Irms is the RMS current, f is the frequency (60Hz), deltaT is the time difference (in seconds) between zero crossing of the voltage and the current, P is real power in watts, S is apparent power in VA, and pf is the power factor.

Lastly, you need to double check these power ratings again:

Mouk:
For example, when my pc is connected to the device I should have approximatively:
100V | 0.30~0.50Amp | ~30Watt

Are they DC values or AC RMS values?

I'd also double check the tutorials you used to see what units the code provides the resulting current in. Is it A, mA, nA, etc?

Now you're averaging 20,000 readings using int's, so its overflowing drastically as int is 16 bits...