I'm working on a Home Automation Project. I can turn on-off ESP32 pin from Alexa and Google Assistant Successfully now.

What I need help is with a way to read if the AC supply is ON/OFF on the Arduino pin. Or if possible to save pins I would want to read it on the same pin which id defined as Digital output to control the relay.

Below attached image of what I have in mind.

  1. Normal switch turns On/off supply to load without any requirement of WiFi.
  2. Load is also connected to Relay NO, which is controlled by Arduino Pin D13.

What I want is instead of just reading the status of the D13 pin, I want to read the status of the Mechanical Switch which directly provide to Load.

So I'll actual status of the Load whether it's turned on by Switch or Relay with interrupting directly connected switch from load to supply.

enter image description here

Edited: As others suggested it is not possible to use the Same Pin to control the relay and read AC load status. So is there a way to read the AC supply through the GPIO pin of ESP32 maintaining galvanic isolation, (opposite working of Relay)?

  • if your manual switch has a second contact, you can read that
    – Juraj
    Oct 4, 2022 at 15:37
  • Every time I want to answer I stop myself because it's dangerous to connect to the high voltage 110 or 220 VAC side. I like @edgar's answer. If your load is resistive (old incandescent lamps) you might look at old X10 schematics to see what they did. If new (LED lamps) then do what edgar said.
    – st2000
    Oct 5, 2022 at 12:39

2 Answers 2


If you want to know the state of the load, I suggest to put the mechanical switch on the low-voltage side of the relay:


simulate this circuit – Schematic created using CircuitLab

The voltage divider R1/R2 is meant to lower the voltage at the transistor's collector (up to one diode threshold above Vcc) to a value suitable for the Arduino digital input.

Note that you still need two separate inputs for controlling and for sensing the state.

  • That was my concern, I don't want to use the switch to turn on the load indirectly, as changing the switch in the future will be easier than the relays also any fault in the circuit won't affect the manual system. But if I consider your case why don't I use the Switch in the same place as the Control Pin? Oct 5, 2022 at 13:36
  • 1
    @JustdoinGodswork: If you connect the switch and the control pin together, you will likely short the pin, and possibly destroy its output stage. Oct 5, 2022 at 13:53

What you want to do is not possible, you'll need an extra pin. The reason is that the relay requires a lot of current (works like a very strong pulldown resistor). So, whatever (high impedance) sense signal you apply to the relay, it will just be "consumed" by the relay.

When reading the state of the switch, i.e. the voltage across it, galvanic isolation is absolutely mandatory (mains voltage can be deadly). This can be achieved with an opto coupler and some lines of code or additional hardware - as it's AC voltage, you can't simply read a steady voltage, so you'd either have to sample the AC signal or incorporate some kind of circuitry (software is simpler and cheaper).

Another issue with your circuit is that you must not drive the relay directly with the GPIO. The relay sinks way to much current, it'll fry the GPIO. Use a transistor to switch it.

  • My bad for the cheap circuit, I couldn't find an online tool to make it. I'll use the proper Opto coupler driver circuit for the relay in actual hardware. Then I'll try to find an increased GPIO pin controller as my priority is not to interfere with the manual system. Is there any device that is opposite to the relay? Eg. If AC passes through the coil I'd be able to read it at a GPIO pin? Oct 5, 2022 at 13:43

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.