# How do I check the state of a GPIO pin on startup?

**URL:** <https://discourse.nodered.org/t/how-do-i-check-the-state-of-a-gpio-pin-on-startup/52864>\
**Category:** General\
**Created:** [28 October 2021 07:02 UTC](https://discourse.nodered.org/t/how-do-i-check-the-state-of-a-gpio-pin-on-startup/52864 "2021-10-28T07:02:20Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mastiff](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@Mastiff](https://discourse.nodered.org/u/Mastiff)\
**Post date:** [28 October 2021 07:02 UTC](https://discourse.nodered.org/t/how-do-i-check-the-state-of-a-gpio-pin-on-startup/52864/1 "2021-10-28T07:02:20Z")

</div>

I have a pin connection on the Pi 4 (ground to pin 35 in node-red-node-pi-gpio) that will be driven by a relay, so when the relay closes media playback starts, and when it opens, playback stops. The problem is on startup. I want it to start playback if the relay is closed on startup, but not if the relay is open. How can I check for that pin being open? There is no "through" on that node, so I can fire an "On startup" request to it.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 October 2021 08:55 UTC](https://discourse.nodered.org/t/how-do-i-check-the-state-of-a-gpio-pin-on-startup/52864/2 "2021-10-28T08:55:12Z")

</div>

Are you sure that it does not send an initial message on restart?

---

<div class="post-metadata">

**Author:** ![Mastiff](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@Mastiff](https://discourse.nodered.org/u/Mastiff)\
**Post date:** [28 October 2021 09:07 UTC](https://discourse.nodered.org/t/how-do-i-check-the-state-of-a-gpio-pin-on-startup/52864/3 "2021-10-28T09:07:08Z")

</div>

Didn't seem like it, because I restarted the Pi with a jumper cable on, and playback didn't start.

---

<div class="post-metadata">

**Author:** ![gerry](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gerry/32/21604_2.png) [@gerry](https://discourse.nodered.org/u/gerry)\
**Post date:** [28 October 2021 12:00 UTC](https://discourse.nodered.org/t/how-do-i-check-the-state-of-a-gpio-pin-on-startup/52864/4 "2021-10-28T12:00:20Z")

</div>

Sounds like you need a pull up/pull down resistor in your circuit. If the relay is open the pin is grounded or if relay is closed then pin is high or vice-versa. Then check pin status on startup.

---

<div class="post-metadata">

**Author:** ![Mastiff](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@Mastiff](https://discourse.nodered.org/u/Mastiff)\
**Post date:** [28 October 2021 12:04 UTC](https://discourse.nodered.org/t/how-do-i-check-the-state-of-a-gpio-pin-on-startup/52864/5 "2021-10-28T12:04:03Z")

</div>

But the pin is up and down as it should be. It's the startup detection I don't know how to do. Sort of like "if pin 35 = 0 --\> start playback". The question was that I don't know how to check if it's up or not.

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [28 October 2021 13:37 UTC](https://discourse.nodered.org/t/how-do-i-check-the-state-of-a-gpio-pin-on-startup/52864/6 "2021-10-28T13:37:35Z")

</div>

> [@Mastiff](#):
>
> The question was that I don't know how to check if it's up or not.

Make sure that in the GPIO node you have selected this option  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/a/6ab4db5b1e621539dcdcc5c70ce09feb01bca909.png)  
That tells the node to send you a message on startup.

---

<div class="post-metadata">

**Author:** ![Mastiff](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@Mastiff](https://discourse.nodered.org/u/Mastiff)\
**Post date:** [28 October 2021 18:26 UTC](https://discourse.nodered.org/t/how-do-i-check-the-state-of-a-gpio-pin-on-startup/52864/7 "2021-10-28T18:26:59Z")

</div>

Thank you! That was it! 🙂 One slight problem, it seems like on startup this happens before the media program is ready. Is there a way to delay until the process mediacenter26 is running?

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [29 October 2021 08:06 UTC](https://discourse.nodered.org/t/how-do-i-check-the-state-of-a-gpio-pin-on-startup/52864/8 "2021-10-29T08:06:40Z")

</div>

Since you only want to delay the first message, you could use node-red-contrib-queue-gate, set it to startup in the Queueing state. and when you know that the media program is ready then send an Open message to the gate to allow the initial message through.

---

<div class="post-metadata">

**Author:** ![Mastiff](https://avatars.discourse-cdn.com/v4/letter/m/ee7513/32.png) [@Mastiff](https://discourse.nodered.org/u/Mastiff)\
**Post date:** [2 November 2021 07:29 UTC](https://discourse.nodered.org/t/how-do-i-check-the-state-of-a-gpio-pin-on-startup/52864/9 "2021-11-02T07:29:45Z")

</div>

Thanks, I'll look into that!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 January 2022 07:30 UTC](https://discourse.nodered.org/t/how-do-i-check-the-state-of-a-gpio-pin-on-startup/52864/10 "2022-01-01T07:30:09Z")

</div>

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