How to Measure Voltage Difference (Between Min. & Max.)?

In one analog pin, let's say a sine wave is applied, whose peak and trough is unknown.

I would like to measure the voltage difference between minimum & maximum values. How do I do this?

A code snippet would be appreciated.

Thank you.

Assuming the signal is relatively clean.....but even if it is not, then have a code that stores two particular values A and B. Give both values an initial starting value.......and that starting value can be the very first measurement gathered by the analog pin.

For future samples of the analog signal, compare your two values against each future sample.

If the most recent sample is less than or equal to the existing value stored in "A", then make the most recent sample equal to the new "A".

If the most recent sample is greater or equal to the existing value stored in "B", then make the most recent sample equal to the new "B".

In the end.... "A" will hold an estimate of the smallest value. While "B" will hold an estimate of the highest value.

Every once in a while, you might need to run the same algorithm using two other variables, eg. "C" and "D" ---- in case your sine-wave changes amplitude levels. This is assuming a 'DC-offsetted' sinusoidal signal.

Alternatively, you could use some hardware to convert the sine-wave to a square-wave, from which you could calculate the period of the sinewave (using timers). Then.....whenever a sinewave zero-crossing occurs, you could probably make the processor wait for one-quarter period. This means wait 1/4 period after a zero-crossing, and then take a sample to measure an instantaneous value of the sine-wave....which will either be a max value or a min value. Because one-quarter period after a zero-crossing gets you to a min or max.

Since I don't know what "scaling" you're using and I don't know the maximum, let's just work with the raw ADC readings (0-1023).

Initialize Min & Max to their "opposites extremes" (at least one of these values will be updated on the 1st read/loop):

Min = 1023;   //Highest possible value
Max = 0;      //Lowest possible value

Then in a loop:

if (X < Min)    // Update if new reading is less than old Min
   Min = X;   

if (X > Max)   // Update if new reading is greater than old Max 
    Max = X;

let's say a sine wave is applied, whose peak and trough is unknown.

A sine wave has a negative peak equal to it's positive peak, and it crosses-through zero twice per cycle, so the minimum (absolute value) is zero. The Arduino can't read negative voltages, but fairly it's common to bias the input at 2.5V . Then, you can optionally subtract-out the bias.

1 Like

And be careful because if you really don't know the amplitude of the signal and it turns out that it is greater than 5V you're in danger of damaging the Arduino.

Steve

If the amplitude is greater than 2.5V it will endanger the Arduino. peak-to-peak voltage is twice the amplitude...