Method for measuring the effective voltage value using MK

In this post we will talk about one of the options for measuring the effective value of the voltage and frequency of the network on an 8-bit PIC18 microcontroller. If desired, you can transfer the method to any other MK, up to all your favorite ARDUINOs (if they support the implementation of timer interrupts with a frequency of 5-10 kHz).
Also, this method allows you to measure the frequency of the mains voltage without the use of external additional tools, such as comparators. But, at the same time, one has to sacrifice either temporary MK resources or the accuracy of frequency measurement.
Why is it important to measure the effective value, and not any other, for example, the average straightened? Most of the "Chinese" electronic voltmeters measure the line voltage according to the average rectified value. The measurement procedure is as follows: during the period of the mains voltage, a selection is made of N values ​​of the voltage amplitude, the results are summed (unsigned), divided by N (averaged), after which the result is multiplied by a coefficient.
The specified coefficient determines the dependence of the effective value of the sinusoidal (!) Signal from the average rectified .
This measurement technique is simple, does not require many resources of the microcontroller (both temporary and memory resources). The main disadvantage of such a measurement technique is a large measurement error on non-sinusoidal signals.
As everyone knows, a change in the signal of the mains voltage obeys a sinusoidal law (due to the use of synchronous generators in power plants), with a signal change frequency of 50 Hz (60 Hz). However, in practice, due to the influence of external factors (mainly connecting high-power non-linear loads to the network), as well as the use of inverters with a quasi-sinusoidal output voltage (see figure), the voltage sine wave is either significantly distorted or replaced by rectangular pulses. In such cases, the above measurement method will give a very large error (for example, in quasi-sinusoidal inverters the output voltage measured by the "Chinese" voltmeter can be 180-200V, while the effective voltage will be 220V).
For example, the voltage at home Why is it important to measure the actual value of voltage (current)? Because it is the current (also called effective) voltage and current values ​​that determine the operation of the electrical system (roughly speaking, an electric heater generates heat in direct proportion to the current values ​​of the voltage and current of the network).
The rms value of the measured periodic quantity calculated by the formula
or after sampling obtain
Ie we need to make a selection of a number of values ​​for the period of the mains voltage, sum the values ​​of the squares of the points of the sample, divide by the number of points for the period (when determining the number of points of the sample, do not forget the Kotelnikov-Shannon theorem), and take the square root of the result.
It seems to be nothing complicated if not for:
1) Each period, the sampling points are recruited, which increases the measurement error;
2) In a real network, near zero voltage, both "zero shelves" and repeated transitions through zero voltage can occur, which will significantly introduce an error into the measurement.
We will struggle with the first point by measuring the sum of the squares of the sample points for each half period, after which we add up the nth sum of squares from the (n + 1) th and recline the (n-1) th.
We will struggle with the second item by introducing voltage insensitivity zones (we will introduce the zero-voltage voltage boundary on the positive and negative sides), usually 5-10 V in both directions, and frequency insensitivity zones (we will limit the allowable frequency of the voltage signal).

Thus, we obtain the calculated value of the effective value of the mains voltage for the period at each half-period of the mains voltage. The voltage frequency is calculated by the formula:
where Fd is the sampling frequency (for convenience and to increase the accuracy of measuring the frequency, it was chosen equal to 10 kHz (sampling period - 100 μs)).
Now we will consider the structural diagram of the measuring part (in a real circuit, filtering and protective elements should be added). Attention! In this measurement method, the galvanic isolation of the microcontroller from the network is not implemented. Galvanic isolation is implemented on the side of the digital interface for transmitting data from the microcontroller.
At the input there is a differential operational amplifier with a voltage divider, supported on half of the reference voltage (2.048 V). Since, to reduce the size, it is better to use low-power resistors, install them at least 3 pcs. equal in resistance - to increase the total breakdown voltage of the resistors. In this case, it is necessary to calculate the power loss at the maximum input voltage (P = U ^ 2 / R) - so as not to exceed the allowable power of the resistors. The shoulders of the differential amplifier are also made equal. Then, the voltage at point 1 is calculated by the formula: And the voltage at point 1 will look like:
Also, half of the reference voltage is supplied to one of the ADC channels. This allows you to continuously determine (for example, once per period) the position of the zero level of the measured voltage.
Those. we managed with a unipolar-powered op-amp, and our input signal at point 1 changes from 0 to Uop. This method gives fairly accurate results, compared, for example, with voltage rectification using diodes.
The calculation of the divider and the ADC coefficient is as follows: where A and B are ADC measurements (minus the measured signal zero value - AN1) for the current and previous half-periods; N1, N2 - the number of measurements for the current and previous half-periods; Nadс - ADC capacity; U'op is the reference voltage minus the non-linearity (insensitivity) zones of the operational amplifier (usually 0.6 V).
It is convenient to carry out the calculation of the divider by considering the signal constant, reduced to the amplitude of the sinusoidal, and not sinusoidal. Then the effective value of the signal is equal to the amplitude and equal to the value of each measurement.
For example, you need to calculate the divider to measure the maximum value of 420V AC: The
resistance Ra is selected in the range from 500 kOhm to 1500 kOhm. Based on the selected resistance Ra, Rb is calculated.
As a result, the algorithm for calculating the effective value of voltage and frequency will take the form:
In this case, part of the costly calculations (division, root extraction) can be transferred from the interrupt to the main program.

When calculating the effective values ​​on an 8-bit MK, it is advisable to use integer methods (using scale factors) without resorting to floating point calculations, and also simplify arithmetic operations (division, removal of the square root, etc.) if possible. This significantly saves MK resources.