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?
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")
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.
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.
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.
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.
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.
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:
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:
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:
Use a relay with an extra pole to provide a status signal, or
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.
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.