tonilopezmr / tonilopezmr.github.io

My web portfolio.
https://tonilopezmr-github-io.vercel.app
Other
4 stars 1 forks source link

Current Sensor ACS712 #37

Open tonilopezmr opened 5 years ago

tonilopezmr commented 5 years ago

Schematic

Code example

/*
Measuring AC Current Using ACS712
*/
const int sensorIn = A0;
int mVperAmp = 185; // use 100 for 20A Module and 66 for 30A Module

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

void setup(){ 
 Serial.begin(115200);
}

void loop(){
 Voltage = getVPP();
 Serial.print("Zero volts ");
 Serial.print(Voltage);
 VRMS = (Voltage/2.0) * 0.707; 
 Serial.print(" VRMS: ");
 Serial.print(VRMS);
 AmpsRMS = (VRMS * 1000)/mVperAmp;
 Serial.print(" Amps RMS: ");
 Serial.println(AmpsRMS);
}

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

   for(int i = 0; i < 1000; i++) {   
       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;
       }
   }

   // Subtract min from max
   result = ((maxValue - minValue) * 5.0)/1024.0;

   return result;
 }

References

ASC712 Datasheet ACS712 ASC712 with ESP8266