// 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 int dcPin = A1; // DC voltage connected to A1 (via voltage divider?)
const float acDividerRatio = 1; // AC voltage divider ratio
const float dcDividerRatio = 1; // DC voltage divider ratio
const float referenceVolt = 5.0; // Reference voltage for Arduino ADC
const int sampleTime = 100; // Sample time in milliseconds
int test_parameters[7] = {1, 2, 3, 4, 5, 6, 7};
int dut1[5][8] = {
{0, 1, 2, 3, 4, 5, 6, 7},
{1, 97, 109, 121, 92, 64, 245, 101},
{2, 98, 110, 122, 124, 36, 241, 119},
{3, 99, 111, 43, 60, 38, 49, 114},
{4, 100, 112, 45, 62, 39, 50, 97}
};
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) {
float acRead = analogRead(acPin) * (referenceVolt / 1023.0);
float dcRead = analogRead(dcPin) * (referenceVolt / 1023.0);
// Adjust the read values based on your voltage dividers
acRead = (acRead - 0) / acDividerRatio; // Remove DC bias and scale
dcRead = dcRead / dcDividerRatio; // 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 += dcRead * dcRead;
sampleCount++;
}
//--------------------------------------------------------------------
// Calculate peak-to-peak voltage for AC
acPeakToPeak = acMax - acMin;
// Convert peak-to-peak to RMS for AC
//acRMS = acPeakToPeak / (2 * sqrt(2)); // Only valid for sinusoidal waveforms
// Calculate RMS for DC
dcRMS = sqrt(sumOfSquares / sampleCount);
// Calculate sample rate
sampleRate = 1000 / sampleTime * sampleCount; // Samples per Second (S/Sec)
// Print the results
Serial.print("DC RMS Volts = ");
Serial.print(dcRMS);
Serial.print("v : ");
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(1000);
}