Measuring and filtering spikes

229 views
Skip to first unread message

Bart Butenaers

unread,
Sep 3, 2017, 5:04:21 AM9/3/17
to Node-RED
Hi folks,

I connected yesterday a magnetic sensor (reed relay) to a GPIO pin of my Raspberry.  The pin is monitored by a RPI-GPIO pin that produces following messages:
  • Payload '0' when door is closed
  • Payload '1' when door is open
This works fine, except from spikes from time to time:


I could get rid of these spikes by adding resistors and capacitors, but I would prefer to solve this in my flow ...


I (ab)used the debouncer of the RPI-GPIO node to get rid of these glitches:


Now everything seems to be working fine ...  

However I have a few questions about this setup:

  • I'm a little bit confused that a debouncer can solve this.  I should except that the debouncer should accept the first edge, and only ignore the second edge (since this is within the interval of 400 msec).   Now both edges (i.e. the entire spike) is ignored.  This is good for this case, but I didn't expect that behaviour.  I think I'm overlooking something here ...  Even the description on the RPI-GPIO website explains it like that ('further' edges):

# add rising edge detection on a channel, ignoring further edges for 200ms for switch bounce handling
GPIO
.add_event_detect(channel, GPIO.RISING, callback=my_callback, bouncetime=200)


  • I saw in the other discussions (about RPI-GPIO) keywords like smoothing, filtering, ...  Are there better ways (for any means) to get rid of these spikes?
  • I determined the debounce interval of 400 msecs by trial and error.  But I would like to be able to measure the (maximum) length of the spikes, during e.g. 24 hours: to be able to determine the minimal debounce interval that fits my needs.   How can I measure the length of such spikes, i.e. how can I measure the interval length between successive messages?  I cannot find a contribution, but perhaps I used the wrong keywords.  If it doesn't exist yet, I would like to publish a new contribution: any feedback on required functionality would be welcome !
Thanks a lot !

Bart Butenaers

Colin Law

unread,
Sep 3, 2017, 5:31:29 AM9/3/17
to node...@googlegroups.com
It may well just be sloppy wording on the website. The common way for debounce to work is to delay the output signal for the specified time, and only signal an event if the input is still there at the end of the period. Thus short spikes are completely ignored.

I can see a number of ways of answering the timing question, but none is very elegant. I will leave this one for someone else to come up with a nice solution.

Colin

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.
To view this discussion on the web, visit https://groups.google.com/d/msgid/node-red/593114c1-5e76-4735-ae60-27f51aa8c6c3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Caparrós

unread,
Sep 3, 2017, 9:17:35 AM9/3/17
to Node-RED
In any case you should add a pull-down resistor mate....

David Caparrós

unread,
Sep 3, 2017, 9:19:38 AM9/3/17
to Node-RED

You can not let the pin floating, this will be the end of the story... you will see, 10K resistor for instance.

Colin Law

unread,
Sep 3, 2017, 9:47:44 AM9/3/17
to node...@googlegroups.com
David is correct, if you have not already done so you should have a
resistor from the pin. Usually though I wire the resistor (4.7k is ok)
from the pin up to 3.3V and the relay shorts the pin to 0V.

Colin

On 3 September 2017 at 14:17, David Caparrós <davi...@gmail.com> wrote:
> In any case you should add a pull-down resistor mate....
>
> --
> http://nodered.org
>
> Join us on Slack to continue the conversation: http://nodered.org/slack
> ---
> You received this message because you are subscribed to the Google Groups
> "Node-RED" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to node-red+u...@googlegroups.com.
> To post to this group, send email to node...@googlegroups.com.
> Visit this group at https://groups.google.com/group/node-red.
> To view this discussion on the web, visit
> https://groups.google.com/d/msgid/node-red/35cde2da-a5f3-47cd-993d-409c439b8f98%40googlegroups.com.

Toshi Bass

unread,
Sep 3, 2017, 10:16:41 AM9/3/17
to Node-RED
eer he's setting the   resistor? to pullup  I have always assumed that sets the raspberry pi's internal resistor on that gpio, so you don't need a physical resistor 

Colin Law

unread,
Sep 3, 2017, 10:36:46 AM9/3/17
to node...@googlegroups.com
The internal resistor is fairly large, I believe. It is ok if the pin
is just connected to something adjacent to the pi (such as some logic
on a connected PCB, but if the connection is going out into the real
world that may well be too high a value to prevent pickup of spurious
signals. That is why I suggest fitting a 4.7k resistor.

Colin
> https://groups.google.com/d/msgid/node-red/9a373709-c5e3-4d1c-97b7-d8b1eb36283f%40googlegroups.com.

Bart Butenaers

unread,
Sep 3, 2017, 3:13:58 PM9/3/17
to Node-RED
Hi folks,

thanks for all the help !!!

So the debouncing mechanism is clear to me, thanks to Colin.  

I was also expecting that the internal resistor (in the Raspberry) would be sufficient for my simple setup:

To me the pin40 doesn't seem to be floating:
  • Door open --> Resistance (of reed relay and wire) = infinite --> pin40 will be +- 3.3 Volt --> msg.payload = 1
  • Door closed --> Resistance (of reed relay and wire) = 4,5 ohm --> pin40 will be a little bit above 0V --> msg.payload = 0 (with spikes from time to time)
Is the small voltage across the 4,5V resistor messing up my entire measurement setup?

I will do some experiments with the external resistors (as you guys suggested), and post my results ...

Kind regards,
Bart

Colin Law

unread,
Sep 3, 2017, 5:21:26 PM9/3/17
to node...@googlegroups.com
There are two potential (sorry!) issues. Firstly the fact that reed switches *do* bounce, particularly when they close. The way to solve this is with the software debounce that waits for it to settle before signalling the state. The second is the possibility of electrical pickup in the wire when the reed is open. Our houses are full of electrical noise, radiation from sparks when you switch a kettle on/off, radiation from switching power supplies, radiation from computers etc. When the reed is open you have a wire that will pick up this radiation (as a radio aerial does for example) and connect it to the pin. If the radiation is powerful enough then it can look switch the pin transiently.  The point about the resistance value of the pullup resistor is that the lower the resistance the less effect the interference has, so the less likely it is to be seen as a signal by the pin.  You can't use a very low value though or it will load the power supply when the reed is closed, and generally a value of 4.7k is good enough.  This should keep the interference to a minimum and then the debounce software should mop up and odd spikes that do get through.

To answer your specific question about the voltage when the reed is closed, a value of 4.5 ohms with the current through the 4.7k resistor  is only a few millivolts and will not get anywhere near the switching point of the pin. I am surprised that you say you see occasional spikes with the reed closed. Are you sure the reed is not being disturbed or something? If you tap a reed switch then it may open or close transiently for example.  Is the reed a long way from the pi?  What sort of wiring have you got between the switch and the pi?

Colin

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.

To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

Bart Butenaers

unread,
Sep 3, 2017, 5:36:53 PM9/3/17
to Node-RED
Colin, 
interesting information!  The distance is only about 6 meter, but the wire runs into a tube with lots of other cables (power supply cables, ethernet cables, ...).  So perhaps there is some interference: because for the reed relais I have used normal dual flat ribbon speaker cable (because I had a lot of it available at the time being ...):

Colin Law

unread,
Sep 3, 2017, 5:44:21 PM9/3/17
to node...@googlegroups.com
Six metres is a long way to run a cable directly connected to the processor. Put the pull up on and play with the debounce value and see if you can make it go reliably. Twisted pair might well be better.

Colin

--
http://nodered.org
 
Join us on Slack to continue the conversation: http://nodered.org/slack
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+unsubscribe@googlegroups.com.
To post to this group, send email to node...@googlegroups.com.
Visit this group at https://groups.google.com/group/node-red.

David Caparrós

unread,
Sep 3, 2017, 6:02:02 PM9/3/17
to Node-RED
I had some issues while using too small section cable, this could be one trouble, consider to use a dull-down resistor I'm sure you will get ride of your spikes with this + ensure enough cable section, try this mate and will see, no need to start thinking about more strange cases.

Regards

Bart Butenaers

unread,
Feb 25, 2018, 11:07:25 AM2/25/18
to Node-RED
Hi guys,

Some time has passed, but will here summarize what I have done meanwhile to get rid of all the noise and spikes.  Perhaps it can be of any help to other users...

The input impedance of the Raspberry GPIO inputs are very high (about 50 kOhm), see link.  

The GPIO pins have been designed this way, to assure that a signal source - whose signal we are measuring - is not loaded heavily (by keeping the current low).  This way the Raspberry can measure signals without consuming lots of energy.


However I had long wires between my magnetic sensors and the GPIO pins, and unfortunately those wires are bad quality (i.e. lack of shielding and twisting).  These wires act as antenna's, that receive all kind of environmental noise (from nearby devices, AC lines, ...):

Since the Raspberry has a very high impedance, a small distortion will result quickly in a rather high noise level.   As a result I ended up reading a very noise signal.  I deactivated the Raspberry's internal pullup resistor, and added a an external 10 kOhm resistor (between GPIO pin and 3,3V).  The signal was cleaner, but there was still too much noise (1 division = 1 Volt):



As soon as some threshold voltage is exceeded, the GPIO pin will consider (incorrectly) the noise as a digital 1:


As a result, the RPI-GPIO input node injected an horrible amount of messages into my flow: the flow editor freezed since it could not handle all the information ;-( 


The node's debouncing functionality is added to get rid of a short series of spikes, not for an endless spike train...  In this case the debouncer only reduces (drastically) the amount of messages.  When the debounce interval is N msecs, all messages within that interval will not be injected in the flow.  But at the end of the interval, a message will be generated if the GPIO pin still has the same value (compared to the value at the start of the interval).  Since we have lots of noise, chances are pretty high that we receive still lots of messages (but with a plural of N msecs in between).  I assume that explains why I got spikes at rather regular intervals.


At the end I have added a low-pass RC filter, that filters away all high frequencies:

P.S. I also tried it first without the resistor of 270 ohm, however then it doesn't detect anymore when the door is opened and closed very fast ...


Now at least I had a clean signal !!  And my flow started running like nothing happened ...


Bart Butenaers

Reply all
Reply to author
Forward
0 new messages