// Arduino Uno/Nano Test of ADC sampling, showing DC V and AC p-p V and sample rate.
// Over a 100ms sample period.
// Base code generated by ChatGPT, unverified in reality
// see definitions for pins used for ADC.
//===========================================================================
const int acPin = A0; // AC voltage connected to A0 (via voltage divider?)
const float acDividerRatio = 1; // AC voltage divider ratio
const float referenceVolt = 5.0; // Reference voltage for Arduino ADC
const int sampleTime = 100; // Sample time in milliseconds
void setup() {
Serial.begin(115200);
}
void loop() {
unsigned long startTime = millis();
float acMin = referenceVolt;
float acMax = 0;
float sumOfSquares = 0;
float acPeakToPeak, acRMS, dcRMS;
int sampleCount = 0, sampleRate;
//----------------------------------------------------------------------
// Start ADC sampling for sampleTime period
while (millis() - startTime < sampleTime) {
// ontime = pulseInLong(acPin, HIGH);
// offtime = pulseInLong(acPin, LOW);
float acRead = analogRead(acPin) * (referenceVolt / 1023.0);
// Adjust the read values based on your voltage dividers
acRead = (acRead - 0) / acDividerRatio; // Remove DC bias and scale
// Find minimum and maximum values for AC
if (acRead < acMin) {
acMin = acRead;
}
if (acRead > acMax) {
acMax = acRead;
}
// Sum the squares of the DC values
sumOfSquares += acRead * acRead;
sampleCount++;
}
//--------------------------------------------------------------------
// Calculate peak-to-peak voltage for AC
acPeakToPeak = acMax - acMin;
// Convert peak-to-peak to RMS for AC
acRMS = acPeakToPeak * 1.0 / (2.0 * sqrt(2)); // Only valid for sinusoidal waveforms
dcRMS = sqrt(sumOfSquares / sampleCount);
// Calculate sample rate
sampleRate = 1000 / sampleTime * sampleCount; // Samples per Second (S/Sec)
Serial.print(sampleCount);
Serial.print(" AC P-P Volts = ");
Serial.print(acPeakToPeak);
Serial.print("v : ");
Serial.print("AC RMS Volts = ");
Serial.print(acRMS);
Serial.println("v");
// Serial.print("Sample rate = ");
// Serial.print(sampleRate);
// //Serial.print(dut1[2][6]); // 0 is first index
// //Serial.print(test_parameters[6]);// 0 is first index
// Serial.println(" S/Sec");
// Wait a bit before next measurement
delay(50);
}