How to detect AC 60Hz mains voltage from ADC numerical reading - Page 1
Forum section: electronics / projects

author: drakejest
author_rank: Regular Contributor
timestamp: October 19, 2021, 03:10:29 pm
content: hello ,I would like to ask what are the techniques in extrapolate that a 240v 60Hertz nominal from a adc readings(i must detect it within a maximum of 10cycles of the 60 hertz upon turn on) and also the formula must also tolerate the dirty siggnal nature of the mains voltagge.The very simple aproach i could think of is check if there is the voltage exceeds a certain voltageg lets say 100v and is happening at realtivily the same interval for 10 times(although the adc samples at a fix interval i do not want to use the interval time so that i wont have to adjust it everytime im playing with the sampling rate.)but is there other ways to do this more reliably?My guess

author: David Hess
author_rank: Super Contributor
timestamp: October 19, 2021, 04:23:38 pm
content: I would calculate the statistical standard deviation over single cycles until it settles.

author: drakejest
author_rank: Regular Contributor
timestamp: October 19, 2021, 04:56:10 pm
content: Quote from: David Hess on October 19, 2021, 04:23:38 pmwhat do you mean''over single cycles until it settles."? can you please elaborate a little more sir.

author: magic
author_rank: Super Contributor
timestamp: October 19, 2021, 08:00:20 pm
content: I wonder if DFT could work?You wouldn't need to calculate all of it, just the frequency bin (or bins?) of interest. Not sure how sensitive it is to frequency mismatch.Also, what's the ballpark sample rate? Any other signals/noise expected?

author: David Hess
author_rank: Super Contributor
timestamp: October 19, 2021, 08:20:58 pm
content: Quote from: drakejest on October 19, 2021, 04:56:10 pmI mean calculate the standard deviation for a group of samples lasting for the duration of one power line cycle, and then repeat it for the next, and the next, until the results start repeating. The last standard deviation represents the RMS after it has settled, within whatever parameters you set. (1)The standard deviation is equal to the AC RMS. Of course doing it this way includes high frequency noise, but that should be insignificant compared to the total. High frequency noise could be filtered out before the standard deviation calculation.(1) If I had enough time to make the measurement, then I would calculate the standard deviation of the standard deviations to make an estimate of the certainty, but 10 cycles is pretty short.

author: DavidAlfa
author_rank: Super Contributor
timestamp: October 19, 2021, 08:31:03 pm
content: Why don't simply use a zero-crossing detector?Connect it to a trigger-schmidt pin and you'lll have a good noise inmunity.Then simply measure the time between changes, you know it's 16.6ms for a full cycle and 8.3ms for a half-wave.

author: drakejest
author_rank: Regular Contributor
timestamp: October 19, 2021, 09:48:02 pm
content: Quote from: magic on October 19, 2021, 08:00:20 pmplanning on 5000 samples per second, but again i dont want it to be sample dependent because i might increase the sampling rate to as high as 32 000 samples per second

author: Cerebus
author_rank: Super Contributor
timestamp: October 19, 2021, 10:05:50 pm
content: It's not clearwhatyou're trying to measure. Voltage? Frequency?

author: David Hess
author_rank: Super Contributor
timestamp: October 21, 2021, 03:23:41 am
content: Quote from: DavidAlfa on October 19, 2021, 08:31:03 pmThat can work but line noise can be too high to get a good period measurement. If accuracy is required, then it is better to phase lock an oscillator to the line and make the measurement of the phase locked signal.

author: Infraviolet
author_rank: Super Contributor
timestamp: October 21, 2021, 04:19:12 am
content: I have some sample code for AVR microcontrollers which can read an ADC and identify if a user specified frequency is present. I've used in for applications in the few KHz range (can't really cope above 10KHz due to limitations of microcontrolrl clock speed), but with modifications it could be made to work right down at 60Hz. I'll post if you're interesed, but need to check how code ought to be formatted on this forum.

author: AaronD
author_rank: Frequent Contributor
timestamp: October 22, 2021, 05:26:44 pm
content: Quote from: drakejest on October 19, 2021, 03:10:29 pmAny time you're using a series of digital samples to mean something, you're working with Digital Signal Processing (DSP). A lot of that field looks intimidating at first glance, and there are ways to do simple things without making it look like you're in that world. But you still are, and the same fundamental rules still apply.One of those rules is that *all* DSP functions are based on samples and care nothing about time. If you vary the sample rate, then all of your frequency settings track that variation perfectly. So if you dive into using FIR's, IIR's, FFT's, or anything else that has to do with frequency, then you have to redo all of that math to get the same behavior with the new sample rate. Thus, when physical frequency is mentioned in a DSP equation, it's *always* as a ratio with the sample rate: Fc/Fs.It might appear that you can get around this by using an additional, slower clock that is unrelated to the stream of samples, and grab your working set at that rate. But what you're actually doing there is decimation. Still a DSP concept, and the most trivial and naive form of re-sampling. Nevertheless, if the highest frequency captured (interest means nothing here) is less than Nyquist at that lower rate, it works just fine.You can conceptually build an arbitrary re-sampler by finding an integer ratio between the source and destination rates, stuffing zero-valued samples into the original, lowpassing it (now you have a smooth signal that is ridiculously oversampled), and then taking the samples you need from that (same decimation as above). Then combine all the steps to end up with what is essentially a modified single-stage FIR lowpass, that only calculates what it needs and inserts the appropriate zeros at its input.Something that I've done a lot in my projects is to run the ADC as fast as it will go, free-running, and put a "quick and dirty" lowpass in the ISR. (1st-order filter, or "exponential average", implemented using shifts and a lot of arithmetic rearranging to make it trivial to code: just 2 lines in C) The output of that lowpass goes to a global variable that everything else can grab whenever it gets around to it. Effectively, this oversampling followed by a lowpass and decimation, converts speed into resolution, and is also used in pretty much every audio ADC. In that form, it's called "sigma-delta".One of the huge benefits of this, that may not be immediately apparent, is that the requirements are greatly relaxed for an analog lowpass that comes before the ADC. Your *initial* sample rate determines the Nyquist frequency for the analog signal, and then the digital lowpass finishes the job. For example, the initial sample rate for an audio ADC is typically in the mid-MHz range. The chip then lowpasses it at that rate and pulls samples to send out at 48kHz or whatever the actual output is. So the analog filter only has to care about that initial mid-MHz rate...for audio! That is MUCH easier to design and cheaper to make! And then the digital filter has the "brick wall" response that is needed to fit a 20kHz signal with no attenuation into 48kHz samples.If you run your ADC at, say, 10ksps (10kHz sample rate) for a 60Hz signal of interest, then you're roughly in the same ballpark as that audio ADC.

author: AaronD
author_rank: Frequent Contributor
timestamp: October 22, 2021, 05:32:09 pm
content: Or if all you want to do is detect if you HAVE an AC signal or not, then I'd take a completely different approach:Don't use the ADC at all, but a capacitive dropper to drive the LED side of an optoisolator. (still have a resistor there, but the capacitor does most of the work) The transistor side then drives a digital input just like a switch would. If that digital input wiggles, you have power. If not, you don't.If you care about the phase, then omit the capacitor and let the resistor do all the work...and for a much safer design, run that from the secondary side of a transformer. Might even be the same transformer that powers the rest of the project.

author: Terry Bites
author_rank: Super Contributor
timestamp: October 22, 2021, 06:03:51 pm
content: unsigned int average (unsigned int x, unsigned int y){return (x&y)+((x^y)>>1);}

author: AaronD
author_rank: Frequent Contributor
timestamp: October 22, 2021, 06:14:03 pm
content: Quote from: Terry Bites on October 22, 2021, 06:03:51 pmHow is that better than `return (x+y)>>1;`?

author: magic
author_rank: Super Contributor
timestamp: October 22, 2021, 06:48:35 pm
content: It doesn't overflowThe inputs need to be cast to a wider type before addition.That being said, on a CPU with "rotate right through carry" instruction the straightforward formula can be implemented very efficiently if the compiler is smart enough to know which bits really matter and how to use the instruction.editavr-gcc fails at it