# Loops with GPIO

**URL:** <https://discourse.nodered.org/t/loops-with-gpio/15623>\
**Category:** General\
**Created:** [17 September 2019 10:52 UTC](https://discourse.nodered.org/t/loops-with-gpio/15623 "2019-09-17T10:52:34Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![maku12](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@maku12](https://discourse.nodered.org/u/maku12)\
**Post date:** [17 September 2019 10:52 UTC](https://discourse.nodered.org/t/loops-with-gpio/15623/1 "2019-09-17T10:52:34Z")

</div>

Hi, i want to turn on a led (gpio 17) for 2 seconds , then turn off for 5 seconds, and this should be repated 5 times. How can I do this with Nodes Red nodes?

Many Thanks

Kind Regards

marc

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [17 September 2019 10:58 UTC](https://discourse.nodered.org/t/loops-with-gpio/15623/2 "2019-09-17T10:58:04Z")

</div>

turn on then off can be done with the `trigger` node  
then stay off for a period can be done with the `delay` node

How to repeat? You will need to store a value and then check on that value. Take a look at the "working with context" page in the docs. and create a loop by joining the output of a node back to the input of an earlier node.

---

<div class="post-metadata">

**Author:** ![Bobo](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobo/32/8401_2.png) [@Bobo](https://discourse.nodered.org/u/Bobo)\
**Post date:** [17 September 2019 10:58 UTC](https://discourse.nodered.org/t/loops-with-gpio/15623/3 "2019-09-17T10:58:17Z")

</div>

Try doing a search in this forum on gpio, there are numerous posts on the subject.

---

<div class="post-metadata">

**Author:** ![maku12](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@maku12](https://discourse.nodered.org/u/maku12)\
**Post date:** [17 September 2019 12:29 UTC](https://discourse.nodered.org/t/loops-with-gpio/15623/4 "2019-09-17T12:29:16Z")

</div>

Hi,

i tried following flow

[{"id":"4f99c8dc.2bfcd8","type":"trigger","z":"a8eea3bb.19802","op1":"1","op2":"1","op1type":"num","op2type":"num","duration":"3","extend":false,"units":"s","reset":"","bytopic":"all","name":"","x":761,"y":391,"wires":[["5a120861.468828"]]}]

I expected that the gpio led shine 3 sek, then 2 sek. off, then shine again.

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [17 September 2019 12:32 UTC](https://discourse.nodered.org/t/loops-with-gpio/15623/5 "2019-09-17T12:32:01Z")

</div>

Unfortunately your flow isn't currently importable.Please read the following post [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506) and then edit the above message.

---

<div class="post-metadata">

**Author:** ![maku12](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@maku12](https://discourse.nodered.org/u/maku12)\
**Post date:** [17 September 2019 12:35 UTC](https://discourse.nodered.org/t/loops-with-gpio/15623/6 "2019-09-17T12:35:19Z")

</div>

[flows.json](https://discourse.nodered.org/uploads/short-url/x9KKVR8t6BPVT9LRg1juCprq1qh.json) (1.2 KB)

---

<div class="post-metadata">

**Author:** ![maku12](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@maku12](https://discourse.nodered.org/u/maku12)\
**Post date:** [17 September 2019 12:36 UTC](https://discourse.nodered.org/t/loops-with-gpio/15623/7 "2019-09-17T12:36:00Z")

</div>

i hope this works with json attached

---

<div class="post-metadata">

**Author:** ![ukmoose](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ukmoose/32/13_2.png) [@ukmoose](https://discourse.nodered.org/u/ukmoose)\
**Post date:** [17 September 2019 12:43 UTC](https://discourse.nodered.org/t/loops-with-gpio/15623/8 "2019-09-17T12:43:12Z")

</div>

It will work but requires more steps for anyone wanting to import your flow and also requires someone to trust as to what they are downloading as opposed to being able to see what they are copying. Which is why I linked to instructions that show you exactly what you needed to do.

---

<div class="post-metadata">

**Author:** ![maku12](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@maku12](https://discourse.nodered.org/u/maku12)\
**Post date:** [17 September 2019 12:46 UTC](https://discourse.nodered.org/t/loops-with-gpio/15623/9 "2019-09-17T12:46:17Z")

</div>

ok i see, sorry.

`[{"id":"a8eea3bb.19802","type":"tab","label":"Flow 2"},{"id":"5a120861.468828","type":"rpi-gpio out","z":"a8eea3bb.19802","name":"","pin":"11","set":"","level":"0","out":"out","x":901,"y":517,"wires":[]},{"id":"29e4db63.8ab454","type":"inject","z":"a8eea3bb.19802","name":"","topic":"","payload":"1","payloadType":"num","repeat":"","crontab":"","once":false,"onceDelay":"","x":211,"y":334,"wires":[["fe1197c.be32f68"]]},{"id":"fe1197c.be32f68","type":"trigger","z":"a8eea3bb.19802","op1":"1","op2":"0","op1type":"num","op2type":"num","duration":"3","extend":false,"units":"s","reset":"","bytopic":"all","name":"","x":403.5,"y":249,"wires":[["2e6669c0.0bed26"]]},{"id":"2e6669c0.0bed26","type":"delay","z":"a8eea3bb.19802","name":"","pauseType":"delay","timeout":"2","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":610.5,"y":313,"wires":[["4f99c8dc.2bfcd8"]]},{"id":"4f99c8dc.2bfcd8","type":"trigger","z":"a8eea3bb.19802","op1":"1","op2":"1","op1type":"num","op2type":"num","duration":"3","extend":false,"units":"s","reset":"","bytopic":"all","name":"","x":761,"y":391,"wires":[["5a120861.468828"]]}]`

---

<div class="post-metadata">

**Author:** ![clickworkorange](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/clickworkorange/32/12109_2.png) [@clickworkorange](https://discourse.nodered.org/u/clickworkorange)\
**Post date:** [17 September 2019 17:24 UTC](https://discourse.nodered.org/t/loops-with-gpio/15623/10 "2019-09-17T17:24:12Z")

</div>

> [@maku12](#):
>
> i want to turn on a led (gpio 17) for 2 seconds , then turn off for 5 seconds, and this should be repated 5 times. How can I do this with Nodes Red nodes?

Create a function node with the following code:

```javascript
var count = 5;
var tick = setInterval(tock, 7000);

function tock() {
    if(count > 0) {
        node.send({"payload": 1});
        setTimeout(function() {
              node.send({"payload": 0});
        },2000);
        count--;
    } else { 
        clearInterval(tick);
    }
}

```

When it's triggered, it will set a timer which ticks every seven seconds. The function which gets called each tick (`tock`) fires off a message with the payload set to 1, before adding a one-shot two second timeout which (via an anonymous function) sends another message with the payload set to 0. The `count` variable is decremented by 1 with each tick; after 5 ticks, the repeating timer is stopped.

**Edit:** An example flow:  
**Edit 2:** Updated example flow:

![Screenshot_2019-09-17_22-20-32](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/4/4b86b35e925cb19b41568727839e4637fc4e40db.png)

```nodered
[
    {
        "id": "71143251.cec954",
        "type": "function",
        "z": "db824c43.5b7dc8",
        "name": "Tick-Tock",
        "func": "var count = 5;\nvar tick = setInterval(tock, 7000);\n\nfunction tock() {\n if(count > 0) {\n node.send({\"payload\": 1});\n setTimeout(function() {\n node.send({\"payload\": 0});\n },2000);\n count--;\n } else { \n clearInterval(tick);\n }\n}",
        "outputs": 1,
        "noerr": 0,
        "x": 360,
        "y": 3180,
        "wires": [
            [
                "1ff11c2c.b1d37c"
            ]
        ]
    },
    {
        "id": "a1158aca.f9b0b",
        "type": "inject",
        "z": "db824c43.5b7dc8",
        "name": "Go!",
        "topic": "",
        "payload": "true",
        "payloadType": "bool",
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "x": 210,
        "y": 3180,
        "wires": [
            [
                "71143251.cec954"
            ]
        ]
    },
    {
        "id": "1ff11c2c.b1d37c",
        "type": "rpi-gpio out",
        "z": "db824c43.5b7dc8",
        "name": "",
        "pin": "11",
        "set": "",
        "level": "0",
        "freq": "",
        "out": "out",
        "x": 510,
        "y": 3180,
        "wires": []
    }
]

```

(Note: "Pin 11" = GPIO 17)

---

<div class="post-metadata">

**Author:** ![maku12](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@maku12](https://discourse.nodered.org/u/maku12)\
**Post date:** [18 September 2019 07:16 UTC](https://discourse.nodered.org/t/loops-with-gpio/15623/11 "2019-09-18T07:16:54Z")

</div>

Hi clickworkorange,

great, many thanks for your help.

---

<div class="post-metadata">

**Author:** ![clickworkorange](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/clickworkorange/32/12109_2.png) [@clickworkorange](https://discourse.nodered.org/u/clickworkorange)\
**Post date:** [18 September 2019 09:56 UTC](https://discourse.nodered.org/t/loops-with-gpio/15623/12 "2019-09-18T09:56:44Z")

</div>

Thanks @maku12. Don't forget to mark your question as "solved" - if indeed you feel it is 🙂
