# Optional gpio input debounce

**URL:** https://discourse.nodered.org/t/optional-gpio-input-debounce/12131
**Category:** Feature Requests
**Created:** [12 June 2019 16:06 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131 "2019-06-12T16:06:56Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![edrean](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edrean/32/9263_2.png) [@edrean](https://discourse.nodered.org/u/edrean)
#### Post date: [12 June 2019 16:06 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/1 "2019-06-12T16:06:56Z")

</div>

I'm dealing with an input that is pulled high and then goes low for a very small amount of time before going high again, in other words a very quick low-going pulse. The GPIO input node does not pick it up and I'm fairly certain that it is due to the debounce. I need a way to turn off the debounce feature on an input pin so that I can read very quick pulses.

One way to do this could be to have a checkbox indicating if debounce is required. If it is checked, the debounce value is used and implemented. Otherwise not.

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [12 June 2019 16:51 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/2 "2019-06-12T16:51:36Z")

</div>

Hi  
Couple of points

Debouncing usually means ignoring any changes after the first change for a small period of time. So any debouncing shouldn't effect detection of the transition from high to low

I think setting the debounce time to 0 turns it off in any case

---

<div class="post-metadata">

### Author: ![edrean](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edrean/32/9263_2.png) [@edrean](https://discourse.nodered.org/u/edrean)
#### Post date: [12 June 2019 18:14 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/3 "2019-06-12T18:14:28Z")

</div>

In my experience, debounce could be implemented in two ways. One is what you mentioned.

The other is to wait for a period of time after state change and see if the state change persists. If not, ignore.

I'm not sure how it is implemented here, but I know two things are happening :

- No state change is detected when given a very quick pulse (\< 1ms).
- Setting debounce time to 0 causes the node to indicate "Stopped".

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [12 June 2019 20:41 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/4 "2019-06-12T20:41:24Z")

</div>

Sorry- your right - if debounce is set to 0 - it says stopped and ignores any changes on the input.

Which makes me now think that the debounce might not be implemented in the way I thought it was

Off to check source code to find out ...

---

<div class="post-metadata">

### Author: ![edrean](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edrean/32/9263_2.png) [@edrean](https://discourse.nodered.org/u/edrean)
#### Post date: [12 June 2019 20:42 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/5 "2019-06-12T20:42:56Z")

</div>

Thanks for your effort!

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [12 June 2019 21:00 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/6 "2019-06-12T21:00:51Z")

</div>

If I've followed the rabbit hole correctly then the python code that handles the pin does debounce by waiting the bounce time and then reporting the pin state (the method you suggested)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/2129e47a70cc66408575562a1cc64c22c7c19793.png)

Just trying to find out why a value of 0 isn't allowed

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [12 June 2019 21:09 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/7 "2019-06-12T21:09:04Z")

</div>

Closing in on it

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/2/229c48df0732e547f3e9e3adaff860d149eb6431.jpeg)

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [12 June 2019 21:21 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/8 "2019-06-12T21:21:08Z")

</div>

My current thinking is this  
node-red uses a python prog nrgpio.py to handle the Pi pins

That uses standard RPi.GPIO library

RPi.GPIO library handles debouncing internally but doesn't accept a value of 0 for bouncetime - reports an error and that error causes the node to stop

Going to see if I can make a small mod to nrgpio.py to test for 0 bouncetime  
[edit]

I made a mod and it worked (as in 0 bounce time doesn't stop the node from working)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/af4e1e2e3135ade71871e2dbe7f0a2216e9cad0e.png)

I don't have a source of 1ms pulses handy (prob going to need another Pi to generate them) but maybe your able to modify the same file on your Pi and see what happens?

I'm off to bed now

[/edit]

---

<div class="post-metadata">

### Author: ![edrean](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edrean/32/9263_2.png) [@edrean](https://discourse.nodered.org/u/edrean)
#### Post date: [12 June 2019 21:41 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/9 "2019-06-12T21:41:44Z")

</div>

Also off to bed now. Will try in the morning and report back.

---

<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: [13 June 2019 04:20 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/10 "2019-06-13T04:20:39Z")

</div>

@cymplecy Happy to look at a Pull request for this mod if you want to provide one.

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [13 June 2019 16:33 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/11 "2019-06-13T16:33:13Z")

</div>

I rigged up a WEMOS D1 mini to produce 1ms pulses every 3 seconds and connected it to my Pi - my modified node seems to catch them reliably with bounce time set to 0

---

<div class="post-metadata">

### Author: ![edrean](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edrean/32/9263_2.png) [@edrean](https://discourse.nodered.org/u/edrean)
#### Post date: [13 June 2019 20:12 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/12 "2019-06-13T20:12:28Z")

</div>

I made the changes to the code as you did above. I can confirm that a debounce time of 0 no longer causes the flow to stop.

However, it still does not catch my pulses.

I will put my pulses on a scope tomorrow and come back with more exact information of my setup.

In the meantime, a quick question. Does the GPIO node make use of the interrupt functionality of the pin? Or is it simply polling the pin on a regular basis? I can get it to work with the node-red-contrib-opi-gpio library. It uses the interrupt to trigger the change. The difference might be with how the two gpio nodes are triggered...

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [13 June 2019 20:32 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/13 "2019-06-13T20:32:09Z")

</div>

> [@edrean](#):
>
> Does the GPIO node make use of the interrupt functionality of the pin? Or is it simply polling the pin on a regular basis?

I don't know whether it uses interrupts or polling I'm afraid - I'll see if I can find out

What settings are you using (board type / pin number) to get this opi node to work on a Pi?

Where are you getting your pulses from BTW?

---

<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: [13 June 2019 20:54 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/14 "2019-06-13T20:54:38Z")

</div>

Well the code was as per above

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/81eb4415029b90a3edfe8cbe411613303d7a074c.png)  
It uses interrupts on both edges - which then call the callback code which does  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/5/56ab85c1e0896b5b6b9dd2a44e2286bdea856a8d.png)  
ie waits for the debounce time then re-reads the pin.

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [14 June 2019 06:38 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/15 "2019-06-14T06:38:19Z")

</div>

> [@cymplecy](#):
>
> I don't know whether it uses interrupts or polling I'm afraid

What I mean is - I don't know whether the RPi.GPIO library is reacting to hardware interrupts or just doing fast polling and generating the callbacks that way - does the Pi have hardware interrupts on its GPIO pins?

[edit]

Reading the RPi.GPIO docs - I think it strongly implies that it is using interrupts

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/d/d27a48e1c3530b5e56996623d7f2de1610a37c04.png)

[/edit]

---

<div class="post-metadata">

### Author: ![edrean](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/edrean/32/9263_2.png) [@edrean](https://discourse.nodered.org/u/edrean)
#### Post date: [1 July 2019 12:44 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/16 "2019-07-01T12:44:16Z")

</div>

Sorry for the long silence.

I have since hooked up my scope and checked the pulse width. It is a low going pulse of around 0.2ms in duration. See below (0.1ms per div):  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/a25d958d907496da8f309783d7765bb8b7407937.png)

> [@cymplecy](#):
>
> Where are you getting your pulses from BTW?

It is generated by a diesel flow meter.

> [@cymplecy](#):
>
> What settings are you using (board type / pin number) to get this opi node to work on a Pi?

I'm using board type as "other".  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/a6aa7b72599f305077ecbd5680e3fc1c1abfdd31.png)  
I then use a stray rpi-gpio node just to enable the pullup on the pin.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/3/37af84b96cfea8834fe567ccbd35f3a6dfce160e.png)

---

<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: [1 July 2019 14:52 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/17 "2019-07-01T14:52:02Z")

</div>

I believe this is the first time you have mentioned that you are using node-red-contrib-opi-gpio node. Everyone here has (I imagine) been assuming that you are using the standard node-red gpio node. Try the standard one and see if it works ok.  
In addition I dread to think what the effect of also using a standard gpio node watching the same pin is. Why are you not just using that for reading the pin, now that you can specify 0 debounce on that node?

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [1 July 2019 14:54 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/18 "2019-07-01T14:54:06Z")

</div>

> [@Colin](#):
>
> I believe this is the first time you have mentioned that you are using node-red-contrib-opi-gpio node  
> ...  
> Why are you not just using that for reading the pin,

No - they told us earlier that they got it work using it

> [@Optional gpio input debounce](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/12):
>
> I made the changes to the code as you did above. I can confirm that a debounce time of 0 no longer causes the flow to stop. However, it still does not catch my pulses. I will put my pulses on a scope tomorrow and come back with more exact information of my setup. In the meantime, a quick question. Does the GPIO node make use of the interrupt functionality of the pin? Or is it simply polling the pin on a regular basis? I can get it to work with the node-red-contrib-opi-gpio library. It uses th…

---

<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: [1 July 2019 14:55 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/19 "2019-07-01T14:55:58Z")

</div>

Ah, I missed that. I take it all back.

---

<div class="post-metadata">

### Author: ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)
#### Post date: [1 July 2019 15:06 UTC](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131/20 "2019-07-01T15:06:21Z")

</div>

> [@edrean](#):
>
> It is a low going pulse of around 0.2ms

Aah - that could possibly explain why the standard node isn't catching it - it will get triggered by the high/low transition but the pin value could have returned to it high before it state is read and sent out

I'll have a play using 0.1ms pulses and see if I can come up with method of reporting them

[Next page](https://discourse.nodered.org/t/optional-gpio-input-debounce/12131.md?page=2)
