Looking for IO pin Debounce example


#1

There are some official examples in
https://docs.particle.io/guide/getting-started/examples/photon/

but there is no IO pin Debounce example - which must be one of the most common Particle usecases:-

Particle monitors a digital pin (or 2) and publishes an event (eg IFTTT) on change. The tricky bit is the debounce which trips up many customers (see forum)

So comeon Particle. Give us an OFFICIAL simple Debounce example


#2

I don’t think there’s a real need for an ‘official example’, since this is so common, answers can be found almost anywhere. It’s an electromechanical issue that’s thaught in the basics of EE, and there is no ‘right’ solution since various methods can be used to remedy this, both hardware and software. As such, it depends on your application and requirements which of these is the best. One single ‘official example’ would rule out other viable solutions.


#3

Hi Rhubard,

You are quite right, there is not an official Debounce example or many hardware examples in the Particle documentation.

But it might be worth mentioning from a hardware perspective, and in particular much of the sensor intergrations are 99% identical to applications which can be applied to many alternative micro processor development platforms such an Arduino. The singular biggest difference being that the Particle enviroment is 3.3v logic were many (not all) the Arduino development boards are 5v logic.

This means much of the excellent tutorials developed for the Arduino are applicable. For example

Arduino Debounce Example

So it seems, that it is a case of not reinventing a common (but not universally) know wheel. I agree frustrating if you are one of the few who don’t know about this cross platform resource, but it is there. And it is alot more common that this will be known rather not, within this type of Tech community.

It is not going to hurt to actually acquire an Arduino, and use it as a testing enviroment, for testing hardwear implimentations, before transfering the work on to a Particle device to exploit its IOT functionallitly. You might notice alot of people will when experiancing hardwear problems will ask people, if they tested it on an Arduino, just to prove its not a dub sensor, before digging down what sometimes can be an wierd issue within the Particle platform.

Its not a Particle vs Arduino game, but the two do complement each other, and it the main reason why the Intergrated Development Enviroment (IDE) are closely matching, and many of the tools share a common root.

Liam


#4

What Moors7 said is true. However after the mesh documentation is done there’s an everything you ever wanted to know about switches tutorial that will include, among many other topics, a few methods of debouncing, that’s on my list.


#5

Are you using an Interrupt to detect the pin change?
If so:

  • declare a Global Unsigned Long
  • In the ISR, evaluate if ( millis() - Global Unsigned Long >= debounceDelay )
  • if true: update the UL = millis(). & set an event flag in the ISR for loop() to react on the new data, (publish, etc)

#6

@rhubarb65 The previous contributors have summed things up well. There are in my experience 2 approaches to ‘debouncing’ - generally this applies to mechanical switches but also to some signals especially if they are derived from analogue circuits.

Approach 1. - electronic circuit debouncing, the standard circuit uses a resistor and a capacitor with the values selected to ensure a smoothed drop or rise in voltage on the IO pin. I always do this or specify it. Additionally, if the IO pin signal is coming on any length of wire it is sensible to have diode protection (for voltage spikes) and a small current limiting resistor in series.

Approach 2. - software debouncing. A quick search on the web will lead you to many clever techniques that are compute lite and can be used with ISRs. You need to select the best method for your original signal and what you need to achieve in terms of a clean output.


#7

Hi
Thanks for the suggestions. I am a software engineer myself, and I think it would be perfectly possible to do an official vanilla example or 2, eg ISR based, and non ISR based.

I did not mention earlier that I am having trouble with my particle (garden gate monitor) and so I want to get rid of as many variables as possible in order to isolate the problem. Offical code removes one variable.

But I am busy (familly) so I will wait for the cut and paste official example. How will I know when it is available.?


#8

It’s not hard to give an example or two, but doing so officially would devalue other equally good methods. You mention it yourself: you’ll just copy-paste, where that might not be the right solution for your scenario.
There’s no right or wrong way to debounce though, just ways that are better suited for different use cases.

That said, it’s a solved problem, and if you want a copy-paste solution, take a took over here: https://www.arduino.cc/en/Tutorial/Debounce it’s nothing hardware specific, just waiting for a bit, then checking again, so should work on the Particle devices just the same.


#9

@rickkas7 have you done the Switches Tutourial? link?


#10

Hi Rhubarb65, I have been using this algorithm for quite a while now with great results:
http://www.kennethkuhn.com/electronics/debounce.c

Please find one example/implementation here:

And the source code is here.

EDIT: if you feel like reading some “science” behind debouncing an input, you can do so here (excellent source of info by the way). I got the method I use from that guide.