We have made an automation module with relay connecting the mains and the raspberry pi. We are maintaining the state for the switch on our server. We have the raspberry pi connected to the device over internet which can control the switch state. The problem we are facing is that when we turn on or turn off the switch from the physical switch on the main we are not able to get the state for the switch(whether on or off).... **is there any way to get the state from the physical switch to raspberry pi over relay*circuit connection*.

NOTE : I am a noob in electrical.

  • 1
    You could use a current transformer (CT) to determine if the circuit is powered. A relay however does not report its state, however If the relay has an unused pole that could be used to report its state. I suspect also your drawing is not correct, as a suspect the switch by passes the relay entirely.
    – Tyson
    Dec 13 '17 at 15:08
  • A nice self contained CT with additional electronics to provide easy dry contact integration to the PI is: alliedelec.com/dwyer-instruments-mcs-111050/70673425 There are less expensive CT's bare CT's however that leaves you with more circuit to design.
    – Tyson
    Dec 13 '17 at 15:24
  • Actually use a double pole switch , one side for your control and the other side for your Feedback loop (indicator). This is how it is done in the real world. (but that is the switch) to know if the relay is energized a similar setup is needed - one set of contacts for the control and the other for the feedback loop (indicator). In the real world these conections would go to a PLC input .
    – Ken
    Dec 13 '17 at 15:28
  • Your diagram - why run the hot lead through the switch to your relay contacts ? Why not run the hot lead direct to the relay contacts and utilize your switch to turn the relay on off by interrupting the control voltage of the relay (vcc). Then you only need to know if the relay is energized or not. You know if you commanded it remotely to be on or off, so if you did and it is not on - the switch is off or something is broken.
    – Ken
    Dec 14 '17 at 2:22
  • See my edited answer..
    – Ken
    Dec 14 '17 at 2:51

It's really easy. Given mains powered equipment, and wanting to detect if it is energized.

You get a common 5-volt wall-wart power supply, the kind you plug into the wall and it makes 5 volts. You plug that in, in a way that the wall-wart receives power if the equipment does. How you do that is up to you, follow the Electrical Code obviously.

The output of this 5VDC, you simply connect to a digital input of the Raspberry Pi. Either the digital input will sense 5 volts, or it will not. If it does not, then the equipment is not receiving power.

Add a high value pulldown resistor at the Pi so it definitely pulls the input down to zero when the supply is not running.

It's simple, bulletproof, and largely off the shelf.

  • thanks for the answer...can you please explain it with a block diagram...as I am noob in electricals Dec 14 '17 at 7:31
  • @NakashJawahire I don't know how to draw a block diagram for this. That seems like it would make it more complicated than it actually is. I did an edit to try to make it easier to understand. Dec 14 '17 at 8:10

Actually use a double pole switch , one side for your control and the other side for your Feedback loop (indicator) vcc to supply the feedback when turned on. This is how it is done in the real world. (but that is the switch) to know if the relay is energized a similar setup is needed - one set of contacts for the control and the other for the feedback loop (indicator) vcc to supply the feedback when energized. In the real automation world these feedback connections would go to a PLC input and the PLC status for these inputs would be read by the program and / or the HMI .

EDIT 12 13 2017

After reading various comments I thought I should add this here:

Your wiring should be different - the relay should be the only control for the device hot wire. Your switch should control the relay just as your Pi Controls the relay (you can AND this or OR the function) Pi on AND switch on = device on. Typically if someone is remotely operating a device that could be a hazard to others - it should be an AND and you should have safety feedbacks to prevent operation if a hazard is present.

A rewire of your design is below.

Automation 101


Automation 101 Part 2

  • Keep in mind you can’t mix wiring classes/voltages in the same junction box compartment.
    – Tyson
    Dec 14 '17 at 1:47
  • @Tyson - yes his switch is AC voltage, his relay however is low voltage control and inside a panel you can have mixed voltages - it is done all the time 24VDC, 120VAC . However the switch and how they are doing this is really not a correct approach.
    – Ken
    Dec 14 '17 at 2:16
  • The second pole of your double pole switch was where my concern lies.
    – Tyson
    Dec 14 '17 at 2:19
  • @Tyson - yes - but he should wire this differently. Normally a control switch is wired to control the relay and in his case he wants an 'and' condition apparently. So the switch needs to be wired between Pi and relay VCC control. The Hot should be wired to the relay contact so when energized the hot is made to the relay contact that is wired to the device. He would only need to know the most important thing is the relay energized or not although he would also be able to know switch position - by feeding back the relay vcc connection to the Pi if he has vcc at relay the switch is on.
    – Ken
    Dec 14 '17 at 2:29
  • 1
    @NakashJawahire best case: if you want the Pi to always know the state of the equipment then you would REMOVE all manual switches from the circuit and let only the Pi control the equipment. Then if you need manual local controls in the form of switches add back switches that turn on a GPIO on the Pi, then the Pi will turn the load on. 2nd best: if the manual switch can’t be eliminated use a CT to sense the flow of electricity. (See my comments on the question for CT information.)
    – Tyson
    Dec 14 '17 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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