This is my first post, so I hope it is in the correct place. The purpose of this project is to measure the current that passes through. I'm using ACS712 5A and using 18 watt fluorescent lamp with 220 volt.
The problem is i keep getting on constant result whether i plug the power supply or not.
Can somebody here help me to spot is there something wrong with my code?that would really help me a lot
My first attempt
float current=0;
const int currentPin = A0;
const unsigned long sampleTime = 100000UL;Â Â Â Â Â Â Â Â Â Â Â Â Â // sample over 100ms, it is an exact number of cycles for both 50Hz and 60Hz mains
const unsigned long numSamples = 250UL;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // choose the number of samples to divide sampleTime exactly, but low enough for the ADC to keep up
const unsigned long sampleInterval = sampleTime/numSamples; // the sampling interval, must be longer than then ADC conversion time
const int adc_zero = 510;Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â // relative digital zero of the arudino input from ACS712 (could make this a variable and auto-adjust it)
void setup()
{
Serial.begin(9600);
}
void loop()
{
CurrentSense();
Serial.println(current);
delay(1000);
}
void CurrentSense()
{
unsigned long currentAcc = 0;
unsigned int count = 0;
unsigned long prevMicros = micros() - sampleInterval ;
while (count < numSamples)
{
 if (micros() - prevMicros >= sampleInterval)
 {
  int adc_raw = analogRead(currentPin) - adc_zero;
  currentAcc += (unsigned long)(adc_raw * adc_raw);
  ++count;
  prevMicros += sampleInterval;
 }
}
float rms = sqrt((float)currentAcc/(float)numSamples) * (27 / 1024.0); //sensitivity for 5A
{
rms=0;
}
current=rms;
}
My second attempt
/*
Measuring AC Current Using ACS712
www.circuits4you.com
*/
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(9600);
}
void loop(){
Voltage = getVPP();
VRMS = (Voltage/2.0) *0.707;Â //root 2 is 0.707
AmpsRMS = (VRMS * 1000)/mVperAmp;
Serial.print(AmpsRMS);
Serial.println(" Amps RMS");
}
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 minimum sensor value*/
     minValue = readValue;
   }
 }
Â
 // Subtract min from max
 result = ((maxValue - minValue) * 5.0)/1024.0;
  Â
 return result;
}
My third attempt
/*
Measuring AC Current Using ACS712
www.circuits4you.com
*/
const int sensorIn = A0;
const unsigned long numSamples = 250UL;Â Â
int mVperAmp = 186; // use 100 for 20A Module and 66 for 30A Module
double Voltage = 0;
double VRMS = 0;
double AmpsRMS = 0;
void setup(){
Serial.begin(9600);
}
void loop(){
Voltage = getVPP();
VRMS = sqrt ((double)Voltage/(double)numSamples);
AmpsRMS = (VRMS * 1000)/mVperAmp;
Serial.print(AmpsRMS);
Serial.println(" Amps RMS");
}
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 minimum sensor value*/
     minValue = readValue;
   }
 }
Â
 // Subtract min from max
 result = ((maxValue - minValue) * 5.0)/1024.0;
  Â
 return result;
}