Loops with GPIO

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


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.

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


i tried following flow


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

Unfortunately your flow isn't currently importable.Please read the following post How to share code or flow json and then edit the above message.

flows.json (1.2 KB)

1 Like

i hope this works with json attached

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.

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"]]}]

Create a function node with the following code:

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

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

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:


        "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": [
        "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": [
        "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)

1 Like

Hi clickworkorange,

great, many thanks for your help.

1 Like

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