Detecting Mains Voltage

Hello,
As a part of a little home automation project that I am trying to do I need to detect if 220VAC is presence in order to know if the lights are on or off.
I have started to look for information about this and found out about optocouplers and more specifically about the PC817 chip.
So my question is this, is the wiring is as simple as the one shown in my attachment, considering that I will read the INPUT pin from the optocoupler as an INPUT_PULLUP on the Arduino… So is it that simple? If not what do I need to do and how do I wire it up?
Thx!

That optoisolator runs at 1.2 V and nominal 20 mA. Maximum reverse voltage is 6V.

At a MINIMUM you will need a protection diode so that when the AC line voltage reverses the voltage across the LED doesn’t exceed 6V. You will also need a current limiting resistor of AT LEAST 6240 Ohms to keep from exceeding the 50 mA absolute maximum current rating. Better would be 11k to limit the RMS current to 20 mA.

Note that your output will probably be pulsing at the AC line frequency.

The "easiest" way to detect AC voltage is with a relay that has a 220VAC coil. Relays are a bit bulky and probably a bit more expensive, but they are electrically isolated it's a "good old reliable" way of doing it.

As a part of a little home automation project that I am trying to do I need to detect if 220VAC is presence in order to know if the lights are on or off.

There's nothing wrong with what you're trying to do, but most home automation systems don't have any feedback. If you want the lights on, you send a "lights on" command and it doesn't matter if they are already on or not.

Your 11k resistor would disspipate more than 5 watts with 240vac across it ...

If you don't need great speed a 5v wallwart would supply a safe signal.

Allan.

Or use a chip like HCPL-3700V
https://www.digikey.com/product-detail/en/fairchild-on-semiconductor/HCPL3700V/HCPL3700V-ND/1057446

You’ll need to change the 2 series resistors, they limit current flow thru the chip.
AC_loss.jpg