# How to use push button to toggle Led in raspberry pi?

**URL:** <https://discourse.nodered.org/t/how-to-use-push-button-to-toggle-led-in-raspberry-pi/51444>\
**Category:** General\
**Created:** [23 September 2021 15:00 UTC](https://discourse.nodered.org/t/how-to-use-push-button-to-toggle-led-in-raspberry-pi/51444 "2021-09-23T15:00:33Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![JasonV](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JasonV](https://discourse.nodered.org/u/JasonV)\
**Post date:** [23 September 2021 15:00 UTC](https://discourse.nodered.org/t/how-to-use-push-button-to-toggle-led-in-raspberry-pi/51444/1 "2021-09-23T15:00:33Z")

</div>

Hello my name is Jason,

I have a question about how to set a push button as on/off function.  
I already started to construct a push button which functions quite nicely as long as I push and release the button. It then changes from 1 to 0. But my intention that I very much would like to do is to turn on and off the LED light by PRESSING the button instead of releasing.

Kind regards,

Jason

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [23 September 2021 20:41 UTC](https://discourse.nodered.org/t/how-to-use-push-button-to-toggle-led-in-raspberry-pi/51444/2 "2021-09-23T20:41:26Z")

</div>

Is it a physical button connected to a GPIO or a Node-RED dashboard button?

Can you paste a picture of your flow?

---

<div class="post-metadata">

**Author:** ![Gunner](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gunner/32/43582_2.png) [@Gunner](https://discourse.nodered.org/u/Gunner)\
**Post date:** [23 September 2021 21:21 UTC](https://discourse.nodered.org/t/how-to-use-push-button-to-toggle-led-in-raspberry-pi/51444/3 "2021-09-23T21:21:09Z")

</div>

> [@JasonV](#):
>
> But my intention that I very much would like to do is to turn on and off the LED light by PRESSING the button instead of releasing.

Near as I can tell, Node-Red's `ui_button` **only**"triggers" on release - as I discovered when working on this topic...

> [@Can ui-button respond to double-click](https://discourse.nodered.org/t/can-ui-button-respond-to-double-click/50783/6):
>
> Actually, it doesn't trigger on the "press", rather it triggers on the "release"... strange that I never noted that before. It clearly detects the press visually, so why not trigger on both?

However there are some solutions - such as creating your own button ...

> [@Press button state](https://discourse.nodered.org/t/press-button-state/50817/2):
>
> Hello Ali .. you could use the ui\_template node and take advantage of javascripts mousedown and mouseup eventlisteners on the button. Example Flow [{"id":"81fc920712f2501c","type":"ui\_template","z":"4895ea10b4ee9ead","group":"e68f0343d6bbfce5","name":"","order":0,"width":0,"height":0,"format":"\<button id=\"myBtn\"\>Click Me\</button\>\n\n\<script\>\n(function(scope) {\n\nlet myBtn = document.getElementById('myBtn')\n\nmyBtn.addEventListener('mousedown', e =\> {\nscope.send({payload: 1})\n});\n\nmyB…

---

<div class="post-metadata">

**Author:** ![edje11](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edje11/32/572_2.png) [@edje11](https://discourse.nodered.org/u/edje11)\
**Post date:** [24 September 2021 09:09 UTC](https://discourse.nodered.org/t/how-to-use-push-button-to-toggle-led-in-raspberry-pi/51444/4 "2021-09-24T09:09:24Z")

</div>

Hi Jason,

Have a look at the [node-red-contrib-toggle](https://flows.nodered.org/node/node-red-contrib-toggle)  
It will do the job.

---

<div class="post-metadata">

**Author:** ![JasonV](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JasonV](https://discourse.nodered.org/u/JasonV)\
**Post date:** [24 September 2021 11:15 UTC](https://discourse.nodered.org/t/how-to-use-push-button-to-toggle-led-in-raspberry-pi/51444/5 "2021-09-24T11:15:37Z")

</div>

Dear Cymplecy

Yes it is a physical button connected to a GPIO of a raspberry pi( Ground -\> GPIO 26) and a LED to the pins ( ground -\> GIO 16)

When i push the button the state from the button change to 0 en the led turns off and when i release the button the state change back to 1

( when I press the push button )

 ![Schermafbeelding 2021-09-24 om 13.11.13](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/d/2d5264964053a4488ccfaea3202914cdf8ec1623.png)

( when I release the push button )

 ![Schermafbeelding 2021-09-24 om 13.11.04](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/f/9ff4e7699d8a246edf7426c420ada1753c5b0c01.png)

I would like that the LED stay on when the push button is released and goes back out when I press again

---

<div class="post-metadata">

**Author:** ![eddee54455](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eddee54455/32/43350_2.png) [@eddee54455](https://discourse.nodered.org/u/eddee54455)\
**Post date:** [24 September 2021 11:37 UTC](https://discourse.nodered.org/t/how-to-use-push-button-to-toggle-led-in-raspberry-pi/51444/6 "2021-09-24T11:37:58Z")

</div>

Try this:

![screenshot-192.168.0.118_1880-2021.09.24-13_35_07](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/c/dc198ffa39f43cf07b9f403dbc78c22fa94d2a20.png)

and then make sure that the topic of this:

![screenshot-discourse.nodered.org-2021.09.24-13_36_35](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/1/4/1480f94a8d8e61462d323dfbb1412b293d9bf010.png)

is set to: toggle

Ed

---

<div class="post-metadata">

**Author:** ![JasonV](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JasonV](https://discourse.nodered.org/u/JasonV)\
**Post date:** [24 September 2021 11:58 UTC](https://discourse.nodered.org/t/how-to-use-push-button-to-toggle-led-in-raspberry-pi/51444/7 "2021-09-24T11:58:05Z")

</div>

I tried but then the led doesn't work anymore it just stays on

Settings Input

 ![Schermafbeelding 2021-09-24 om 13.53.21](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/3/231795fffa7b99cbf05f5cecd51c7887f9a60a34.png)

Toggle node

 ![Schermafbeelding 2021-09-24 om 13.53.13](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/f/9f7b0cb5efb6ad6bb08647e93716a9da1708c036.png)

---

<div class="post-metadata">

**Author:** ![eddee54455](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/eddee54455/32/43350_2.png) [@eddee54455](https://discourse.nodered.org/u/eddee54455)\
**Post date:** [24 September 2021 12:53 UTC](https://discourse.nodered.org/t/how-to-use-push-button-to-toggle-led-in-raspberry-pi/51444/8 "2021-09-24T12:53:46Z")

</div>

Add this before the Toggle node:

![screenshot-192.168.0.118_1880-2021.09.24-14_52_00](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/3/33ecbd8057039c6e2acbd72a4360c92425c15d1d.png)

Edit it to look like this:

 ![screenshot-192.168.0.118_1880-2021.09.24-14_50_53](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/c/0c8316a3c7a056c6a60ea26e02680ebb5ccdfc1f.png)

Ed

---

<div class="post-metadata">

**Author:** ![JasonV](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@JasonV](https://discourse.nodered.org/u/JasonV)\
**Post date:** [24 September 2021 13:30 UTC](https://discourse.nodered.org/t/how-to-use-push-button-to-toggle-led-in-raspberry-pi/51444/9 "2021-09-24T13:30:40Z")

</div>

Thx !!!

this did the job

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [24 September 2021 13:43 UTC](https://discourse.nodered.org/t/how-to-use-push-button-to-toggle-led-in-raspberry-pi/51444/10 "2021-09-24T13:43:43Z")

</div>

Or a basic change node 🙂  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/9/f9198d6a1cc969576d9a15b43b0d4bd44b417978.png)

No need for any JS 🙂

---

<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:** [8 October 2021 13:43 UTC](https://discourse.nodered.org/t/how-to-use-push-button-to-toggle-led-in-raspberry-pi/51444/11 "2021-10-08T13:43:59Z")

</div>

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