2

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 verify/measure 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
0

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")
    else:
        print("The relay is currently OFF")

EDIT:

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
3

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
1

Answer:

Yes. There are several ways this can be done. The method used will depend upon the characteristics of the load connected to the relay's contacts.

Background:

As @OyaMist has stated, "the actual relay state may differ from its nominal state". From a distance, when the relay state must be known, we are reduced to making a simple assumption; i.e. "The input is energized, and so I assume the output is likewise energized." Pretty weak stuff these assumptions... never try to put a man on the moon with that approach!

Relays are useful devices, and been adapted into numerous applications over time. Even the invention of the transistor in 1947, and the solid state revolution it engendered has not displaced relays. In principle they are simple devices (ref schematic below) consisting of only two components: a coil (electromagnet) used to control the mechanical position of contacts to be opened or closed.

schematic

simulate this circuit – Schematic created using CircuitLab

There are a variety of contact arrangements possible and available. SPST (shown above), SPDT, DPDT, etc. We will make use of one of these other contact arrangements to provide a status indicator for the state of the relay.

Some Examples:

An additional pole in the relay can always be used to provide status. Simply put, an additional set of contacts may be used to provide the status feedback. In the schematic below, a SPST contact arrangement was all that was required to switch a single load ON or OFF. We have chosen a DPST relay for this so that we can use the "extra" contacts to signal the state of the relay:

schematic

simulate this circuit

In this example, we route the RPi's 3V3 source through the "extra" contacts as a signal to a properly configured GPIO pin that the contacts are both "energized", or in the closed position. Even if we cannot see the incandescent lamp illuminated, this signal will serve to tell us that at least power is applied to it.

Since contact bounce is a fact of life, a small RC filter was added to the schematic. Just choose R & C values that give a time constant appropriate to the "bounce specifications" of the relay, and the switching frequency. This RC filter is redundant if you are willing to write additional code to effectively "ignore" the input until it settles.

An additional pole will always provide for determination of the relay state regardless of the characteristics of the load. But in some cases, it may be more convenient to sample the voltage applied to the load by the primary contacts:

schematic

simulate this circuit

In this case we have sampled the load voltage using a Zener diode to reduce it to the level required for RPi GPIO input (3.3 V). A voltage divider could also be used to sample the load voltage, but the Zener will provide a stable voltage largely independent of fluctuations in the supply voltage.

In summary then, there are two approaches:

  1. Use a relay with an extra pole to provide a status signal, or

  2. Sample the load voltage.

Sampling the load voltage will typically require an interface be designed that provides a signal of 3.3 V or Ground to meet the RPi's GPIO specifications. The "extra pole" approach may avoid the necessity of designing & building circuitry to sample the load voltage.

Caveats:

Having said all of this, we must also consider the fact that there remains a possibility the status signal we generate is in error. In fact, it's true that after this effort we are still required to make an assumption, albeit an assumption that is (hopefully) more likely true than an assumption based only on the input to the relay.

For the majority of Raspberry Pi applications, it may be that the approaches outlined above will be sufficient. However, if your application requires greater assurance that the system is operating properly, you should know where the limits are, and have some idea how to increase that assurance.

Recognition of these limits has led to development of specialized relays that try to give greater assurance that "all is well". For example, force-guided relay contacts, were developed to address concerns that all of the contact armatures in a single relay may not change state when the coil was energized. There are also specialized classes of relays developed to address safety and reliability concerns, for example: 1, 2, 3.

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.