0
\$\begingroup\$

I am trying to make a UPS, which will need to pass safety regulations. I need to detect:

  • When the mains AC voltage is present
  • When the mains AC voltage is under/over voltage

I thought of using a capacitive power supply because it has high charge/discharge speed, unlike an SMPS which takes a long time to charge/discharge. The detection time I need for mains failing is less than 5 ms. The detect signal would drive an MCU input GPIO pin. That line would have a TVS and other over-voltage protection for the pin.

I know capacitive power supplies are inefficient, and can be lethal if they misbehave, but is there any other fast switching alternative?

This is how it looks in the picture. The 120 Ω resistor is used to pull the voltage to approximately 3..4 V so the MCU can read it. Would such a circuit work properly?

enter image description here

\$\endgroup\$
15
  • 1
    \$\begingroup\$ Good heavens! Are you planning on implementing a power supply in order to measure voltage? \$\endgroup\$
    – winny
    Commented Jun 15, 2023 at 10:35
  • 3
    \$\begingroup\$ 240VAC has a peak of 340V. C1 is massively under-rated \$\endgroup\$
    – Mukira
    Commented Jun 15, 2023 at 10:43
  • 2
    \$\begingroup\$ it needs to pass the safety regulations which regulations and which class? Does your safety requirements say anything about isolation from mains? Is GND lane user-accesible? \$\endgroup\$ Commented Jun 15, 2023 at 10:59
  • 1
    \$\begingroup\$ calm down guys, firstly, I already stated in my question that I need a circuit which could help to measure the 220V mains although I was asking for a standardized idea, so if you could better to give an idea. Secondly the capacitor is 275VAC rated(yellow X2 capacitors) so it would work just fine. Third, yes it would be Isolated for most part but the capacitor part is not so for that I am asking if something instead of SMPS(because it is relatively slow in charging/discharging) so I can make fully isolated. the device would be enclosed in black dyed(isolated dye) aluminum body. \$\endgroup\$ Commented Jun 15, 2023 at 11:24
  • 2
    \$\begingroup\$ One cycle of 50 Hz power is 20 ms, so detecting a power failure in under 5 ms is not possible. \$\endgroup\$ Commented Jun 15, 2023 at 15:32

2 Answers 2

2
\$\begingroup\$

What you want here isn't a power supply, but a measurement system.

If you only want to know whether there's AC present or not, that's trivially easy to do with an optocoupler or solid-state relay:

schematic

simulate this circuit – Schematic created using CircuitLab


If you want to compare the line voltage to a fixed value, that's also fairly simple; rectify it, divide it down, and use a comparator to compare the peaks to a voltage reference. Then pass the output of that comparator through an optocoupler to your output side.


If you want to measure the voltage with a little bit of precision, you can use a linear optocoupler like the aforementioned H11AA1, but you'll have to measure the current transfer ratio and calibrate that out in software.

The current transfer ratio, or CTR, is defined as \$\frac{I_{input}}{I_{output}}\$; if you give the optocoupler an input current of say 10 mA, and if the optocoupler has a CTR of say 50%, then the output current, provided there's enough voltage available that the output transistor isn't saturated, will be 5 mA.

The CTR varies quite a lot from device to device (I've never seen an optocoupler with tighter than a 1-to-2 spread, and they're more often on the order of 1-to-10), and it varies with temperature as well. In addition, the CTR changes (usually decreasing) as the device ages, so you'll need to compensate for that too, if you don't want old devices to constantly trip the undervoltage alarm.

Here's the most basic circuit for doing that. It's not too different from the circuit I show above, but this one is non-inverting:

schematic

simulate this circuit

R1 functions to convert the AC voltage to a current (we can assume the LED forward voltage is negligible in comparison to common mains voltages; if you're measuring low voltage AC you will want to compensate for it), then the optocoupler transfers that current over the isolation barrier and applies its CTR to it, then R2 converts that current back into a voltage, which can be measured with an ADC and then used to back-calculate what the AC input was:

$$\begin{align} V_{ADC} &= I_{output}·R2\\ V_{ADC} &= I_{input}·CTR·R2\\ V_{ADC} &= \frac{V_{AC} - V_f}{R1}·CTR·R2\\ \frac{R1}{R2}·V_{ADC} &= (V_{AC} - V_f)·CTR\\ \frac{1}{CTR}·\frac{R1}{R2}·V_{ADC} &= V_{AC} - V_f\\ \frac{1}{CTR}·\frac{R1}{R2}·V_{ADC} + V_f &= V_{AC}. \end{align}$$

Note that, for the H11AA1 shown here, positive and negative AC voltages will give the same response; you can't determine the AC polarity with this circuit.

You can trim the CTR in the analog domain by adding a resistor from base to emitter of the optocoupler (which lowers the effective CTR depending on the resistance--lower resistance means lower CTR), or in the digital domain by adjusting the constants in that calculation.

This is not an accurate measurement. The main source of error is the CTR, which (as previously mentioned) varies significantly from device to device, changes with temperature, and slowly degrades as the device ages. If your device is frequently turned on and off, and if it's safe to assume that the measured voltage will be within the expected range when the device is turned on, you can just automatically calibrate every time it's powered up and, instead of looking for specific numbers, look for specific deviations from the number it saw on power-up.

Obviously, all measurements here will need to take into consideration the fact that AC is AC. You don't want your device deciding it's lost AC power just because you measured the voltage a few milliseconds before the AC peak. Either synchronize your measurements to happen at the same time every half-cycle, or take measurements frequently enough that you can reconstruct the whole sine wave and look at that. It's easy to synchronize measurements here, because you can use the optocoupler's output itself as a timing reference; it will go low for a handful of milliseconds at each zero crossing.


If you want to measure it accurately, you'll need to set up a measurement circuit on the high-voltage side, feed that into an ADC, and connect the output of the ADC to your MCU through a digital isolator of some kind. This is the most straightforward way, though it may be a bit involved.

Another option would be to use a current transformer, if you happen to be more interested in the AC current than its voltage (or your application allows the two to be considered interchangeable, like if you have a known resistive load on the AC). This would avoid the need for a high-side power supply, ADC, and digital isolator.

\$\endgroup\$
11
  • 1
    \$\begingroup\$ I tried the optocoupler and it works fine, but the problem I can't measure if there is an over voltage or under voltage so how could I get it with an optocoupler? \$\endgroup\$ Commented Jun 15, 2023 at 19:35
  • 2
    \$\begingroup\$ @HazardousVoltage I described solutions for that as well; what do you not follow? Do you want to compare to one or two fixed voltages, or do you want to get an accurate measurement? \$\endgroup\$
    – Hearth
    Commented Jun 15, 2023 at 22:55
  • \$\begingroup\$ I get your point although I would be more interested in an approximate measurement using the optocoupler, but I don't get how it would measure when it is under/over voltage, what would be the mechanism? \$\endgroup\$ Commented Jun 16, 2023 at 11:20
  • \$\begingroup\$ Comparator and reference. Perhaps just use two optocouplers and transfer each signal separately. \$\endgroup\$
    – winny
    Commented Jun 16, 2023 at 16:03
  • 1
    \$\begingroup\$ @HazardousVoltage You should always use an optocoupler or some other form of isolation when sensing mains voltage. \$\endgroup\$
    – Hearth
    Commented Jun 16, 2023 at 19:26
-1
\$\begingroup\$

You could do it with an optocoupler replacing your bridge and add a pull up resistor to the output of the optocoupler then to the MCU. There are many approved for this application. Some will also be bidirectional on the input so you can get the positive and negative haves of the signal. This would be layout critical. I have also done it by using a small transformer in front of the optocoupler, that makes isolation much easier.

The mains voltage can be measured from the internal power supply. If it is a custom transformer you can add a winding to do both functions.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ I would be delighted if you could share the circuit with me, I thought of making it with an optocoupler but how to measure over/under voltage? \$\endgroup\$ Commented Jun 15, 2023 at 11:16

Not the answer you're looking for? Browse other questions tagged or ask your own question.