Author Topic: How to detect AC 60Hz mains voltage from ADC numerical reading  (Read 1337 times)

0 Members and 1 Guest are viewing this topic.

Offline drakejestTopic starter

  • Regular Contributor
  • *
  • Posts: 200
  • Country: 00
How to detect AC 60Hz mains voltage from ADC numerical reading
« on: October 19, 2021, 03:10:29 pm »
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
« Last Edit: October 19, 2021, 03:13:06 pm by drakejest »
 

Offline David Hess

  • Super Contributor
  • ***
  • Posts: 16618
  • Country: us
  • DavidH
Re: How to detect AC 60Hz mains voltage from ADC numerical reading
« Reply #1 on: October 19, 2021, 04:23:38 pm »
I would calculate the statistical standard deviation over single cycles until it settles.
 

Offline drakejestTopic starter

  • Regular Contributor
  • *
  • Posts: 200
  • Country: 00
Re: How to detect AC 60Hz mains voltage from ADC numerical reading
« Reply #2 on: October 19, 2021, 04:56:10 pm »
I would calculate the statistical standard deviation over single cycles until it settles.


what do you mean ''over single cycles until it settles." ? can you please elaborate a little more sir.
 

Online magic

  • Super Contributor
  • ***
  • Posts: 6779
  • Country: pl
Re: How to detect AC 60Hz mains voltage from ADC numerical reading
« Reply #3 on: October 19, 2021, 08:00:20 pm »
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?
« Last Edit: October 19, 2021, 08:06:23 pm by magic »
 

Offline David Hess

  • Super Contributor
  • ***
  • Posts: 16618
  • Country: us
  • DavidH
Re: How to detect AC 60Hz mains voltage from ADC numerical reading
« Reply #4 on: October 19, 2021, 08:20:58 pm »
I would calculate the statistical standard deviation over single cycles until it settles.

what do you mean ''over single cycles until it settles." ? can you please elaborate a little more sir.

I 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.
 
The following users thanked this post: drakejest

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5907
  • Country: es
Re: How to detect AC 60Hz mains voltage from ADC numerical reading
« Reply #5 on: October 19, 2021, 08:31:03 pm »
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.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline drakejestTopic starter

  • Regular Contributor
  • *
  • Posts: 200
  • Country: 00
Re: How to detect AC 60Hz mains voltage from ADC numerical reading
« Reply #6 on: October 19, 2021, 09:48:02 pm »
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?

planning 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
 

Offline Cerebus

  • Super Contributor
  • ***
  • Posts: 10576
  • Country: gb
Re: How to detect AC 60Hz mains voltage from ADC numerical reading
« Reply #7 on: October 19, 2021, 10:05:50 pm »
It's not clear what you're trying to measure. Voltage? Frequency?
Anybody got a syringe I can use to squeeze the magic smoke back into this?
 

Offline David Hess

  • Super Contributor
  • ***
  • Posts: 16618
  • Country: us
  • DavidH
Re: How to detect AC 60Hz mains voltage from ADC numerical reading
« Reply #8 on: October 21, 2021, 03:23:41 am »
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.

That 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.
 

Offline Infraviolet

  • Super Contributor
  • ***
  • Posts: 1017
  • Country: gb
Re: How to detect AC 60Hz mains voltage from ADC numerical reading
« Reply #9 on: October 21, 2021, 04:19:12 am »
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.
 

Offline AaronD

  • Frequent Contributor
  • **
  • Posts: 260
  • Country: us
Re: How to detect AC 60Hz mains voltage from ADC numerical reading
« Reply #10 on: October 22, 2021, 05:26:44 pm »
(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.)

Any 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.
 

Offline AaronD

  • Frequent Contributor
  • **
  • Posts: 260
  • Country: us
Re: How to detect AC 60Hz mains voltage from ADC numerical reading
« Reply #11 on: October 22, 2021, 05:32:09 pm »
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.
 

Offline Terry Bites

  • Super Contributor
  • ***
  • Posts: 2393
  • Country: gb
  • Recovering Electrical Engineer
Re: How to detect AC 60Hz mains voltage from ADC numerical reading
« Reply #12 on: October 22, 2021, 06:03:51 pm »
unsigned int average (unsigned int x, unsigned int y)
{
  return (x&y)+((x^y)>>1);
}
 

Offline AaronD

  • Frequent Contributor
  • **
  • Posts: 260
  • Country: us
Re: How to detect AC 60Hz mains voltage from ADC numerical reading
« Reply #13 on: October 22, 2021, 06:14:03 pm »
unsigned int average (unsigned int x, unsigned int y)
{
  return (x&y)+((x^y)>>1);
}

How is that better than `return (x+y)>>1;`?
 

Online magic

  • Super Contributor
  • ***
  • Posts: 6779
  • Country: pl
Re: How to detect AC 60Hz mains voltage from ADC numerical reading
« Reply #14 on: October 22, 2021, 06:48:35 pm »
It doesn't overflow ;)
The 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.

edit
avr-gcc fails at it :P
« Last Edit: October 22, 2021, 06:53:50 pm by magic »
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf