AC signal for ADC [solved]

pls believe that i do not want to argue with anybody.
let s take a simple task and do it step by step:
somebody wants to measure the Vpeak of an Ac (sine, triangle, arbitrary anything) 400hz. We take as fact that signal is stable Vpeak.

int val=0;
int meas=0;
for (i=0;i<1000;i++)
{meas=analogRead(A1);
if (meas >val) val=meas;
}

I believe that

  1. above code is an approximation to Vp.
  2. the more samples (=periods are sampled) will at the end get Vp
  3. In every period, peak might not be grabbed, but “not peak reads” (meas <= val) will not affect the recorded (val)

Your comments?

thank you

Whatever you're doing..... maybe you can try using an analog comparator with hysteresis to turn your sine-waves into square-waves. Then feed each edge signal to an arduino input, and use arduino interrupt features to detect the falling edges. Then use the arduino interrupts to get timing information for the periods..... and phase difference between the two square-wave waveforms.

You should take several readings, and average the reading.

Also..... specify what kind of absolute error you're after .... +/- 0.1 degree? +/- 0.05 degree ?

Also, if you want to get peak value of a sine-wave.... I guess you could measure a bunch of points over 1 period.... and repeat....and repeat... get a few sets of data. Then run it through a curve-fit process to get a least-squares fit - applied to a sinusoid.

Southpark: Whatever you're doing..... .

Im trying to display the angle of a resolver, based on sine and cosine signals

Southpark: ..... and phase difference between the two square-wave waveforms.

this is achieved by hardare, ie, ref signal is fed to optocoupler and output transistor is digitalread. so if Vp happens when digitalread is 1 signal is in phase with ref. Same with other signal

Southpark: You should take several readings, and average the reading.

I take an average of 5 last full cycles, thats why i have said that measurements are not flikkering

Southpark: Also..... specify what kind of absolute error you're after .... +/- 0.1 degree? +/- 0.05 degree ?

Since Im working with sine ang cos and their variation is in 1 quadrant (other quadrants come out as about you suggested, by phase timing, and since I have 10 bits, I expect ideally about 0,1 deg (900 bitsteps). BUT : ADC has an accuracy of +-2 lsb max. From this somebody could say that worst case is 4lsb or 0.4 deg. This is correct for start. If use some trigonometry its much better. Anyway, I expect 0,12 deg In practice it is achieved for the full 1st and 3rd quadrant.

And my problem keeps going : Why it is the much greater error when one signal is opposite phase from the other (about +0.4 and -0.25 at midquadrants). The error is less as angle goes nearer the full quadrant region.

Southpark: Also, if you want to get peak value of a sine-wave.... I guess you could measure a bunch of points over 1 period.... and repeat....and repeat... get a few sets of data. Then run it through a curve-fit process to get a least-squares fit - applied to a sinusoid.

I see is a good approach and thank you but dont have time to analyse it. Perhaps I will stay with my errors and correct them with a lookup table (they are steady).

Is the problem now measuring peak value or phase difference?

At 400 Hz both are a challenge.

Phase difference: add an op-amp or schmidt trigger that turns the signal into a block wave (5V out when input is >0, 0V out when input <0), then use interrupts to determine at pretty high precision (microseconds or even better - at 20 mHz clock a single clock tick takes 50 nanoseconds) when the phase transitions take place. That should get pretty accurate values for this (2,500 microseconds per cycle, so at 1 us resolution this can get you to 0.144° phase difference, at 50 ns resolution it’s down to 0.0072°).

Peak value: indeed you could try to sample the signal, the problem is that the signal value changes fast in comparison to the sampling rate. It’s not constant while a sample is taken in the ADC, sampling takes about 0.1 ms. If it’s a sine wave it should be constant enough for a long enough time when at the peak value.

The least-squares fit may also give good results but an Arduino can’t work with too big datasets, just a few kB of memory available, and it requires you to know the exact shape of your input signal which it seems you don’t.

wvmarle: ...you to know the exact shape of your input signal which it seems you don't.

Excellent. For sure it seems I may try to energise a resolver with square!!!

demkat1: Im trying to display the angle of a resolver, based on sine and cosine signals

What's the difference between a sine and cosine, other than the 90°, 1/4 wavelength offset? The shape is the same.

this is achieved by hardare, ie, ref signal is fed to optocoupler and output transistor is digitalread. so if Vp happens when digitalread is 1 signal is in phase with ref. Same with other signal

This introduces errors. Serious errors. It's totally unsuitable for what you're doing. Any signal coming out of such a system is worthless for phase measurements. 1) An optocoupler requires the input signal to reach about 1.2V before the LED switches on. This is a delay from the true start of the cycle. 2) The optocoupler requires your system to supply significant current, 10-20 mA typically. This may upset the rest of your system, depending on it's internal resistance. This resistance is actually a necessity to limit the current through your LED. 3) There's a delay between the LED getting sufficient power to light up, and the phototransistor to start conducting and pulling your digital port HIGH or LOW. 4) Your actual signal varies (it's a wave), so the actual current through the LED varies, and with it the resistance of the phototransistor varies, and you don't know at what voltage level or at which point in the cycle it finally flips your port. This all combined with an unknown waveform means you have no idea what you're measuring.

Since Im working with sine ang cos and their variation is in 1 quadrant (other quadrants come out as about you suggested, by phase timing, and since I have 10 bits, I expect ideally about 0,1 deg (900 bitsteps). BUT :

You're again mixing up voltage (the current value of the signal, which is what the ADC measures) with phase (the moment in time). And what are "bitsteps"?

wvmarle: What's the difference between a sine and cosine, other than the 90°, 1/4 wavelength offset? The shape is the same.

Yeah you got it.

Hmmm but then, hmmm if a resolver shows always 90, what is the use? And the angle simulator? what it produces that is accepted by the angle indicator and displays not 90??? Strange science...

Or someone does not know what is a resolver....and is posting just to increase post count...

Over and out

Indeed, I don't know resolvers, so what?

I'm participating on this forum to discuss people's projects, hopefully help people out by sharing my knowledge, and learn something in the process myself, such as just now about the existence of a device called "resolver" and the use of them.

Unfortunately you don't seem to care about that learning part, instead you're just making dumb sneers trying to hide the fact you have no answers to the many questions asked.

demkat1:
let s take a simple task and do it step by step:
somebody wants to measure the Vpeak of an Ac (sine, triangle, arbitrary anything) 400hz. We take as fact that signal is stable Vpeak.

int val=0;

int meas=0;
for (i=0;i<1000;i++)
{meas=analogRead(A1);
if (meas >val) val=meas;
}




I believe that 
1. above code is an approximation to Vp. 
2. the more samples (=periods are sampled) will at the end get Vp
3. In every period, peak might not be grabbed, but "not peak reads" (meas <= val) will not affect the recorded (val)

Your comments?

thank you

I agree with that. What is next step?

Smajdalf: I agree with that. What is next step?

Make sure this works as expected. After all, this is all you asked for in the very beginning (measure peak value).

When this is working, you may continue with the phase timing - and in the replies above is pretty much all the info you need to get that working, too. Remember that zero-passes are easier to time than peak values.

thanks (and karmas) to
Smajdalf
Southpark
hammy
MorganS,
for being supportive

problem is the leaking or reverse current on clamping schottky diodes.

Someone provided the tip, but i cannot locate now the exact post. Anyway THANKS friend

bye

Could it be #6?

Grumpy_Mike: Putting a zener on the output you are trying to measure is going to screw you up.

Grumpy_Mike: Could it be #6?

Noop. I think was saying "schottkies are leaky"..

i didnt have any use of scottkies in past. When i read it first time i didnt know theycan be THAT leaky and they differ a lot in leakage current depending on type. Ie: bat54 is less than 1microA @10V which means that with 1kohm final output (input to adc) resistor 1mV loss . bat721 is 15 microA @10V which means that with 1kohm final output (input to adc) resistor 15mV loss, that is almost 2bits. Common rectifier schottkies are 1mA which means.....

Anyway it couldnt be anything else. I had change "sources", ihad change arduinos. only the input circuit remain (OR adc inputs by design/contruction , which is clear that cannot affect that much).