# Function programming (ADS1115 and ACS712)

I trying to measure current using ACS712 is not a big problem. Using the datashet I was able to make for DC, a load-free reading is taken, inserted into the formula and the correct current is read, unfortunately for me it is AC. For AC I need to write a slightly more complicated program that I don't know unfortunately, so I'm looking for someone who could help me. I tried and found a code that can be used via arduino for esp32, etc.

``````my cod for DC
var offset = 2.8365;
var sens = 100;
return msg;

cod for esp 32
/*
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 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
{
// see if you have a new maxValue
{
/*record the maximum sensor value*/
}
{
/*record the minimum sensor value*/
}
}

// Subtract min from max
result = ((maxValue - minValue) * 5.0)/1024.0;

return result;
}
``````

In order to know the RMS value it is necessary to find the peak values. In the code you show it spends one second flat out continually measuring the voltage to find the peak values. You can't do that in node-red without locking the whole system up for that time, and even then it might not be reliable as Linux (assuming you are using linux) is not a real time OS and other things may grab the processor without warning. Don't try and put it in node-red, keep it in a separate device.

1 Like

Thanks Man! I need RTOS

Or no OS at all, as with Arduino/ESP32.

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.