Rms voltage and peak voltage calculation

hi, I am using a sensor that gives a peak to peak voltage of 4, i.e -2 to 2 waveform in analog form but I have to take the difference from the reference voltage of 4v, but I found that the Arduino read only positive voltage, can anyone suggest the code to calculate Vrms and V peak to peak? the frequency of the waveform is 5Hz, I used a special circuit to add the DC value to the waveform to shift the Origin.

hope that helps...

i tried this code but in this i didn't find how he took waveform reading from A0 pin, so i didn't got output.

you need to study the code and cct a bit more closely my friend!

calcV(0, 2.34, 5.0, &Vrms, &Vpp);

the '0' here is synomymous of ch A0!

I thought it is a Digital pin! thanks, I will check again...

I checked but it is not showing the output, is there any changes I have to make for the 5hz frequency?

u probably will as that link shared was designed for 50hz

I am trying the waveform shown in the figure but I need to calculate the peak to peak voltage for the waveform shown in the figure can anyone give me the code I tried almost all but I didn't get op,

We can't see what you tried - maybe you made just a simple mistake.

You need to remember the minimum and maximum with variables that persist longer than loop() (i.e. static or global), and then take the difference between them.

I have merged your topics @RamaKrishna1703.

In the future, please only create one topic for each distinct subject matter.

Thanks in advance for your cooperation.
Per

Like this? (wouldn't have to guess if we could see your circuit).

I tried the code in #post2 but in thinkercad I got an error at return. and I tried the code: for RMS voltage(didn't get the correct value) but I need Vpp also

int pin = A1;
int Voltage = 0;

double V_square = 0;
double accum = 0;
unsigned long avg = 0;
double V_rms =0;
unsigned int period = 2000;
unsigned long t_start = micros();
unsigned long counts = 0;

void setup() {

Serial.begin(9600);
while (micros() - t_start < period){
Voltage = (analogRead(pin));
// Serial.println(Voltage);
V_square = VoltageVoltage;
accum += V_square;
counts++;
//Serial.println(Voltage);
} //Serial.println(accum);
avg=accum /counts;
V_rms=sqrt(avg)/1023
5;
Serial.println( V_rms);
}

void loop() {
delay(1000);
}

Hopefully looking back at that code, you can see why we ask you to please remember to use code tags when posting code

And the error was . . .?

I got this type of error in tinkercad. but in Arduino ide, it is showing no error, but in simulation software, the code is not working.

In function 'double calcV(int, double, double, double*, double*)':
139:3: error: return-statement with no value, in function returning 'double' [-fpermissive]

exit status 1

See reply #9.

You haven't posted that code

You really need to start using code tags

/*
 Name:    readADCVoltage.ino
 Created: 12/29/2021 6:47:41 PM
 Author:  Carlos Balseiro
 Adapted from the code of openenergymonitor (https://github.com/openenergymonitor/EmonLib/blob/master/EmonLib.cpp#L114)
*/

#define ADC_COUNTS 1023

double calcV(int ch, double offsetV, double SupplyVoltage, double* Vrms, double* Vpp)
{

  unsigned int crossCount = 0;             //Used to measure number of times threshold is crossed.
  unsigned int numberOfSamples = 0;        //This is now incremented
  unsigned int timeout = 500;              //mseconds 
  unsigned int crossings = 10;            // sampling 10 trace crossing 
  double adjustedV;
  double sampleV;
  double startV;
  double sqV;
  double sumV = 0;
  int sumMin = 0, sumMax = 0, max = 0, min = 1023, Chng = 0, maxCount = 0, minCount = 0, lastSample, onLow = 0, onUp = 0, oldChng = 0, stayCount = 5;
  double lastVCross;
  double checkVCross;
  int maxValue = 0;
  int minValue = 1024;
  int i = 0;

  //-------------------------------------------------------------------------------------------------------------------------
  // Start
  //-------------------------------------------------------------------------------------------------------------------------
  numberOfSamples = 0;
  crossCount = 0;
  adjustedV = 0;
  double start = millis();
  sumV = 0;                                       //Reset accumulator
  startV = analogRead(ch);
  lastSample = startV;

  //-------------------------------------------------------------------------------------------------------------------------
  // Measurement loop
  //------------------------------------------------------------------------------------------------------------------------- 
  while (crossCount < crossings && (millis() - start) < timeout)     //millis()-start makes sure it doesnt get stuck in the loop if there is an error.
  {
    numberOfSamples++;                       //Count number of times looped.
//-----------------------------------------------------------------------------
// Read in raw voltage samples
//-----------------------------------------------------------------------------
    sampleV = analogRead(ch);
    //---------------------------------------------------------------------------
    // Voltage calculation corrected by offset
    //-----------------------------------------------------------------------------
    adjustedV = sampleV / ADC_COUNTS * SupplyVoltage;
    //Serial.print(adjustedV); Serial.print(" ");
    adjustedV -= offsetV;
    //Serial.println(adjustedV);

    //-----------------------------------------------------------------------------
    // RMS voltage
    //-----------------------------------------------------------------------------  
    sqV = adjustedV * adjustedV;
    sumV += sqV;
    //-----------------------------------------------------------------------------
    // Find the number of times the voltage has crossed the initial voltage
    // - every 2 crosses we will have sampled 1 wavelength 
    // - so this method allows us to sample an integer number of half wavelengths which increases accuracy
    //-----------------------------------------------------------------------------       
    lastVCross = checkVCross;
    if (sampleV > startV) checkVCross = true;
    else checkVCross = false;

    if (numberOfSamples == 1)
      lastVCross = checkVCross;

    if (lastVCross != checkVCross) {
      crossCount++;
    }

    //-----------------------------------------------------------------------------
    // Find max peak and min peak to measure peak to peak voltage
    //-----------------------------------------------------------------------------  
    if (lastSample < sampleV) {               //trace is going up
      if (Chng < 0 && onUp < stayCount)       //If minimum was detected we are going to check there is not a false minimum
        oldChng = Chng;
      if (oldChng < 0 && onUp> stayCount) {   //if we found a minimum and after stayCount samples the trace is going up then recorded the minimum and reset
        sumMin += min;
        min = 1023;
        minCount++;
        oldChng = 0;
        onUp = 0;
        onLow = 0;
      }
      lastSample = sampleV;
      Chng = 1;
      onUp++;
      if (max < sampleV)
        max = sampleV;                      // get max value
    }
    else if (lastSample > sampleV) {        //trace is going down
      if (Chng > 0 && onLow < stayCount)    //If maximum was detected we are going to check there is not a false maximum
        oldChng = Chng;

      if (oldChng > 0 && onLow > stayCount) {   //if we found a maximum and after stayCount samples the trace is going down then recorded the maximum and reset
        sumMax += max;
        max = 0;
        maxCount++;
        oldChng = 0;
        onLow = 0;
        onUp = 0;
      }
      lastSample = sampleV;
      Chng = -1;
      onLow++;
      if (min > sampleV)
        min = sampleV;                      //get min value
    }
    else
      lastSample = sampleV;

  }

  //-------------------------------------------------------------------------------------------------------------------------
  // Vrms and Vpp calculations
  //------------------------------------------------------------------------------------------------------------------------- 
  //Calculation of the root of the mean of the voltage squared (rms)
  *Vrms = sqrt(sumV / numberOfSamples);
   if (minCount != 0 && maxCount != 0)
    *Vpp = (double)(sumMax / maxCount - sumMin / minCount) / ADC_COUNTS * SupplyVoltage;
  else
    *Vpp = 0;

  if (*Vpp < 0)
    *Vpp = 0;

  //Serial.println("Vrms: " + String(Vrms) + "  Vpp: " + String(Vpp));

  //delay(100);

  return;
}

void setup() {
  Serial.begin(115200);
  while (!Serial) {};

}

void loop() {

  // Get Voltage   pin, offset, VSupply
  double Vpp, Vrms;
  calcV(1, 2, 5.0, &Vrms, &Vpp);

  Serial.println("Vrms: " + String(Vrms) + "  Vpp: " + String(Vpp));

}

You promised the compiler that this function would return a value, but it doesn't.

yeah but this code I got from the forum only i checked the code but i didn't understand where to return value.

Using the return statement