What is the goal ?
You have a low power device (typically battery powered, or on UPS) that need to detect if the main power is lost. How do you do that efficiently ?
What is the difficulty ?
When dealing with alternating current and a microcontroller, you’re out of the blink led tutorial. If the voltage on the line is higher than than 50V, it becomes hazardous to handle, you MUST TAKE ALL POSSIBLE CARE ELSE YOU’LL BE KILLED.
You don’t want to spend a Watt for detecting if power is there, since each Watt, 24/7 costs you more than 1€ per year.
If you need to monitor more than a single line, then the most efficient solution is preferred.
Some basic solution involves using a resistor divider followed by a diode (or better, a diode bridge) like this:
Even if you set up your divider R2/R1 so that the voltage on R1 is a lot below your microcontroller’s allowed input pin voltage, you’ll burn it.
In the former case, the voltage on NODE1 is measured w.r.t the AC’s Ground, in the later case, it’s measured w.r.t the Digital Ground.
These kind of circuits have numerous disadvantages.
They consumes permanent power in the resistors, and the diodes. For example, if your microcontroller is 5V tolerant, in the 230V case, you’d need VR1 higher than two diode threshold voltage plus the minimum to detect the output on the microcontroller (let’s say 2.5V). VR1 in that case will have to be at least 5V (2.5V for the microcontroller, plus 1.2V per diode), so R1/(R1+R2) must be set to 0.022. The current that flows through the diodes must be high enough to ensure the diode will switch, and low enough compared to the current that goes through the resistor. Let’s say you want 4mA in the bridge center, you’ll need at least 16mA in the divider. So, the current in R2 will be 16mA, which gives R2 = 14kΩ, R1 = 314Ω.
The power dissipated in the resistor will be 3W in R2, and 200mW in the bridge.
The resistor R1 is there to ensure that, when no load is present in the bridge, the voltage on the diodes is limited to what the diode could support.
Non immune to line variation
The differential voltage between AC power line can become as high as 400V in Europe.
With the above computation, if it either happens, then the bridge will transmit this over voltage to its load, your microcontroller. This will likely kill it.
Non immune to component death.
If a diode is killed, and shorts itself (PN junction typically shorts themselves when burnt), then your microcontroller will be killed in the next instant.
If a resistor is killed (typically R2), then it’s safe.
Second obvious solution
You could use a AC/AC Transformer to avoid the loss in the divider and have the voltage you want on the bridge. It’s much more safe, transformers are very reliable.
However, they have a terrible power loss, even more when the load is very light.
Typically efficiency for such transformer is between 20% to 80% (it’s below zero when no load is connected to the secondary). So if your transformer is rated 230V/5V 1A, it can consume between 1W (for the most efficient) to 4W for the less efficient.
That simple USB transformer will cost you more than 16€ in 4 years just for detecting if the power was loss.
Given that power loss is a rare event, you could be tempted to use a 230V relay to detect such event (230V on the relay commuter, and 5V on the other side). A 230V relay consumes continuous current (typically 5mA on 230V, so close to 1.2W).
The best solution
A component like VOS628A-3T or PS2505 are AC optoisolator that support AC input on their light emitting diode, and a classical silicon based phototransistor on their “secondary” circuit.
The design will look like this:
Depending on the Optocoupler chosen, you need to adjust the inductance Z0 (likely by changing C0) in order to have a current in the diode has close as possible to the minimum specified in the datasheet. Typically, for this component the recommended current in the diodes is between 5mA to 10mA. Resolving the equation gives C0 around ~160nF for 10mA, and ~80nF for 5mA. C0 must be rated for 400V.
The consumption for such circuit is very low (in the tens of mW range), yet provides the best insulation you could expect when dealing with AC power line.
On the other side of the Optoisolator, you have a classical NPN transistor which can be directly connected to your microcontroller with a current limiting resistor. Depending on the microcontroller, the internal pull up for digital input will be enough to power the transistor and detect its status (saturated or not).