Read an RPi *output* pin state?

Good morning...

I have an output GPIO in use, but can't seem to poll it for the current state. Users can turn it on and off through the UI.
I don't want it re-initialized because I may do a dozen deploys while working on other parts of the flow, and don't want it shutting off each time.
After a deployment without initializing, the GPIO node just shows "OK", as opposed to True/False.



Is there a way to automatically poll it for what the last user selection was?
Ideally, I'd want to read the pin state, so if it is someday set to re-init, we know that, rather than getting status from a user-set variable, etc.
Search results make it look like the answer is no, but I'm not sure they were all asking the same thing...

No, you can't read an output pin.

Are you doing a full deploy every time? If you click the dropdown on the deploy button then you can select Modified Nodes and then it will only redeploy nodes that have changed, so the output pin will stay as it was.

If you want to know what the last user selection was then you will need to remember that. I do that by using Retained topics in MQTT. It can be done using flow or global context though.

1 Like

This site has poor connectivity, so I usually just deploy changes...but I just checked and it was doing a full deploy every time. This is a replacement I just installed yesterday, must have missed that.

It does leave the GPIO node alone on deploys now if I set it to reinitialize, so I'll leave it that way.

I may set temperature and humidity conditions in addition to the UI control, so I will have to add a change node to set a variable in front of the GPIO node. Not really a true status, but if it's the only option then it's the only option.

Sucks that you can't perform a read on the pin. I thought the Pi could behave like a microprocessor, where you can still read pins configured as outputs to get their current state.

The Pi is not only behaving like a microprocessor, it is a microprocessor.

It isn't the only option. As I said, you can use MQTT, which has the big potential advantage that it would automatically set the output, UI, etc following a reboot.

I should have said a simpler microprocessor. My experience with microprocessor's is that you can do a pin "read" even if set as an output. IE, I have an arduino that sets a pin on/off, then if on, it holds a different pin opposite. Could I do that with a variable? Sure, but a read gets the actual current state with no chance of another process activating it, and that process forgetting to update the variable.

Ah well, it is what it is.

MQTT isn't really a good option for the environment.

Do you have spare GPIO pins? You could presumably connect the output to an input pin to read the status.

My first reaction was "That's simple and brilliant!"
But I'm not sure, the pin is triggering a micro-relay that triggers a 240v coil on a contactor. I would need to find the resistance that doesn't drain the voltage enough to drop the relay, while still being enough to trigger the input pin.
But that gets me going. For this specific use-case, I could have the micro-relay trigger a 5v ice-cube relay with contacts for the 240v load separate contacts to ground an input pin on the pi.
It's a lot of stuff, but it gets me the positive confirmation of the switching state.


I see that you need to ensure that monitoring it doesn't interfere with it's primary relay switching role.

I imagined the relay output switching a separate 3.3V to gpio circuit with a tiny transistor or an optoisolator.
I don't know offhand if the current to switch a transistor is more or less than the current grounded through a gpio.

As you can probably tell, I do not know or understand electronics!

I switch a few 220VAC and a lot of 110VAC and through trial and error I have found SSR's to be my component of choice. They work down to 3.3 volts and are isolated from the outputs and will take up to 50 amps.

Also to read the outputs I do a relay in the circuit and read those pins open or closed to determine state. One more component but electrically a little cleaner in terms of isolation. Don't forget pullup/pull-down resistors

I don't see the problem. Just join the pins together and configure one as output and one as input. The input pin will not interfere with anything else you connect to it. The outputs are active pull down, float high, so you should make sure that any relay, opto-isolator or similar that you connect is activated by a low pin state, not a high state.

the way @Colin suggests will work but it will not give you a true indication if the load gets switched. My way won't give you a true indication either as the relay could pull in and the contactor fail but at least you will know the load switched. Depending on how important it is to know exactly you could just tell the pin to switch and display that on a switchboard somewhere, or actually use an opto-isolator across the load and measure that. Depends on your need and the hoops you want to jump through.

That is true, but that isn't what @Rgrove asked for in the first post.

Thanks for the suggestions, I'll hopefully update with confirmed success or issues after the next site visit.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.