# Raspberry Pi Read GPIO pin at regular intervals

**URL:** <https://discourse.nodered.org/t/raspberry-pi-read-gpio-pin-at-regular-intervals/25344>\
**Category:** General\
**Created:** [23 April 2020 06:41 UTC](https://discourse.nodered.org/t/raspberry-pi-read-gpio-pin-at-regular-intervals/25344 "2020-04-23T06:41:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![leeg77](https://avatars.discourse-cdn.com/v4/letter/l/c5a1d2/32.png) [@leeg77](https://discourse.nodered.org/u/leeg77)\
**Post date:** [23 April 2020 06:41 UTC](https://discourse.nodered.org/t/raspberry-pi-read-gpio-pin-at-regular-intervals/25344/1 "2020-04-23T06:41:37Z")

</div>

Hello, first time posting, I am on a steep learning curve with Node-Red, so forgive my ignorance.  
I am trying to create a flow that reads the state of a GPIO pin on the raspberry pi every minute. I can read the current status using the rpi-gpio in node but it only activates when there is a change of state and I want to read on a defined period.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [23 April 2020 07:20 UTC](https://discourse.nodered.org/t/raspberry-pi-read-gpio-pin-at-regular-intervals/25344/2 "2020-04-23T07:20:17Z")

</div>

This is the way event based programming works. If it hasn't changed it is the same value as before so why do you need to read it again ?  
If you really do need to you can save it in a flow context variable and then use a repeating inject to read it back out again, but that shouldn't be necessary.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [23 April 2020 07:25 UTC](https://discourse.nodered.org/t/raspberry-pi-read-gpio-pin-at-regular-intervals/25344/3 "2020-04-23T07:25:59Z")

</div>

I fully agree with @dceejay that it shouldnt be necessary as you can save the state in a flow variable to have access to it any time but just for options you could also use the exec node and [wiringpi](http://wiringpi.com/) in combination with an inject node to read it every minute if necessary.  
Install wiringpi using `sudo apt-get install wiringpi`and than use a command like `gpio -g read 17` in the exec where 17 is your pin to read.  
Johannes

---

<div class="post-metadata">

**Author:** ![leeg77](https://avatars.discourse-cdn.com/v4/letter/l/c5a1d2/32.png) [@leeg77](https://discourse.nodered.org/u/leeg77)\
**Post date:** [24 April 2020 11:05 UTC](https://discourse.nodered.org/t/raspberry-pi-read-gpio-pin-at-regular-intervals/25344/4 "2020-04-24T11:05:40Z")

</div>

Thank you for your replies. The purpose of the reading the GPIO at regular intervals is so that I can display the status of the GPIO pin over time but connecting the rpi gpio in node straight to the chart node gives me a graph like this instead of a line running at either 1 or 0  
 ![2020-04-24-113752_1280x1024_scrot](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/6/265d4e976a89be004d0bd00980a6f324dd0d83e2.png)

So I figured that if I set up a time stamp to take a reading every minute then I would get the desired result..I am making my first foray into the world of flow context variables but have hit another brick wall This is what I have so far

 ![2020-04-24-111602_1280x1024_scrot](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/3/03778f2c627eff8985f1ab0cd320e0977494a7f5.png)

With this in my change node

 ![2020-04-24-111830_1280x1024_scrot](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/3/0/3038a75ab600aeaeb8f8e8417bdf7f568c37dc8d.png)

Assuming what I have done is correct how do I get the message to read in the lower flow? As the I example I saw used a get option but I don't seem to have that option in my change node.

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [24 April 2020 11:17 UTC](https://discourse.nodered.org/t/raspberry-pi-read-gpio-pin-at-regular-intervals/25344/5 "2020-04-24T11:17:45Z")

</div>

There is an even easier way:  
Feed the Gpio directly into the chart but go to the settings of the chart node and at the bottom just above the color selection you will see a dropdown labeled interpolate.  
Change this from linear to step and I think you will get the desired result.  
Johannes

Edit:  
the way you set the flow var looks right, you can see that its working if you look on the top of where the debug tab is there is a context browser where you can see the set values of context flow and global vars.(click on the little arrow next to the dashboard settings)

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

---

<div class="post-metadata">

**Author:** ![JGKK](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jgkk/32/18515_2.png) [@JGKK](https://discourse.nodered.org/u/JGKK)\
**Post date:** [24 April 2020 11:37 UTC](https://discourse.nodered.org/t/raspberry-pi-read-gpio-pin-at-regular-intervals/25344/6 "2020-04-24T11:37:18Z")

</div>

And i forgot something in the editing of the previous post 🙈  
To get the flow var in another change node just turn around what you do to set it:  
`set msg.payload to flow.something`  
Johannes

---

<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:** [24 April 2020 14:13 UTC](https://discourse.nodered.org/t/raspberry-pi-read-gpio-pin-at-regular-intervals/25344/7 "2020-04-24T14:13:14Z")

</div>

If you want to get a regular repeat of the gpio signal to keep the graph updated then insert a Trigger node between the GPIO and the chart. Configure the Trigger node like this  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/3/4313cc7cc677d9c52e9df2c1ece317399f519be0.png)  
Then the original value will be passed on every minute till the pin changes state, then that value will be repeated.

---

<div class="post-metadata">

**Author:** ![leeg77](https://avatars.discourse-cdn.com/v4/letter/l/c5a1d2/32.png) [@leeg77](https://discourse.nodered.org/u/leeg77)\
**Post date:** [25 April 2020 14:42 UTC](https://discourse.nodered.org/t/raspberry-pi-read-gpio-pin-at-regular-intervals/25344/8 "2020-04-25T14:42:27Z")

</div>

Thank you all for your responses and solutions, I seem to have got the desired solution. Now ll I have got to do is create a pie chart to show the percentage time that the pin has been high or low.... but that is for another day.

---

<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:** [24 June 2020 14:42 UTC](https://discourse.nodered.org/t/raspberry-pi-read-gpio-pin-at-regular-intervals/25344/9 "2020-06-24T14:42:27Z")

</div>

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