ACS712 current sensor with NodeMCU

When we buy an electrical appliance we trust that the load mentioned on specs is correct. Let us try to build something to measure actual power usage of a 220V AC electrical appliance. ACS712 is available is 5A, 20A and 30A current rating. This sensor works on the principal of Hall effect. From the data sheet of ACS712, you can get sensitivity values of each model. Basically, sensor’s analog data pin output ranges between 0 to 1023 and applying sensitivity on this count gives milli volt value for the amperage of current passing through ACS712 IC. This millivolt value is used to calculate amps of current passing from ACS712 IC. To implement it you will need:

  1. ACS712 current sensor ( I have got 5amp sensor, 185 mv sensitivity ).
  2. NodeMCU board.
  3. 5 V 2 amp power supply to NodeMCU (cell phone charger works).
  4. Few jumper cables.
  5. An appliance to connect on AC load (of less than 5 amp).

Connections:

  1. Connect VCC of sensor to 3.3V on NodeMCU.
  2. GND of sensor to GND of NodeMCU.
  3. Datapin of sensor to A0 Analog pin of NodeMCU.

Here the code using RMS formula to measure current and instantaneous power usage (considering 220V AC supply)


const int sensorIn = A0;
int mVperAmp = 185; // use 185 for 5A, 100 for 20A Module and 66 for 30A Module

double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;

void setup(){ 
      pinMode(A0, INPUT);
      Serial.begin(115200);
      delay(10);
      Serial.println(F("Init...."));
}

void loop(){
 Voltage = getVPP();
 VRMS = (Voltage/2.0) *0.707; // sq root
 AmpsRMS = (VRMS * 1000)/mVperAmp;
 float Wattage = (220*AmpsRMS)-18; //Observed 18-20 Watt when no load was connected, so substracting offset value to get real consumption.
 Serial.print(AmpsRMS);
 Serial.println(" Amps RMS ");
 Serial.print(Wattage); 
 Serial.println(" Watt ");
}

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;
       }
/*       Serial.print(readValue);
       Serial.println(" readValue ");
       Serial.print(maxValue);
       Serial.println(" maxValue ");
       Serial.print(minValue);
       Serial.println(" minValue ");
       delay(1000); */
    }
   
   // Subtract min from max
   result = ((maxValue - minValue) * 5)/1024.0;
      
   return result;
 }

I have combined this with my IoT relay project to get real time power consumption of a electricity board over MQTT network. I will post that video soon.