Selecting right capacitor filter values, while measuring ac mains - Page 1
Forum section: electronics / beginners

author: Vindhyachal.takniki
author_rank: Frequent Contributor
timestamp: August 19, 2017, 07:30:39 am
content: 1. I am measuring ac mains directly though microcontroller. Microcontroller is power by isolation. So what I did, I put a divider of 330K & 2.2K between line & neutral.Shorted neutral to microcontroller ground.Ac to measure is 220vac/50hz, mains vary from 170Vac to 300vac & freq could be 50 or 60hz from mains supply.2. On this divider I was thinking of placing a capacitor for filtering also, for noises. How to calculate value of this capacitor:a) make a low pass filter, block freq above 60Hzb) make a band pass filter, pass only 50/60Hz component onlyc) make a software based filter3. With a divider of 330K & 2.2K, MCU will see a equivalent thevenin by 2.2K parallel to 330K i.e 2.18KIf I look at this:http://www.electronics-tutorials.ws/filter/filter_2.htmlWhat fc should I select, but keeping in error margins of all resistors & capacitors?or should I select 60Hz ?If I select 60Hz, then fc = 1/2*pi*R*CC = 1.21uF, nearest I have is 1uF.With 1uF fc will be: 72HzAre these calculations correct?4. Similarly for band pass, values can be found.http://www.electronics-tutorials.ws/filter/filter_4.html5. Or should I use software filter? I am sampling using:a) Sample every 500us after for 50 readings i.e 25ms. Find the highest point, this is peak.b) find 25 peaks like this & store it in array.c) sort this array, reject lowest 5 & highest 5. Take avarge of mid 15 valuesd) this is peak, divide by root2, to get rms.What cut off freq should be selected keeping in mind all variation of resistors & caps assuming 1% resistors & X7R caps.6. Currently I am able to measure values which are near to its calibrated values. 2 point calibration is done.Just curious to know how to calculate these values.

author: Ian.M
author_rank: Super Contributor
timestamp: August 19, 2017, 08:41:33 am
content: That doesn't sound good at all. Resistors that can withstand the peak voltage of 220V AC mains are fairly expensive, and ordinary resistors should never have more than the maximum voltage specified in the datasheet across them (typically 200V for axial 1/4W metal film resistors), so a single 330K resistor is totally unsuitable unless it has a >600V voltage rating (to allow for spikes). Exceeding its voltage rating frequently results in a resistor going open circuit, and it could flash over and destroy your MCU.Also your circuit makes absolutely no provision for the negative half of each AC cycle, and at the negative peak will pass 2mA through the lower internal protection diode of your MCU's ADC pin, which is almost certainly out of spec for normal operation and is likely to cause the MCU to misbehave due to current injection into the substrate of its die.(e.g. as little as 0.35mA protection diode current can cause a PIC16 oscillator frequency to shift by 50%)I therefore suggest splitting your 330K resistor into three series 110K ones and using two resistors for the lower arm of the divider, one to MCU 0V and the other to MCU Vdd to bias the ADC input to Vdd/2 at the mains zero crossing points. That should give you an approx. 2V peak to peak swing for 220V in, which is a comfortable range for a 3.3V MCU.If you are using a 5V MCU, you'll probably want to increase the lower arm resistors to use more of the ADC input range. If you are using an external reference for the ADC, take the lower arm bias resistor to the reference output, not to Vdd.You definitely need low pass filtering - 1uF to 0V at the ADC input would give you a -3dB cutoff frequency of 144Hz with the resistors I suggested. If you need to calculate the true RMS you'll probably want a higher cutoff frequency to let more harmonics through. 0.1uF and a cutoff frequency of 1.44KHz might be more appropriate.There will be significant software changes as well. If you want to keep your current algorithm you need to subtract the 0V in ADC reading from all readings then double the result. Its probably a better idea (if you've got enough processing power) to implement a true RMS algorithm that first subtracts the long-term average voltage to remove the DC offset.Alternatively, use a 1N4007 diode feeding a 400V 0.68uF capacitor to get a DC voltage equal to the positive half cycle peak, and use your existing divider and software design, except replace the 330K resistor with 3x 110K in series.

author: Kleinstein
author_rank: Super Contributor
timestamp: August 20, 2017, 04:36:32 pm
content: Final filtering is probably better done in software. So the analog filtering would be for anti aliasing and thus depends on the ADCs sampling rate and not so much on the line frequency. Some low pass filtering is highly recommended - but likely more with a cross over frequency in the low kHz range. I don't see a need for a hardware band-pass, DC removal is much better in software. RMS calculation close to the definition is doable with many µCs - it can be surprisingly good, especially for the relatively small range expected for mains voltage. For calculation it is a good idea to use an interval that is an multiply of the period. So something like 100 ms to get this for 50 Hz and 60 Hz.Many µC can tolerate more than 2.2 K input impedance - thus larger resistor values (e.g. 3 times 220 K) are possibly.

author: Zero999
author_rank: Super Contributor
timestamp: August 20, 2017, 06:18:20 pm
content: Quote from: Ian.M on August 19, 2017, 08:41:33 amI wouldn't say that. Most metal film resistors, in the standard through hole 6.5mm package, are fine up to 350V but that doesn't give enough margin, so two in series should be used.http://uk.rs-online.com/web/p/through-hole-fixed-resistors/6833165/http://docs-europe.electrocomponents.com/webdocs/0d9a/0900766b80d9a016.pdfMy concern is the fact that there's no isolation from the mains. The original poster is using an isolated supply, then throwing that away by connecting the mains neutral to the negative rail. In a country with polarised mains connectors, it's not as bad as in areas where it's non-polar, but there's still a risk the neutral can become live, especially momentarily as the mains plug is connected or disconnected, because there's no guarantee the neutral will make first.A small mains transformer can be used to measure the mains voltage. It will need to be calibrated, as the unloaded secondary voltage is always higher than the rated value.As far as filtering is concerned. I say don't bother, if all that's required is the peak value, as the higher frequencies will be tiny, compared to the fundamental and won't make much difference to the result. Using a transformer will also help to remove and RF interference.