const int sensorIn = A0;
int mVperAmp = 100; // use 100 for 20A Module and 66 for 30A Module
double Voltage = 0;
double VRMS = 0;
double VRMS1 = 0;
double AmpsRMS = 0;
void setup()
{
Serial.begin(115200);
}
void loop()
{
Voltage=getVPP();
VRMS=(Voltage/2.0)*0.707;
AmpsRMS=(VRMS*1000)/mVperAmp;
Serial.print(AmpsRMS);
Serial.println(" Amps RMS");
// put your main code here, to run repeatedly:
}
float getVPP()
{
float result;
int readValue;
int maxValue=0;
int minValue=4095;
uint32_t start_time=millis();
while((millis()-start_time)<1000) //sample for 1 sec
{
readValue=analogRead(sensorIn);
if(readValue>maxValue)
{
maxValue=readValue;
}
if(readValue<minValue)
{
minValue=readValue;
}
}
//maxValue = 4095-maxValue;
//minValue=4095-minValue;
result=((maxValue-minValue)*3.3)/4095.0;
Serial.println(maxValue);
Serial.println(minValue);
return result;
}