I found a few good answers and guides about how to control a relay module from the pi, however, I want to find a way to check its state/status from the pi, is there a way to determine if the relay is currently on or off so I can update the app interface accordingly ?

  • 1
    use a current sensor to detect if the relay has current flowing through the contacts – jsotola May 31 '18 at 3:57

I don't think I fully understand your question. Relays can either be on or off - depending on if the output to the relay module is high or low. So if you're controlling the relay from code, just keep a variable that tracks the state of the relay. for example, in python:

relayOn = False

def turnRelayOn():
    relayOn = True
    #turn on the relay
def turnRelayOff():
    relayOn = False
    #turn off the relay

def printRelayState():
    if relayOn:
        print("The relay is currently ON")
        print("The relay is currently OFF")


You could use a ready-made current sensor like this ready made module(or something similar) to measure the current between the output of the relay and your load. That current sensor uses a I2C interface, so it should be easy to connect to with the pi.

  • When you have the relayOn = true it's because you set it as a result of an action such as button pressed or an automation script, but what if you set is as true and there was a power outage, is there an option to check what is currently the status of the relay, can I check if the current is flowing through it ? – Yossi May 30 '18 at 22:44
  • I added a second part with a link to a current sensor you could use @Yossi. – scitronboy May 31 '18 at 21:37
  • You are genius, that's exactly what I'm looking for. – Yossi May 31 '18 at 22:21

This is a great question because the actual relay state may differ from its nominal state. Feedback is critical, since actuators are physical objects that die over time. It is well worth the effort to add a way to sense the actual actuator state. For example, if the relay engages a pump, add a vibration sensor or sound sensor to determine if "everything is working as expected." Such a sensor will detect pump failure as well as relay failure. For robust system design, add a sensor for each actuator. Open loop systems (i.e., blind actuators) do work but are maddening when they fail (e.g., "We lost the entire crop because we thought the relay was on"). Simple actuators (e.g. light switch) can be exceptions because the operator can usually deduce their non-operation and repair as necessary.

  • 1
    That's is exactly my point and you describe it better, thank you. – Yossi May 30 '18 at 22:54
  • 1
    So yes, depending on what the relay is driving, there are options as far as sensing what the output status is. If you provide what the relays are driving, I am sure a good option could be given – Chad G May 30 '18 at 22:57
  • 1
    @ChadG An example is a light switch, if I use a light sensor, during the day, the sensor will show that the light in on while it actually off, I need to be able to find it a way to know if the relay is currently flowing current. – Yossi May 31 '18 at 1:43
  • And if your light sensor is shaded from daylight it might actually work. :D This is the fun of engineering design. This discussion is very helpful and illustrates the nuances of designing with real world devices. – OyaMist May 31 '18 at 1:52
  • There are ways, but they are never perfect read up on the "two general problem" current sensor would be a good bet, shows the light is on while still providing isolation – Chad G May 31 '18 at 2:40

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.