I know, but that can be handled in software, by adding a DC offset to the input. Or even, skip that and just measure positive half waves... multiply by 2.
Which eliminates one source of error and introduces another. I can't say which error would be larger.
So, a half-wave rectifier? Still introduces an error.
There is no rectification if an AC voltage is applied to an input pin using an input current limiting resistor, except for the action of the clamping diode. But that is outside the range of ADC measurement.
If the datasheet says it's OK to do so (I haven't checked). Otherwise, you become an Unpaid Test Pilot.
I wouldn't. But a Schottky on the input will make it safe because the clamping voltage will always be less than the internal clamp.
Actually, two Schottky's, one to ground and one to Vdd.
depending on the source of the signal you can add a DC offset to the AC signal using resistors, e.g. sct-013-030-to-measure-watt and sct-013-100a-50ma
Yes, that too, as I mentioned. Then the software has to find the zero voltage point... actually you could calibrate that when no AC is applied. The single polarity version has double the precision, though... in theory. You have to guard carefully against any offsets, or calibrate them away.
With a full wave rectifier, you'll have the voltage drop of 2 diode's to consider. I would suggest trying something similar to this.
The voltage divider (R1=470K;R2=5K) isn't ideal with 6VAC. If we consider the 6VAC as being an average value, then the peak-peak voltage is 18.85V. Let's use 10V for the rectified peak, then a voltage divider using (R1=10K, R2=10K) should give a nice ½ wave rectified AC signal just under 5V peak. This is assuming you're using the ½ wave rectifier circuit in the link (with Shockley Schottky diode).
Shockley was the BJT researcher at Bell Labs. Schottky, I'm not sure... ![]()
Famous last words, and a good learning experience.
The OpenEnergyMonitor code is correct, so you should study it carefully.
Firstly, your choice of data types is a bit strange
Voltage is an integer so why should V_square and accum be floating point?
it isnt: its an adc reading so max is 1023. However you WOULD need to limit the number of samples you take; why not Serial.println(counts) to see what you are actually using?
When testing it makes sense to look at the raw analog readings.
yes, because your voltage divider is taking the 10V pk and giving you 0.1Vpk out so "Voltage"pk = 20; you need to change the divider ratio to suit the adc reference - so eg 6k8/3k3
1024
Your strategy - reading, squaring, accumulating, averaging, take square root is of course correct.
OK, you have a point there. But... does OP want to measure RMS voltage, or does he want to calculate RMS voltage of a sine wave based on the max voltage???
Anyway, the sqrt(sum(V^2)/nummeas) method is correct and should return RMS (RMS stands for Root Mean Square...) (apart from the errors caused by the 0.6 V per diode in the diode bridge).
As long as the signal is a sine wave, your approach will work as well.
This calibration factor will be voltage dependent....
As the voltage over the diodes is relatively large at low measurement voltages...
Below 0.6 V the measured voltage will be 0V. No calibration factor will bring that back to 0.6V.
Schottky diodes can reduce this issue.
Other options are a diode inside an opamp circuit.
I remember there are special units that compare the heat developed in a resistor in the measurement circuit to the heat developed in resistor in a reference circuit. When both are equal, the voltage of the reference circuit is proportional to the RMS voltage of the measurement circuit. Big advantage is that this way the high voltage circuit can be fully isolated from the measurement circuit (so no transformer needed).
I admire your opinion which is a matter of great concern for the production house that is going to design, manufacture and sale the industry grade volt meters. I think that the current project is a hobbyist one.
If you just want to know the the RMS voltage of an AC supply then, depending on your exact requirements, you can simplify as follows:
Transformer as specified, with a potential divider to the input. Rectify for half wave. Find the positive going zero crossing point and wait 10ms, then measure the peak OR take multiple measurements and find the highest, which is the peak. RMS is then 0.707 of the measurement. You don't even need to do the multiplication by 0.707, just calibrate with a trim pot against a multimeter.
The assumptions here are:
- Mains is a sine wave (more or less) so you can assume that the peak is the square root of 2 times RMS.
- The negative peak is equal to (or close enough to enough not to matter) and opposite to the positive peak, so you can assume they are the same and do away with measuring the negative peak.
If all that's needed is an RMS reading, then put a capacitor on the output of the rectifier, wait for the voltage across the capacitor to stabilize, and measure that ONE voltage. Compensate for the diode voltage drop(s) by adding either the (near-zero current) diode forward voltage drop (if using a single diode half-wave rectifier, or two-diodes with center-tapped transformer full wave rectifier), or 2X the diode forward voltage drop (if using a full-wave bridge rectifier). The RMS value will be simply that voltage times 0.707. No critical timing necessary.
1. The OP uses 230V/6V Transformer and full wave rectifier. You (@RayLivingston) added filter capacitor.
2. So,
Vdc = 2Vm/3.14
==> Vdc = 21.41*6/3.14 = 5.38 V
3. So, according to above quote --
Vrms = 5.38*0.707 = 3.80 V
But, the Vrms of the line voltage is: 230V.
4. I would say that you need a calibration factor (the gain factor) in Step-3 to get 230V.
Vrms = 3.80* G
==> Vrms = 3.80230/3.80
==> 3.8060.52
5. The codes are similar to post #12.
void setup()
{
Serial.begin(9600);
}
void loop()
{
float vrms = (5 * analogRead(A1)) / 1023 * 60.52; //
Serial.println((byte)vrms);
delay(1000);
}
Its a poor assumption these days, lots of SMPS and non-tungsten lighting connected to the circuit push a lot of harmonic energy back into the mains. You can get an estimate of this if you have an oscilloscope with a math function including integration - the capacitive mains pick-up is the mains voltage differentiated, so just enable integration and you'll see the mains voltage by remote sensing!
The white trace is the integrated pickup and you can see the characteristic flat-top shape due to all the power supplies pulling more power at that point in the waveform and flattening it - so the sqrt(2) approximation is directly affected.
'Scope probes look mainly resistive below about 1kHz, so the capacitive pickup is intrinsically differentiated by the probe at mains frequencies.
@perry yes too many assumptions.
We've also assumed the wave has a constant frequency of 50Hz.
Its also been said that the rectification introduces an error - and its temperature dependent.
I'd suggest this:

Clamp the negative half cycle with a schottky diode.
Use a suitable divider chain to nearly match the expected Vpk to the ADC range.
Set a threshold on the ADC reading to allow for ADC offset eror, and identify "zero" readings.
Sample the input at regular intervals and if its a positive value process the sample readings until it drops below the threshold.
Then calculate Vrms of the "6V" output.
You would still need to calibrate for the (essentially off-load) transformer to get the "mains" rms voltage.

