I'm currently designing a 'smart' light switch, controllable from Microcontroller and physical switch. I am looking for a low power solution to convert signal so that the Microcontroller can detect if there is mains voltage at the input of the lamp (or arbitrary device). The device will also be embedded in the walls so required small form factor, as well as low power loss / heat due to limited ventilation. Thank you in advance for the advice, I am quite new.

Goal: Detect (from microcontroller) if the output from the relay has AC mains / connected to live wire. Should detect regardless of the lamp being connected or not (no load/open circuit). This is to notify user if the device being powered is safe to handle / exchange or not. It doesn't need to know the exact voltage, just the presence. Delay of few 10s to 100s ms is acceptable.

Additional info: The Microcontroller may be powered from mains or a 24v dc supply line, currently deciding.

Size: aiming to fit 3 sets of relays and detection components (as well as a power supply for the Microcontroller and the Microcontroller) into 8x4 cm.

Power: aiming for the detection solution to take on order of 10s of mW.

Cost: aiming for the detection solution to take ~<$3

From what I've read there are a few approaches.

  • Voltage Transformer into rectifier and capacitor (low pass filter)

Pros: Isolate high and low voltage

Cons: Voltage Transformer may be large and costly.

Unsure: Rectifier may dissipate a lot of power.

  • Optocoupler

Pros: Isolate high and low voltage. Low space

Unsure: Optocoupler may require consume lots of power.

  • Difference Op Amp

Pros: Low space

Cons: No isolation

Unsure: Op Amp power consumption

  • Hall Sensor

Pros: Low space, low cost

Unsure: Hall sensor power consumption. Operation under no load (no current flow -> no magnetic field?)


Simplified Circuit

  • \$\begingroup\$ Are you building this for yourself so you don't mind it's unsafe, or do you need to sell this so it needs to pass any safety and other tests required to sell an electrical device? If the former, are you legally allowed to work with fixed mains voltage installations in your own house, and can you do that as per your local electrical codes and regulations? \$\endgroup\$
    – Justme
    Jul 5, 2023 at 20:06
  • \$\begingroup\$ Detect mains: try inductive pickup. Something like this maybe. No-contact, just run the "antenna" near the hot wire. Should be able to make it draw very little current. \$\endgroup\$
    – rdtsc
    Jul 5, 2023 at 20:19
  • \$\begingroup\$ @Justme I am currently building this for myself. But I also want it to be safe, so do you have recommendations regarding the safety tests/certifications for the device? \$\endgroup\$
    – pedpx2
    Jul 6, 2023 at 2:48
  • \$\begingroup\$ @rdtsc, this looks like a possibility. Is this setup reliable, also is it possible for 3 sets to have close proximity placement? Also can you explain how the signal is coupled to the detector (as I understand, for no load condition -> open circuit, no current flows, thus no magnetic field for detection) ? \$\endgroup\$
    – pedpx2
    Jul 6, 2023 at 3:11
  • \$\begingroup\$ Do you have access to Neutral? Many embedded wall switches don't. \$\endgroup\$
    – Jens
    Jul 18, 2023 at 19:48

1 Answer 1


This circuit provides an isolated HV-AC sensor with high impedance.

The MCU generates short positive pulses of some µs width at OUT. This is the green track in the pictures. 100..200 ns after the rising edge of OUT the input IN is read.

HighZ-MCU-Input During the negative half wave or without any input signal the MOSFET T1 is not conducting. The yellow track shows the voltage at the secondary side of the transformer. It initially follows the positive edge of OUT and is falling because the current in the winding rises. The MCU reads a "high" 100 ns after the rising edge.


At a large percentage of the positive half wave T1 is conducting and shorts the transformer winding. This reduces the inductance of the secondary winding and the voltage there does not follow the rising edge. Only 50 mV peak can be seen here, the MCU will read a "low"


To detect the presence of an AC mains voltage the MCU should take such measurements with a sampling rate of 100 Hz or more. The width of the short pulses is not important, one µs is good enough. If a "high" is read for a longer interval than the AC period duration, there is no AC input signal.

With an input signal the MCU will read a "low" for around 30% of the AC period duration.

R05 and D2 protect the MCU ports.

The transformer isolation capability must be rated for mains usage. There are very small components available.

  • \$\begingroup\$ Thanks for this brilliant design. Sorry for the late follow up. May I ask about the ratings for the MOSFET and the type of transformer? I have searched around on digikey and was not sure which type of transformer to use. \$\endgroup\$
    – pedpx2
    Feb 24 at 2:40
  • \$\begingroup\$ There is no special rating for the MOSFET, just use a small one. This circuit was tested with the double coil SRF0905-102Y from Bourns, but this has no reinforced isolation rating. You may try PH9085.034NLT from Pulse Electronics, 78601/8JC from Murata or LAN transformers like SM453229-381N7Y from Bourns, 300 µH will work as well, I assume. \$\endgroup\$
    – Jens
    Feb 24 at 4:02
  • \$\begingroup\$ Thank you for the suggestions. May I ask how to determine the safety operations of these transformers? as we are essentially ‘shorting’ the transformer (I’m guessing this means non-standard operation of the transformer), then where can we find information in regards to the maximum voltage and/or current passing through primary with secondary shorted \$\endgroup\$
    – pedpx2
    Feb 26 at 4:00
  • \$\begingroup\$ The energy in the transformer comes from the MCU port via R04 and is in the range of 3-5 mA only, the maximum voltage is below 5 V as you can see in the images. No problem for a transformer. \$\endgroup\$
    – Jens
    Feb 26 at 15:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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