# How can I detect if a Raspberry Pi GPIO pin changes?

**URL:** <https://discourse.nodered.org/t/how-can-i-detect-if-a-raspberry-pi-gpio-pin-changes/28331>\
**Category:** General\
**Created:** [14 June 2020 22:54 UTC](https://discourse.nodered.org/t/how-can-i-detect-if-a-raspberry-pi-gpio-pin-changes/28331 "2020-06-14T22:54:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sofakng](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@sofakng](https://discourse.nodered.org/u/sofakng)\
**Post date:** [14 June 2020 22:54 UTC](https://discourse.nodered.org/t/how-can-i-detect-if-a-raspberry-pi-gpio-pin-changes/28331/1 "2020-06-14T22:54:51Z")

</div>

Node-RED on my Raspberry Pi came installed with the node-red-node-pi-gpio module. It has a [rpi gpio in] object that can read the state of any of the Raspberry Pi GPIO pins.

However, how can I detect when the GPIO pin value changes? (i.e. goes from 0 to 1 or 1 to 0)

I actually need to detect whenever any one of three pins change. When any of them change I need to read the state of all three and then convert it into a number (0 to 6).

---

<div class="post-metadata">

**Author:** ![Martin45](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/martin45/32/81884_2.png) [@Martin45](https://discourse.nodered.org/u/Martin45)\
**Post date:** [15 June 2020 01:51 UTC](https://discourse.nodered.org/t/how-can-i-detect-if-a-raspberry-pi-gpio-pin-changes/28331/2 "2020-06-15T01:51:54Z")

</div>

I added the function node for checking the pin state using flow context.

Try this flow! 🙂

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

` [{"id":"3c85057d.ccf3c2","type":"rpi-gpio in","z":"149c4183.9f951e","name":"","pin":"11","intype":"tri","debounce":"25","read":false,"x":301,"y":192,"wires":[["8a007729.76fc88"]]},{"id":"85c67717.9cc928","type":"rpi-gpio in","z":"149c4183.9f951e","name":"","pin":"13","intype":"tri","debounce":"25","read":false,"x":300,"y":266,"wires":[["fbaddec3.e2f93"]]},{"id":"8a1b1c0a.fba1d","type":"rpi-gpio in","z":"149c4183.9f951e","name":"","pin":"15","intype":"tri","debounce":"25","read":false,"x":303,"y":351,"wires":[["e649dbcc.689df"]]},{"id":"8a007729.76fc88","type":"function","z":"149c4183.9f951e","name":"Set State","func":"if(!flow.get(\"state1\"))\n flow.set(\"state1\",msg.payload)\n\n\nlet state = flow.get(\"state1\")\nflow.set(\"state1\",msg.payload)\n\nif(state !== msg.payload)\n msg.payload = {isChanged : true}\nelse\n msg.payload = {isChanged :false}\n\nreturn msg;","outputs":1,"noerr":0,"x":507,"y":193,"wires":[["cb72d425.8b936"]]},{"id":"fbaddec3.e2f93","type":"function","z":"149c4183.9f951e","name":"Set State","func":"if(!flow.get(\"state2\"))\n flow.set(\"state2\",msg.payload)\n\n\nlet state = flow.get(\"state2\")\nflow.set(\"state2\",msg.payload)\n\nif(state !== msg.payload)\n msg.payload = {isChanged : true}\nelse\n msg.payload = {isChanged :false}\n\nreturn msg;","outputs":1,"noerr":0,"x":505,"y":269,"wires":[["cb72d425.8b936"]]},{"id":"e649dbcc.689df","type":"function","z":"149c4183.9f951e","name":"Set State","func":"if(!flow.get(\"state3\"))\n flow.set(\"state3\",msg.payload)\n\n\nlet state = flow.get(\"state3\")\nflow.set(\"state3\",msg.payload)\n\nif(state !== msg.payload)\n msg.payload = {isChanged : true}\nelse\n msg.payload = {isChanged :false}\n\nreturn msg;","outputs":1,"noerr":0,"x":508,"y":350,"wires":[["cb72d425.8b936"]]},{"id":"cb72d425.8b936","type":"switch","z":"149c4183.9f951e","name":"Check ","property":"payload.isChanged","propertyType":"msg","rules":[{"t":"true"}],"checkall":"true","repair":false,"outputs":1,"x":735,"y":268,"wires":[["8857693c.f0f8d8"]]},{"id":"8857693c.f0f8d8","type":"debug","z":"149c4183.9f951e","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":949,"y":266,"wires":[]}]`

---

<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:** [14 August 2020 01:52 UTC](https://discourse.nodered.org/t/how-can-i-detect-if-a-raspberry-pi-gpio-pin-changes/28331/3 "2020-08-14T01:52:03Z")

</div>

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