# First time using buttons

**URL:** <https://discourse.nodered.org/t/first-time-using-buttons/78974>\
**Category:** General\
**Created:** [5 June 2023 19:50 UTC](https://discourse.nodered.org/t/first-time-using-buttons/78974 "2023-06-05T19:50:47Z")\
**Posts on this page:** 13\
**Page:** 2

<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:** [6 June 2023 16:14 UTC](https://discourse.nodered.org/t/first-time-using-buttons/78974/22 "2023-06-06T16:14:07Z")

</div>

As @jbudd and myself said, there is no need for resistors until you want to connect to a physical button. You just need to tell the node what you want it to do.

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/f/ffe1a77d489e46dd943d81fabb20b5bafd0e0ba3.png)

Note the instruction to tell it to use an (internal) pullup.

---

<div class="post-metadata">

**Author:** ![Sean-McG](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/sean-mcg/32/54677_2.png) [@Sean-McG](https://discourse.nodered.org/u/Sean-McG)\
**Post date:** [6 June 2023 16:23 UTC](https://discourse.nodered.org/t/first-time-using-buttons/78974/23 "2023-06-06T16:23:25Z")

</div>

> [@spawnreaper](#):
>
> should I just use a different pin, this one has RxD behind it, maybe thats the reason?

That sounds like you are using the TTL Receive pin, probably not the best choice.

Try and stick to using the pins with a green dot in this table jbudd linked [https://pinout.xyz/](https://pinout.xyz/)

Configure the node as shown by Colin and you should find the random activation stops.

---

<div class="post-metadata">

**Author:** ![spawnreaper](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/spawnreaper/32/33501_2.png) [@spawnreaper](https://discourse.nodered.org/u/spawnreaper)\
**Post date:** [6 June 2023 17:29 UTC](https://discourse.nodered.org/t/first-time-using-buttons/78974/24 "2023-06-06T17:29:16Z")

</div>

Okey, it seems that my button one is stable now.  
and button 3,4 and 5 are also stable.

Last challenge left is button 2.  
What I need it to check is if any relay 2,3 or 4 is on, if any is on then turn all of them off.  
If all are off, then turn all on.  
Only I dont know how to make the relay state part of a check. I mean it feels like a if this then that but I dont know how to accomplish that or with what node.

---

<div class="post-metadata">

**Author:** ![spawnreaper](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/spawnreaper/32/33501_2.png) [@spawnreaper](https://discourse.nodered.org/u/spawnreaper)\
**Post date:** [6 June 2023 17:31 UTC](https://discourse.nodered.org/t/first-time-using-buttons/78974/25 "2023-06-06T17:31:40Z")

</div>

> [@Sean-McG](#):
>
> Try and stick to using the pins with a green dot in this table jbudd linked [https://pinout.xyz/](https://pinout.xyz/)

ill use that layout. Just need to figure out how to get the correct resistors before I hook up any physical buttons. My default electronic shop is out of business, so I have to find a new one.

---

<div class="post-metadata">

**Author:** ![spawnreaper](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/spawnreaper/32/33501_2.png) [@spawnreaper](https://discourse.nodered.org/u/spawnreaper)\
**Post date:** [9 June 2023 21:17 UTC](https://discourse.nodered.org/t/first-time-using-buttons/78974/26 "2023-06-09T21:17:51Z")

</div>

okey, so it seems the relay is not accepting the command thats coming out of the function.  
{"relay2":1,"relay3":1,"relay4":1}  
maybe there is a way to split this output op so it can directed as a 1 or 0 value to the relays.  
I also notice that the function is not checking or ignoring the relay state. every time I press button two is either {"relay2":1,"relay3":1,"relay4":1} or {"relay2":0,"relay3":0,"relay4":0}

---

<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:** [10 June 2023 09:17 UTC](https://discourse.nodered.org/t/first-time-using-buttons/78974/27 "2023-06-10T09:17:56Z")

</div>

Change the function so that it has four outputs, one for each gpio pin.

---

<div class="post-metadata">

**Author:** ![spawnreaper](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/spawnreaper/32/33501_2.png) [@spawnreaper](https://discourse.nodered.org/u/spawnreaper)\
**Post date:** [15 June 2023 21:02 UTC](https://discourse.nodered.org/t/first-time-using-buttons/78974/28 "2023-06-15T21:02:25Z")

</div>

I can give it 3 outputs no problem, but Im still stuck on the function node that needs to check the status, let alone send different commands to different outputs.  
I am asking anyone in my network, but havent found anyone that can help me with the code.  
Its the last button I need to get working, the rest work.

---

<div class="post-metadata">

**Author:** ![spawnreaper](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/spawnreaper/32/33501_2.png) [@spawnreaper](https://discourse.nodered.org/u/spawnreaper)\
**Post date:** [15 June 2023 21:05 UTC](https://discourse.nodered.org/t/first-time-using-buttons/78974/29 "2023-06-15T21:05:15Z")

</div>

> [@dynamicdave](#):
>
> You might find this useful as a reference.
> 
> Here's a [link](http://www.resources-area.co.uk/node-red-flows/having_fun_with_buttons.pdf) to a worksheet I created for my IoT students. It shows how to create various types of buttons.
> 
> [Having fun with buttons](http://www.resources-area.co.uk/node-red-flows/having_fun_with_buttons.pdf)

Ive read though the document multiple times. I cant figure out how to get that last button work. Every code just flips all to on or off but no checking is taking place. Ive added the set var value in the normal button functions, but I have no way to check if the value is actually being set and I dont know how to summen that value in the function 2.

---

<div class="post-metadata">

**Author:** ![Frida](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/frida/32/68250_2.png) [@Frida](https://discourse.nodered.org/u/Frida)\
**Post date:** [16 June 2023 08:35 UTC](https://discourse.nodered.org/t/first-time-using-buttons/78974/30 "2023-06-16T08:35:50Z")

</div>

I am in the process of making something similar, so here is my offer.

 ![pic02](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/2/42cb58b22b9a8efa7a600bbfeacc4b71429f50ae.png)

```auto
[{"id":"7d251d22c0032334","type":"tab","label":"Flow 1","disabled":false,"info":"","env":[]},{"id":"b24a0d3014627bee","type":"junction","z":"7d251d22c0032334","x":660,"y":160,"wires":[["914b35805c25e1ed","10f1beba0c2fc18e"]]},{"id":"85d29c2682d59b78","type":"junction","z":"7d251d22c0032334","x":660,"y":260,"wires":[["37d367a30a521bba","59db5799d8ed456a"]]},{"id":"a1c2df78836eb44d","type":"junction","z":"7d251d22c0032334","x":660,"y":360,"wires":[["b23f88a8d5b95d85","6cf9d9782da9d32f"]]},{"id":"86ebd135637bc771","type":"junction","z":"7d251d22c0032334","x":380,"y":440,"wires":[["c6712ee6195f2f35"]]},{"id":"ea5f10c9de9df017","type":"junction","z":"7d251d22c0032334","x":400,"y":200,"wires":[["c6712ee6195f2f35"]]},{"id":"c6712ee6195f2f35","type":"function","z":"7d251d22c0032334","name":"function","func":"let msg1 = {}\nlet msg2 = {}\nlet msg3 = {}\n\nlet Relay2 = flow.get('Relay2') ?? 0\nlet Relay3 = flow.get('Relay3') ?? 0\nlet Relay4 = flow.get('Relay4') ?? 0\n\nconst invar = msg.payload\nnode.status(invar)\n\nswitch (invar) {\n\n case 'relay2':\n Relay2 = Relay2 ^ 1\n break\n\n case 'relay3':\n Relay3 = Relay3 ^ 1\n break\n\n case 'relay4':\n Relay4 = Relay4 ^ 1\n break\n\n case 'toggle':\n if(Relay2 || Relay3 || Relay4) {\n Relay2 = 0; Relay3 = 0; Relay4 = 0\n } else {\n Relay2 = 1; Relay3 = 1; Relay4 = 1\n }\n break\n\n case 'Reset':\n Relay2 = 0; Relay3 = 0; Relay4 = 0\n break\n\n}\n\nflow.set('Relay2', Relay2)\nflow.set('Relay3', Relay3)\nflow.set('Relay4', Relay4)\n\nmsg1.payload = Relay2\nmsg2.payload = Relay3\nmsg3.payload = Relay4\n\nreturn [msg1, msg2, msg3]\n","outputs":3,"noerr":0,"initialize":"","finalize":"","libs":[],"x":520,"y":260,"wires":[["b24a0d3014627bee"],["85d29c2682d59b78"],["a1c2df78836eb44d"]],"info":"// nrlint function-eslint:off\n"},{"id":"10f1beba0c2fc18e","type":"debug","z":"7d251d22c0032334","name":"Relay2","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":770,"y":140,"wires":[]},{"id":"59db5799d8ed456a","type":"debug","z":"7d251d22c0032334","name":"Relay3","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":770,"y":240,"wires":[]},{"id":"6cf9d9782da9d32f","type":"debug","z":"7d251d22c0032334","name":"Relay4","active":true,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":770,"y":340,"wires":[]},{"id":"5aab6e1218f93d3c","type":"rpi-gpio in","z":"7d251d22c0032334","name":"Button 1gp(17)","pin":"17","intype":"down","debounce":"25","read":false,"bcm":true,"x":120,"y":80,"wires":[["8d95399650789b0e"]]},{"id":"8d95399650789b0e","type":"trigger","z":"7d251d22c0032334","name":"400ms","op1":"1","op2":"0","op1type":"str","op2type":"str","duration":"400","extend":false,"overrideDelay":false,"units":"ms","reset":"","bytopic":"all","topic":"topic","outputs":1,"x":370,"y":60,"wires":[["e07a05aa38b1db0c","7bb6b7867873c612","86d150aa0531a3e5"]]},{"id":"b2d412737bc9a911","type":"inject","z":"7d251d22c0032334","name":"Button 1","props":[{"p":"payload"},{"p":"Repeat","v":"none","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"flow","x":120,"y":40,"wires":[["8d95399650789b0e"]]},{"id":"e07a05aa38b1db0c","type":"rpi-gpio out","z":"7d251d22c0032334","name":"Relay1(22)","pin":"22","set":false,"level":"0","freq":"","out":"out","bcm":true,"x":690,"y":80,"wires":[]},{"id":"7bb6b7867873c612","type":"debug","z":"7d251d22c0032334","name":"debug 263","active":false,"tosidebar":false,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":660,"y":40,"wires":[]},{"id":"86d150aa0531a3e5","type":"function","z":"7d251d22c0032334","name":"All ON","func":"if(msg.payload === '1') {\n flow.set('Relay2', 1)\n flow.set('Relay3', 1)\n flow.set('Relay4', 1)\n}\n\nreturn msg\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":160,"wires":[["ea5f10c9de9df017"]]},{"id":"914b35805c25e1ed","type":"rpi-gpio out","z":"7d251d22c0032334","name":"Relay2(16)","pin":"16","set":"","level":"0","freq":"","out":"out","bcm":true,"x":830,"y":180,"wires":[]},{"id":"37d367a30a521bba","type":"rpi-gpio out","z":"7d251d22c0032334","name":"Relay3(18)","pin":"18","set":"","level":"0","freq":"","out":"out","bcm":true,"x":830,"y":280,"wires":[]},{"id":"b23f88a8d5b95d85","type":"rpi-gpio out","z":"7d251d22c0032334","name":"Relay4(23)","pin":"23","set":"","level":"0","freq":"","out":"out","bcm":true,"x":830,"y":380,"wires":[]},{"id":"981e6c19b83125a7","type":"inject","z":"7d251d22c0032334","name":"Reset","props":[{"p":"payload"}],"repeat":"","crontab":"","once":true,"onceDelay":0.1,"topic":"","payload":"Reset","payloadType":"str","x":290,"y":200,"wires":[["ea5f10c9de9df017"]]},{"id":"f784959592a7c514","type":"rpi-gpio in","z":"7d251d22c0032334","name":"Button 3gp(13)","pin":"13","intype":"down","debounce":"25","read":false,"bcm":true,"x":120,"y":360,"wires":[["82025b33bf7e2aee"]]},{"id":"86ea5712e87f1646","type":"rpi-gpio in","z":"7d251d22c0032334","name":"Button 2gp(6)","pin":"6","intype":"down","debounce":"25","read":false,"bcm":true,"x":110,"y":260,"wires":[["3b13d16d9cf2dd2e"]]},{"id":"fa0f13b192fa7381","type":"rpi-gpio in","z":"7d251d22c0032334","name":"Button 4gp(19)","pin":"19","intype":"down","debounce":"25","read":false,"bcm":true,"x":120,"y":460,"wires":[["a5b323ebee1906b2"]]},{"id":"3b13d16d9cf2dd2e","type":"function","z":"7d251d22c0032334","name":"On or OFF","func":"if(msg.payload === 1) {\n msg.payload = 'toggle'\n}\n\nreturn msg\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":310,"y":240,"wires":[["c6712ee6195f2f35"]]},{"id":"50d4ea272bec4f90","type":"inject","z":"7d251d22c0032334","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":110,"y":220,"wires":[["3b13d16d9cf2dd2e"]]},{"id":"82025b33bf7e2aee","type":"function","z":"7d251d22c0032334","name":"Relay2","func":"if(msg.payload === 1) {\n msg.payload = 'relay2'\n}\n\nreturn msg\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":340,"wires":[["c6712ee6195f2f35"]]},{"id":"a5b323ebee1906b2","type":"function","z":"7d251d22c0032334","name":"Relay3","func":"if(msg.payload === 1) {\n msg.payload = 'relay3'\n}\n\nreturn msg\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":440,"wires":[["86ebd135637bc771"]]},{"id":"85ba8216522c19a3","type":"inject","z":"7d251d22c0032334","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":110,"y":420,"wires":[["a5b323ebee1906b2"]]},{"id":"2365266e3cbf4c50","type":"inject","z":"7d251d22c0032334","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":110,"y":320,"wires":[["82025b33bf7e2aee"]]},{"id":"2784e7b5f4930f88","type":"inject","z":"7d251d22c0032334","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"num","x":110,"y":520,"wires":[["de1376f44c2e3cc0"]]},{"id":"986e3c70dd43edd4","type":"rpi-gpio in","z":"7d251d22c0032334","name":"Button 5gp(26)","pin":"26","intype":"down","debounce":"25","read":false,"bcm":true,"x":120,"y":560,"wires":[["de1376f44c2e3cc0"]]},{"id":"de1376f44c2e3cc0","type":"function","z":"7d251d22c0032334","name":"Relay4","func":"if(msg.payload === 1) {\n msg.payload = 'relay4'\n}\n\nreturn msg\n","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":290,"y":540,"wires":[["86ebd135637bc771"]]}]

```

---

<div class="post-metadata">

**Author:** ![spawnreaper](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/spawnreaper/32/33501_2.png) [@spawnreaper](https://discourse.nodered.org/u/spawnreaper)\
**Post date:** [16 June 2023 09:02 UTC](https://discourse.nodered.org/t/first-time-using-buttons/78974/31 "2023-06-16T09:02:37Z")

</div>

way better then my attempt!  
Button 2gp(6) seems to have some trouble. But thank you so much for giving this. Its very helpfull.  
I did not think this would be such a challenge when I started.

---

<div class="post-metadata">

**Author:** ![spawnreaper](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/spawnreaper/32/33501_2.png) [@spawnreaper](https://discourse.nodered.org/u/spawnreaper)\
**Post date:** [16 June 2023 10:34 UTC](https://discourse.nodered.org/t/first-time-using-buttons/78974/32 "2023-06-16T10:34:57Z")

</div>

I dont follow the toggle command.  
it seems to envoke relay 3 but relay 3 seems to ignore the command,  
also on a fresh deploy the Button 2gp(6) seems to trigger a loop

---

<div class="post-metadata">

**Author:** ![Frida](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/frida/32/68250_2.png) [@Frida](https://discourse.nodered.org/u/Frida)\
**Post date:** [16 June 2023 13:05 UTC](https://discourse.nodered.org/t/first-time-using-buttons/78974/33 "2023-06-16T13:05:31Z")

</div>

toggle command, if one of the relays is active, all will turn off, otherwise they will turn on.

Try disconnecting your inputs and see if that makes a difference. Perhaps there are problems with one of the inputs.

---

<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:** [15 August 2023 13:05 UTC](https://discourse.nodered.org/t/first-time-using-buttons/78974/34 "2023-08-15T13:05:52Z")

</div>

Closed on 15 August 2023 13:05 UTC

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

[Previous page](https://discourse.nodered.org/t/first-time-using-buttons/78974.md?page=1)
