it has been a week that I'm struggling with the arduino to measure the RMS Voltage value. By OSC I can see that the RMS voltage value changes around 3V. But I can't see it by serial port. I think my sampling or conversion has problem and I can't find it. I really appreciate it if anyone can take a look at my codes and suggest anything.
float readRMS_new(int pin, int vref) {
int numReadings[100];
const unsigned long samplingInterval = 2; // Interval between RMS calculations in milliseconds
unsigned int readingsSum = 0; // Sum of squared readings
unsigned int readingsCount = 0; // Number of readings taken
unsigned long lastSamplingTime = 0; // Time of the last RMS calculation
float rmsVoltage = 0;
float finalVoltage = 0;
float circuitGain = 4.31; // Gain of the circuit
unsigned long currentTime = millis(); // Current time
// Check if it's time to perform an RMS calculation
if (currentTime - lastSamplingTime >= samplingInterval) {
lastSamplingTime = currentTime; // Update the last sampling time
// Perform RMS calculation
for (int i = 0; i < 100; i++) {
numReadings[i] = analogRead(pin);
readingsSum += pow(numReadings[i] - vref, 2);
readingsCount++;
}
// Calculate RMS voltage
float rms = sqrt(readingsSum / readingsCount);
rmsVoltage = rms * 0.0048;
finalVoltage = rmsVoltage * circuitGain;
if (finalVoltage > 0.0f) {
// Print RMS voltage
Serial.print("RMS Voltage: ");
Serial.println(finalVoltage, 3); // Print with 3 decimal places
}
// Reset variables for the next RMS calculation
readingsSum = 0;
readingsCount = 0;
return finalVoltage;
}
return 0.0f; // Return 0 if it's not time to perform an RMS calculation
}
Regards
Farzaneh