Better AC sensing circuit for ADC conversion

drjohsmith

Joined Dec 13, 2021
1,614
measuring voltage accurately is certainly nice but... in this application i don't think it makes any difference (True RMS, average, peak...). all that matters is reading that changes proportionally with mains voltage. more over, low values are not important - if the voltage drops from 220 to 5V or 15V, or 60V, it makes no difference - all of them are too low. and that means that puny 0.6V drop due to diode makes no significant difference.

so idea in the very first post to measure peak voltage can work but ... it could (and should) use transformer for isolation.
in fact it should be good enough even without dedicated transformer. one could use same transformer that powers Arduino and relays. the key is measuring DC bus voltage before regulator(s). after all load is constant - most of the time. the only change is at the moment you are switching to another relay and - it is always just one relay that needs to be on (so constant load).

i did a little test:
View attachment 362130

as you can see voltages at nodes marked ADC0 and ADC1 track changes in V1 voltage.

at first it was voltage divider R2/R3 with smoothing cap C2. D5 was added to clamp voltage to safe value to analog input does not get harmed. R4 and C3 are added for extra filtering.
View attachment 362131

and the above graphs are after i introduced a bit of disturbance (I1 to simulate some change in load).
zoomed in it is something like this:
View attachment 362132

schematics could look something like this:
View attachment 362133
the red lines are for high current. TR1 should produce high enough voltage for relays even when mains voltage is down. due to large swing in unregulated voltage across C2, 12V regulator should be switching type and the simplest option here is to use ready made unit.
looks like a good circuit to me ,
I like U3 in place of diodes / fets . neat.
when the relays switch, will the 12v stay up or glitch. U1 is a dcdc ? does it need a specific minimum output capacitance ?
 
Last edited:

drjohsmith

Joined Dec 13, 2021
1,614
This average is called a "running average".
256 could be too long depending on your sampling rate.
or could be too short, if your sampling fast !

Assuming your not trying to sample one exact cycle in ( a lot of work for little if any gain )

The thing to do is to ensure your averaging over more than one cycle of the mains

chose your sample rate ( ADC ) and summation amount accordingly
 

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
measuring voltage accurately is certainly nice but... in this application i don't think it makes any difference (True RMS, average, peak...). all that matters is reading that changes proportionally with mains voltage. more over, low values are not important - if the voltage drops from 220 to 5V or 15V, or 60V, it makes no difference - all of them are too low. and that means that puny 0.6V drop due to diode makes no significant difference.

so idea in the very first post to measure peak voltage can work but ... it could (and should) use transformer for isolation.
in fact it should be good enough even without dedicated transformer. one could use same transformer that powers Arduino and relays. the key is measuring DC bus voltage before regulator(s). after all load is constant - most of the time. the only change is at the moment you are switching to another relay and - it is always just one relay that needs to be on (so constant load).

i did a little test:
View attachment 362130

as you can see voltages at nodes marked ADC0 and ADC1 track changes in V1 voltage.

at first it was voltage divider R2/R3 with smoothing cap C2. D5 was added to clamp voltage to safe value to analog input does not get harmed. R4 and C3 are added for extra filtering.
View attachment 362131

and the above graphs are after i introduced a bit of disturbance (I1 to simulate some change in load).
zoomed in it is something like this:
View attachment 362132

schematics could look something like this:
View attachment 362133
the red lines are for high current. TR1 should produce high enough voltage for relays even when mains voltage is down. due to large swing in unregulated voltage across C2, 12V regulator should be switching type and the simplest option here is to use ready made unit.
Undoubtedly a good suggestion compared to others. Looks like the circuit on the first post was a bit okay, something was wrongly connected. It will be better if you share the LTSPICE simulation. May be I can use low drop diode rectifier here. Getting 220VAC to 3VAC transformer is difficult . Yes, in real circuit LM7812/05 are necessary to power up Arduino Uno. For 6 relay setting I can simulate it in Proteus. At Fuse F1 a varister parallel with resistor may also use.
 

Ian0

Joined Aug 7, 2020
13,158
This average is called a "running average".
256 could be too long depending on your sampling rate.
It’s what I use Sampling at 1600 per second. Actually, i do it twice to give a 2nd order critically damped filter. It’s still only 10 instructions on an ARM.
 

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
Hi,

In theory you do not have to rectify, but it does make it harder to sample. It is best to be synced to the line frequency so you can sample the sine wave itself. The way some expensive power converters used to do it was to sample in two places: maybe about 20 degrees to the left of the peak and 20 degrees to the right of the peak. In fact, the magic angle is +45 degrees, so if you sample at +45 and -45 degrees, add the two samples then divide by 2, you automatically get the RMS value of the sine wave, assuming it is always a sine wave or decent approximation (not always true though with a soft power line).

If you are looking for a stable average DC value though, I'd stick with averaging over many samples either with the two 45 degree samples or rectified DC. I can tell you for sure if you do use rectified DC you can get some really stable DC voltage measurements stable to several decimal digits. You use a reasonable RC low pass filter followed by averaging samples in code. The more samples you average the more stable the reading is, but you do sacrifice speed if the DC value changes suddenly. One formula would be:
Vavg=(Vavg*(N-1)+Vsample)/N
where N is roughly the time constant, so if N=10 then if you calculate this 50 times you will get a result that is within 1 percent of the final value.
This requires floating point math, but there are integer versions too where floating point is only needed in the first and last calculations.
Thank you.
Hi,

In theory you do not have to rectify, but it does make it harder to sample. It is best to be synced to the line frequency so you can sample the sine wave itself. The way some expensive power converters used to do it was to sample in two places: maybe about 20 degrees to the left of the peak and 20 degrees to the right of the peak. In fact, the magic angle is +45 degrees, so if you sample at +45 and -45 degrees, add the two samples then divide by 2, you automatically get the RMS value of the sine wave, assuming it is always a sine wave or decent approximation (not always true though with a soft power line).

If you are looking for a stable average DC value though, I'd stick with averaging over many samples either with the two 45 degree samples or rectified DC. I can tell you for sure if you do use rectified DC you can get some really stable DC voltage measurements stable to several decimal digits. You use a reasonable RC low pass filter followed by averaging samples in code. The more samples you average the more stable the reading is, but you do sacrifice speed if the DC value changes suddenly. One formula would be:
Vavg=(Vavg*(N-1)+Vsample)/N
where N is roughly the time constant, so if N=10 then if you calculate this 50 times you will get a result that is within 1 percent of the final value.
This requires floating point math, but there are integer versions too where floating point is only needed in the first and last calculations.
Do you want me to use this idea into the code that I posted ?
 

drjohsmith

Joined Dec 13, 2021
1,614
Undoubtedly a good suggestion compared to others. Looks like the circuit on the first post was a bit okay, something was wrongly connected. It will be better if you share the LTSPICE simulation. May be I can use low drop diode rectifier here. Getting 220VAC to 3VAC transformer is difficult . Yes, in real circuit LM7812/05 are necessary to power up Arduino Uno. For 6 relay setting I can simulate it in Proteus. At Fuse F1 a varister parallel with resistor may also use.
May be you could try to make the spice model ?
Don't know where you are , but in Europe, the transformer is common and cheap
Not keen on using varistor / resistor instead of a fuse ..sort of defeats the reason for a fuse.
 

panic mode

Joined Oct 10, 2011
5,013
U1 is a dcdc ? does it need a specific minimum output capacitance ?
yes it is a switching regulator. external capacitors are not required but do not hurt:
https://www.mouser.ca/datasheet/3/76/2/oki_78sr.pdf


Undoubtedly a good suggestion compared to others. Looks like the circuit on the first post was a bit okay, something was wrongly connected. It will be better if you share the LTSPICE simulation.
i did not save it... but it is only few parts and easy to recreate.

May be I can use low drop diode rectifier here.
what for...?

Getting 220VAC to 3VAC transformer is difficult .
why 3V? it could be 18V or 24V or whatever, just adapt value of top resistor in voltage divider. changing single resistor is a cheap fix. just open any old wall wart and you got yourself a small transformer. in last image of #177 one could use 15, 18 or 24VAC transformer, only C2 need to have correct voltage rating.
if the nominal mains voltage is 220VAC and range is 180-260VAC then the secondary of the transformer rated for 15V would fluctuate in range 13-17VAC. rectified DC voltage would be 1.4x higher (18-24VDC). so C2 could be rated for 35V or higher.

Yes, in real circuit LM7812/05 are necessary to power up Arduino Uno.
bad choice... DC supply varies a lot (just like mains) and that is exactly the weak point of linear regulators. plus LM78xx require at least 2.5V overhead to regulate (so 7812 needs at least 12+2.5=14.5VDC input). but that is on the low side. since your mains varies +/-25% or more, so will rectified DC output. and when this DC is high, voltage regulator will be burning a lot of heat. it is MUCH better to use SMPS to step down voltage. and this is what switching regulators like one i proposed are great for: wide input voltage range, no heat, stable output. and the current draw would reduce if input voltage increases (it is a win-win).

another thing is 7805... an equivalent of that is already on board of Arduino. and you need to supply Vin with higher voltage (at least 5+2.5=7.5V) so good candidates are 7808 and 7809. since this one is only dropping 3-4V at low current, linear regulator is fine.

One could try using 7805 and bypass internal regulator but this is something that should be tried first but i am not fan of backfeeding idea.

At Fuse F1 a varister parallel with resistor may also use.
Not sure what you mean...
 
Last edited:

panic mode

Joined Oct 10, 2011
5,013
you can use proper ACDC 12V supply for powering controls.
unit like RS-15-12 is small, reliable, low cost, wide input range and simply perfect for the job.
that way small isolation transformer is used only for monitoring mains.
 

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
you can use proper ACDC 12V supply for powering controls.
unit like RS-15-12 is small, reliable, low cost, wide input range and simply perfect for the job.
that way small isolation transformer is used only for monitoring mains.
I will try to follow your design to Arduino uno, you are using 9V Vin to your MCU. Arduino uno has built in 5V and 3.3V VCC. If I finalize it I have to design my PCB from scratch meaning that some part of Arduino Uno can be use. Yes, relay also need 12V supply.
 
Last edited:

panic mode

Joined Oct 10, 2011
5,013
Arduino Uno should be fine with direct 12V if current draw by peripherals is low. if the Arduino on-board 5V regulator gets toasty, you can drop 12V by inserting couple of diodes or 78L08 or similar.

as for my "design", it is just a quick and dirty sketch - just to have a conversation piece. i threw in OLED display and encoder and they make a nice pair but couple of buttons and LEDs can work too. i kept Arduino as you mentioned working with it... other key objectives were simplicity, low cost, scalability (if you are considering different number of relays), thermal efficiency and most importantly isolation from mains, so that thinkering with controls does not put you at risk.

one can make it fancier if and when needed - like adding zero-crossing detector and sampling AC wave. but this complicates things and from what i've seen so far - it is not necessary.
 

eetech00

Joined Jun 8, 2013
4,709
I would not rectify the AC input. Step it down with an isolation transformer, condition the AC input to BIAS at VCC/2 and add a couple of diodes for input protection. Probably need about four relays for auto-transformer tap selection. Let the arduino do all the math, even True RMS, if that's what you want to do.
 

drjohsmith

Joined Dec 13, 2021
1,614
I would not rectify the AC input. Step it down with an isolation transformer, condition the AC input to BIAS at VCC/2 and add a couple of diodes for input protection. Probably need about four relays for auto-transformer tap selection. Let the arduino do all the math, even True RMS, if that's what you want to do.
Do you even need to bias the input , what happens if you earth one side of the transformer secondary ?
 

drjohsmith

Joined Dec 13, 2021
1,614
The signal will clip. The whole sinewave needs to captured to calculate True RMS.
True , for a scientific instrument
But the OP is selecting to switch an output up or down in 10 volt steps , having averaged many AC cycles
This is typical AC mains , just how True do you need to be ?
The OP has posted many circuits of "working" designs , none have anywhere near real RMS ..
Just a thought
 

eetech00

Joined Jun 8, 2013
4,709
True , for a scientific instrument
But the OP is selecting to switch an output up or down in 10 volt steps , having averaged many AC cycles
This is typical AC mains , just how True do you need to be ?
The OP has posted many circuits of "working" designs , none have anywhere near real RMS ..
Just a thought
If you want use peak, you still can, but why do that when you can capture the whole waveform just as easily.
The signal conditioning is only about 7 parts (no opamp buffer).
 

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
I would not rectify the AC input. Step it down with an isolation transformer, condition the AC input to BIAS at VCC/2 and add a couple of diodes for input protection. Probably need about four relays for auto-transformer tap selection. Let the arduino do all the math, even True RMS, if that's what you want to do.
@eT well comeback, draw a circuit then. But this issue you explain here already discuss 3 or 4 times. Do you have any plan to design Arduino Uno lib in Ltspice ?
 

Thread Starter

Hasan2019

Joined Sep 5, 2019
199
@drjohsmith , kindly keep track on the post number #169 and #177, only try to help on this 2. If anything pops up I will post here. In post #177, @panic mode was trying to explain the 1st circuit behavior and the relevant design on the next. Currently, I am working on proteus. Better not to arise a suspicious issue and that may screwed up things here. Lets move bit slow now.
 

MrAl

Joined Jun 17, 2014
13,716
Vavg=(Vavg*(N-1)+Vsample)/N
is
Vavg = Vavg + (Vsample - Vavg)/N

Choose N to be a power of 2 (for example, 16 or 32) and you don't need floating point math.

The method I use is to maintain a variable (or register) to hold N*Vavg.
Sum = Sum + (Vsample - Vavg)
Vavg = Sum / N

This is exactly what I do in a PI controller (as in PID controller).
Hi,

Yes, I like to show the first version first:
Vavg=(Vavg*(N-1)+Vsample)/N

because it reveals the structure making it more apparent how and why the formula works by showing the separate parts.

The part:
Vavg*(N-1)

shows that we are starting with N-1 pieces of the average, working with equal pieces of the average but reducing it by 1 average sample.
For example, if we had 10 unequal samples and the average was 10, once we average each piece every piece has the same weight, which makes them identical. By subtracting one piece we only have 9 pieces remaining, and by adding the next real-life sample to those 9, we again have 10 samples but now it is a new average just because of that 1 new sample.
With N>2 we only multiply before adding and then dividing, so not that much different. It just changes the damping.
Another way of looking at it:
Subtract 1 average sample from the average, then add the new sample, then divide. The new average now includes the new sample.

Besides choosing a power of 2 for N we also need to keep all integers. Since the output of an ADC is an integer, that's not too hard to do. We just let things accumulate as integers and then divide at the end when we want an actual reading.

If we want second order processing then we just go to a more complicated expression like a difference equation similar to:
y[n+1]=(p1+p2)*y[n]−p1*p2*y[n−1]+K*x[n]
where p1, p2, and K are all defined from an original 2nd order RC filter for example.

I use this in computer utility programs also to smooth out the byte counts as operations take place on thousands of files. The disk reads vary tremendously so I have to low-pass-filter the file operation speeds, otherwise it just keeps jumping around and makes no sense to the user :)
 
Top