Measurement of AC current using display

when the AC current supplies the sensor reads the value correctly but when I decrease the AC value , LCD didn't show the new value. It still reads the old value. I don't face with the same problem when the current increased. I use 20A ACS712 sensor. HELP

What do you suggest? Tarot cards? Entrails? Tea leaves?

we have a project that we are going to calculate the AC current in Arduino. We built a circuit on bread board. We connected all components like lcd,jumpers, ACS712(20A) sensor. Everything is okay to that part after that when we increase the AC current like 5A,6A, 10A etc., it shows on lcd correctly but when we decrease the current from 10A to 6A, it shows the old biggest value. We have to reset the Arduino for read the new value. Could you help me at that point?

The problem is almost certainly in the code that you haven't shown us. There's no point us just trying to guess what you might have done wrong in there.

Steve

#include <LiquidCrystal.h>

LiquidCrystal lcd(7, 8, 9, 10, 11 , 12);
const int sensorIn = A0;
const int mVperAmp = 100; //  20A sensor için kullanılan değer

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

float result;
uint32_t start_time;   
int readValue;             //A0 dan okunan değeri atacağı değişken.   ** voltage
int maxValue = 0;          // store max value here  ** voltage
int minValue = 1024;          // store min value here   ** voltage

int counter = 0;
int activate = 0;
int limit = 5;

void setup(){ 
 Serial.begin(9600);
 lcd.begin(20,4); // 16-2 lik lcd kullanıcaz
}

void loop(){
    Voltage = getVPP();  // Voltage = result olmuş durumda
    VRMS = (Voltage/2.0) *0.707; // bölü kök2 hali
    AmpsRMS = (VRMS * 1000)/mVperAmp;
    Serial.print(AmpsRMS);
    Serial.println(" Amps RMS");

    lcd.setCursor(0,0);  // (sutun, satir)
    lcd.print("readValue:"); // 16 krkter  
    lcd.setCursor(10,0);
    lcd.print(readValue);
    lcd.setCursor(0,1); // Akımı yazdırılıcağı yer
    lcd.print("akim:");
    lcd.setCursor(7,1);
    lcd.print(AmpsRMS);
    lcd.setCursor(0,2);
    

   if( AmpsRMS_old <= limit && AmpsRMS <= limit ){
      activate = 0;
    }
    else {
      activate = 1;
    }
    
    if( AmpsRMS <= limit && activate == 1 ){    
        counter++;
    }
    else{
    }
    // cursor ekle
    lcd.setCursor(0,2);
    lcd.print(counter);
    lcd.setCursor(8,2);
    lcd.print("keregecmedi");
    AmpsRMS_old = AmpsRMS;
}

float getVPP()
{
    start_time = millis();
    while((millis()-start_time) < 1000) //sample for 1 Sec  ** sampling 
    {
        readValue = analogRead(sensorIn);
        // see if you have a new maxValue
        if (readValue > maxValue) 
        {
           /*maksimum sensor değerinı kaydediyor*/
            maxValue = readValue;
        }
        if (readValue < minValue) 
        {
           /*maximum sensor değerni kaydediyor*/    
            minValue = readValue;
        }
    }
   
   result = ((maxValue - minValue) * 5.0)/1024.0;  

   return result; 
}

   /*   result'ın degeri return edildigi icin loop'un birinci satırında yazılı olan Voltage'a gönderilmis oluyor.
   */

Moderator edit:. CODE TAGS. You can edit them into the rest of your posts.

You don't reset maxValue and minValue, so they can only ever increase. Try setting them to 0 before the sampling loop in getVPP.

float getVPP()
{
int minValue=0;
int maxValue=0;

start_time = millis();

while((millis()-start_time) < 1000) //sample for 1 Sec ** sampling

{

readValue = analogRead(sensorIn);
// see if you have a new maxValue
if (readValue > maxValue)
{
/maksimum sensor değerinı kaydediyor/
maxValue = readValue;
}
if (readValue < minValue)
{
/maximum sensor değerni kaydediyor/
minValue = readValue;
}

}

result = ((maxValue - minValue) * 5.0)/1024.0;

return result;
}

We put the min value and max value before the millis as you see above in blue but lcd showed that when we didnt suply any current, it showed 8A so it took it as starting point. For example we gave 2A, it showed 10A. How can we solve it? By the way, decreasing problem is solved according to your advice thanks!

Sorry, set minValue to some big value (eg 1024) so it gets’s decreased by lower readings. Setting it to 0 means it will never be > readValue.

It’s already set as 1024 but when we write there in blue

float getVPP()
{

start_time = millis();

while((millis()-start_time) < 2000) //sample for 1 Sec ** sampling
int minValue=0;
int maxValue=0;

{

readValue = analogRead(sensorIn);
// see if you have a new maxValue
if (readValue > maxValue)
{
/maksimum sensor değerinı kaydediyor/
maxValue = readValue;
}
if (readValue < minValue)
{
/maximum sensor değerni kaydediyor/
minValue = readValue;
}

}

It calculated every Peak to peak value like , given 4A looks like 0-2A;0-3A;0-4A at LCD step-by-step.How can we arrange the biggest peak value and write the only biggest in one period?

I meant the bit in blue, where you reset the values, should be minValue = 1024, maxValue=0. So minValue will be reduced each time you get a lower reading, and maxValue gets increased every time you get a higher reading.

Thank you so much, it worked :) Kralsın

Andy, We have 1 more issue that we made this circuit for plug and socket experiment. Plug enters to socket and stay there for 1.5 second and 2.5 second off. If there is any problem like the plug stuck into socket more than 1.5s. We need to calculate it with timers. If you have any idea about that, could you help me?

Best regards,

I don’t understand what you are trying to do. Could you explain and provide some more details?