# How I did a line-interactive UPS (Part 2)

In the second part of our cycle, we continue the discussion about measuring AC voltage, as well as about measuring the output current of a load. I ask all interested under cat.

Part 1
Part 2
Part 3

### Measuring the rms voltage or current

About the measurement of the actual value has been repeatedly written in various sources. I personally liked the following most:

Budget option TrueRMS measurement
Method for measuring the effective voltage value using MK
Briefly and clearly all the formulas are
calculated Calculation of the average and rms current / voltage values

In short, the essence of all such complex calculations is that the power supply itself the voltage can be different from the ideally sinusoidal shape, in addition, in the case of a UPS with a modified sinusoid at the output when working from the inverter, the waveform at the output will also only be remotely apominat sinus. Therefore, if we simplify the measurement and consider the average straightened value, the results will be very different from the real ones.

Here is an example of what happens at the UPS input and output (taken from here ):

The algorithm for calculating the effective voltage in my case does not have any uniqueness. With a frequency of 1121 Hz (for digitizing voltage with a frequency of both 50 and 60 Hz), a timer interrupt is triggered, the ADC is started and measurements are made on three channels (input voltage, output voltage, output current). After accumulation of 90 measurements, they are calculated and actual values ​​are calculated.

The sum of the squares of the values ​​is calculated directly in the interrupt, and in the main program cycle, when using floating-point arithmetic, averaged (over another 20 points) effective values ​​are calculated.

All operations are performed on an 8-bit microcontroller PIC18F26K22. Someone may immediately ask the question: why not STM32, they say it is more powerful, cheaper, etc. I will answer right away. The STM32 controller is good, but somehow it didn’t take root, although it was used in some projects.
Most of our tasks do not require huge computing resources, so 8-bit is more than enough here. In addition, PIC18 has a huge number of developments and its own service software, and this is very important, because significantly accelerates new developments, allowing you not to be distracted by the study of unknown peripherals. And it always takes the most time.
PIC18 also has many advantages. This built-in calibrated generator, minimal external wiring, voltage range from 2.5 to 5V, good built-in peripherals, powerful outputs with currents up to 25 mA, etc. MK operates at a frequency of up to 64 MHz.

### Output current measurement

The current consumed by the load is measured by an integrated sensor ACS712ELCTR-30A-T (at 30A) from Allegro. The sensor generates an analog signal proportional to the flowing current, taking into account the sign. If the current is positive, then the signal will be more than 2 V, if negative, then less than 2 V. The signal generated by the sensor is digitized by MK and used to control the load. Now the manufacturer points to the site and in those. documentation that these sensors are undesirable to use in new developments, and instead recommends a more modern model from the ACS723 series. But for now, buying ACS712 models in Russia from suppliers is much simpler, and even cheaper.

The sensor is extremely convenient in that it allows direct connection to the ADC MK, while it requires only one 5V power supply, and also provides galvanic isolation (in fact, the sensor is non-contact, operating on the Hall effect). The last point is important, because current measurement is highly desirable in a phase conductor, so that the entire neutral of the UPS is the so-called "through", i.e. essentially representing one single conductor. This sensor can be easily used in the gap of any conductor, which simplifies the entire measurement circuit.

However, one interesting point is associated with this sensor. According to the documentation, it can withstand one current pulse of 100A for a duration of 100 ms. Further, irreversible damage to the chip may occur. Naturally, a circuit breaker is installed in the UPS in the input circuit. But its response time is just commensurate with the duration of this pulse. Here is an example of the time-current characteristic of a type C machine:

To have some margin of safety in the event of a short circuit on the UPS circuit board, an additional shunt was made according to the recommendations of the sensor manufacturer itself ( link ).

The point here is quite simple. The resistance of the internal shunt of the ACS712 is 1.2 mOhm. It is proposed on the printed circuit board in the form of a conductor of the desired shape to make the same second shunt, thus increasing the current limit by half (up to 200A), which will allow the circuit breaker to operate much faster.

The dimensions of such a current shunt on a printed circuit board are shown below:

This is how it looks live:

I want to note that this sensor is intended only for measuring the current consumption of the load, in order to assess the load of the UPS and automatically turn it off when the specified limit is exceeded. For example, a 600 W UPS can output a maximum of 3A. In the event that the load starts to consume 4A or more for some time (for example, about 2 seconds), then we simply disconnect it. The circuit breaker protects against hard short circuit during operation from the network. But in the operating mode of the inverter, protection is organized electronically, but using other sensors. This will be discussed a little later when considering the operation of the inverter itself.