The circuit below provides a simple method to detect a mains votlage input with an input range of 90 – 240V AC:
The output gives a low signal for around 90% of the time when a mains input is present, so you need to connect it to an edge detect input of a microcontroller (e.g. an irq or capture input), or add some circuitry to introduce a delay before the output can return high. The selected opto is a high safety type for over kill and a simpler model could be used where local regulations permit. Using 2off 56k resistors on the input gives a low but good enough LED drive voltage range. The opto isolator has to be AC otherwise an external diode would be needed to protect against LED reverse voltage.
Output with 90VAC input:
Output with 240VAC input:
The 2W resistors are overkill and are not at all hot to touch (after turning power off!) at 240VAC input.