Detect 5v signal in same pin where it is detecting AC

i use this circuit to detect AC 220v signal in esp8266 module

so there are 2 PINS i_1 (phase) and N(neutral) and if i connect AC there then i recieve GPIO_HIGH on esp32.

this is ok till here.

what i want is a way that i can use same pin with 5v DC so a way that i can connect 5v+ to N or i_1 pin and ill recieve GPIO_high on esp32

so i can use either AC or DC5v on same pins (N, i_1)

To clarify: you want to modify the circuit so you can monitor AC or DC @5V and 220VAC from the same input?

Yes. So i can connect ac220v or 5v dc and i can get input on esp. 5v will have same gnd as device so only 5vdc+ wire can connect

Your 5 volts must be able to light ONE of the leds in the opto. So, limit the current on the 5 volt line to what is necessary.

1 Like

the question is how can i connect to terminal i_1 or terminal N the 5v . what should i do in circuit so i can connect 5v but also if i connect ac it will work like it works now

Do not connect both at the same time!!!! So, test it!!!!

1 Like

not at the same time. but sometimes AC sometimes 5vdc

With R10 (120k) and R12 (10k) there won't be enough forward current to turn one of U1's LEDs/IREDs on.

Further to what @runaway_pancake said, even if there was enough current to light the LED, their is no ground. Connect 5VDC to L1 and there is no ground for the LED; or connect 5VDC to N and signal to L1 is blocked by D7.

What's the practical purpose of that circuit?

I'm not quite following you here:

  1. Do you wanna switch between reading 220V or 5V, or
  2. read with one pin the state of NONE / 220 / 5 / 220+5 ?


let say i have one 5.08mm2P terminal . so i can choose to connect AC there or connect a wire from 5vdc there. and i will get HIGH signal on esp in booth cases

so 2 different scenarios and i can either connect AC or DC to get signal on esp

That's quite a challenge (for me at least). I'll let the more talented take care of this.

I don't consider that as practical purpose.
Get two terminals, two circuits, twenty cents.

1 Like

i can use 2P2T slide switch also. but question is if this can be done wihtout using switch.

its not that i will do this but its a question so me and others can see if this can be done

Of course it can be done. You can even have fast and reliable internet connection through mains power lines (PLC).

I agree.


Anyways, for what it is worth.
Digging down in my archive of forfeited projects.


If I recall correctly it was not producable because HCPL-0300-500E was not in stock. I'll have to twist my brain hard to figure out how and why it might work. Intended to be used to check if all phases are present in 3-phase 400 VAC system (apx. 565V when rectified).

And, do not attempt this without understanding the risks and how to proper handle them!

For instance by air gapping at the correct places.

For further research have a look here:

I was thinking of something along the lines of Olin Lathrop's suggestion and using an analog measurement. Perhaps use the exact circuit that @arpa123 has, but bias the output of the optocoupler and measure the voltage. Basically my idea is to turn the 220V to 5V span into a signal level that can be measured to determine whether or not an input is present.

I think it's feasible but would require some investigation.

Apparently you want the input to be '1' if AC voltage is present OR when 5V is present.
I suppose there's no input free, because that would be the simplest solution.

If you are not experienced with handling mains power, it’s best to avoid attempting this. Instead, simply use a wall wart that provides the voltage you need. Wall warts are readily available with both AC and DC output options, and they also provide galvanic isolation from the mains, making them much safer to use.

By taking this approach, you ensure safety and significantly reduce the risk of accidents—allowing you to enjoy many more sunrises. If you choose to work with a transformer-based solution, make sure to select the correct transformer for your specific voltage requirements, as this approach works with all AC voltages when matched properly.

2 Likes