Constant Result on ACS712 Sensing [whether i plug the power supply or not]

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 :slight_smile:

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;
 }

when the power supply is plugged.jpg

when the power supply is unplugged.jpg

In your first attemot:

 float rms = sqrt((float)currentAcc/(float)numSamples) * (27 / 1024.0);		//sensitivity for 5A
{
rms=0;
}

current=rms;

Calculate the rms value and then set it to 0. Then, set current to rms (which is now 0). Then, wonder why current is always 0. Well, duh! Isn't that obvious?

In your other examples, it is hard to follow what you are doing, since there are no comments, and you uselessly cast doubles to doubles.

You need to post a schematic and a photo of your implementation of that schematic AND a link to the sensor.