Arduino
Arduino
Arduino
// Note Summary
// Note : Safety is very important when dealing with electricity. We take no
responsibilities while you do it at your own risk.
// Note : This AC Current Sensor Code is for ACS712 current module and Hall effect
split core current transformer use.
// Note : The value shown in Serial Monitor / LCD Display is refreshed every
second and is the average value of 1000 sample readings.
// Note : The current measured is the Root Mean Square (RMS) value.
// Note : The analog value per sample is squared and accumulated for every 1000
samples before being averaged. The averaged value is then getting square-rooted.
// Note : The auto calibration (currentOffset1) is using averaged analogRead value
of 1000 samples.
// Note : The auto calibration (currentOffset2) is using calculated RMS current
value including currentOffset1 value for calibration.
// Note : The unit provides reasonable accuracy and may not be comparable with
other expensive branded and commercial product.
// Note : All credit shall be given to Solarduino.
/
*//////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
////////////////*/////////////*/
/* 0- General */
/* 1- AC Current Measurement */
/* 2 - LCD Display */
/* 0- General */
/* 2 - LCD Display */
/* 0- General */
int buttonRead;
buttonRead = analogRead
(0); // Read analog pin A0. Pin A0
automatically assigned for LCD Display Button function (cannot be changed)
/* Up button is pressed */
else if (buttonRead < 200)
{ LCD.setCursor(0,0); LCD.print ("PRESS <SELECT> "); }
/* 1- AC Current Measurement */
currentSampleCount = currentSampleCount + 1;
/* to count and move on to the next following count */
currentLastSample = millis();
/* to reset the time again so that next cycle can start again*/
}
if(currentSampleCount == 1000)
/* after 1000 count or 1000 milli seconds (1 second), do this following codes*/
{
offsetCurrentMean = offsetSampleSum /currentSampleCount;
/* average accumulated analog values for offset purpose */
currentMean = currentSampleSum/currentSampleCount;
/* average accumulated analog values*/
RMSCurrentMean = sqrt(currentMean);
/* square root of the average value*/
adjustRMSCurrentMean = RMSCurrentMean + currentOffset2;
/* square root of the average value including offset value */
FinalRMSCurrent = (((adjustRMSCurrentMean /1024) *5000)
/mVperAmpValue); /* calculate the final RMS current*/
Serial.print(" The Current RMS value is: ");
Serial.print(FinalRMSCurrent,decimalPrecision);
Serial.println(" A ");
offsetSampleSum = 0;
/* to reset accumulate offset sample values for the next cycle */
currentSampleSum =0;
/* to reset accumulate sample values for the next cycle */
currentSampleCount=0;
/* to reset number of sample for the next cycle */
}
if(OffsetRead == 1)
/* Run this code when button SELECT is pressed */
{
currentOffset1 = 0;
/* set back currentOffset as default*/
if(millis()>= offsetLastSample + 1)
/* keep countng time for offset1*/
{
offsetSampleCount = offsetSampleCount + 1;
offsetLastSample = millis();
if(offsetSampleCount == 1500)
/* after 1.5 seconds, run this codes. */
{
currentOffset1 = - offsetCurrentMean;
/* set the offset values */
OffsetRead = 2;
/* go for second offset Settings */
offsetSampleCount = 0;
/* to reset the time again so that next cycle can start again */
}
if(OffsetRead == 2)
/* Run this code after first offset done */
{
currentOffset2 = 0;
/* set back currentOffset2 as default*/
if(millis()>= offsetLastSample + 1)
/* keep countng time for offset2*/
{
offsetSampleCount = offsetSampleCount + 1;
offsetLastSample = millis();
if(offsetSampleCount == 2500)
/* after 2.5 seconds, run this codes. */
{
currentOffset2 = - RMSCurrentMean;
/* set the offset values */
OffsetRead = 0;
/* change the offset mode to original, wait until the button is pressed again */
offsetSampleCount = 0;
/* to reset the time again so that next cycle can start again */
}
/* 2 - LCD Display */
currentMillisLCD = millis();
/* Set counting time for LCD Display*/
if (currentMillisLCD - startMillisLCD >= periodLCD)
/* for every x seconds, run the codes below*/
{
LCD.setCursor(0,0);
/* Set cursor to first colum 0 and second row 1 */
LCD.print("I=");
LCD.print(FinalRMSCurrent,decimalPrecision);
/* display current value in LCD in first row */
LCD.print("A ");
LCD.setCursor(0,1);
LCD.print(" ");
/* display nothing in LCD in second row */
startMillisLCD = currentMillisLCD ;
/* Set the starting point again for next counting time */
}
-----------------------------------------------------------------------------------
-----
// Note Summary
// Note : Safety is very important when dealing with electricity. We take no
responsibilities while you do it at your own risk.
// Note : This AC Current Sensor Code is for ACS712 current module and Hall effect
split core current transformer use.
// Note : The value shown in Serial Monitor is refreshed every second and is the
average value of 1000 sample readings.
// Note : The current measured is the Root Mean Square (RMS) value.
// Note : The analog value per sample is squared and accumulated for every 1000
samples before being averaged. The averaged value is then getting square-rooted.
// Note : The auto calibration (currentOffset1) is using averaged analogRead value
of 1000 samples.
// Note : The auto calibration (currentOffset2) is using calculated RMS current
value including currentOffset1 value for calibration.
// Note : The unit provides reasonable accuracy and may not be comparable with
other expensive branded and commercial product.
// Note : All credit shall be given to Solarduino.
/
*//////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////
////////////////*/////////////*/
/* 0- General */
/* 1- AC Current Measurement */
/* 0- General */
/* 1- AC Current Measurement */
currentSampleCount = currentSampleCount + 1;
/* to count and move on to the next following count */
currentLastSample = millis();
/* to reset the time again so that next cycle can start again*/
}
if(currentSampleCount == 1000)
/* after 1000 count or 1000 milli seconds (1 second), do this following codes*/
{
currentMean = currentSampleSum/currentSampleCount;
/* average accumulated analog values*/
RMSCurrentMean = sqrt(currentMean);
/* square root of the average value*/
adjustRMSCurrentMean = RMSCurrentMean + currentOffset2;
/* square root of the average value including offset value */
FinalRMSCurrent = (((adjustRMSCurrentMean /1024) *5000)
/mVperAmpValue); /* calculate the final RMS current*/
Serial.print(" The Current RMS value is: ");
Serial.print(FinalRMSCurrent,decimalPrecision);
Serial.println(" A ");
currentSampleSum =0;
/* to reset accumulate sample values for the next cycle */
currentSampleCount=0;
/* to reset number of sample for the next cycle */
}