tekkie1618
Posts: 1
Joined: Fri Aug 19, 2016 7:37 pm

Help with GPIO Bouncing?

Fri Aug 19, 2016 7:57 pm

I'm trying to create an RPM detector using a hall effect sensor. I need to detect RPM anywhere from 0-10000 (around 167 detections per second) if possible. My current script would not be able to detect that many since I only have it running on a 0.01 refresh, but I wanted to figure out this part before moving on. I will be using a button to test this until I get it ready for the hall effect. The script I have works, but due to bouncing, it will count up for every 'tick' the input is held down. How would I only add 1 to my counter when the input changes to on?

Here is my script:

Code: Select all

import RPi.GPIO as GPIO
import time

halldetect = 0
timer = 0

GPIO.setmode(GPIO.BCM)
GPIO.setup(24,GPIO.IN, pull_up_down=GPIO.PUD_UP)

try:
    
    while True:
        hall = GPIO.input(24)

        if timer < 1:

            if not hall:
                halldetect += 1
            timer += 0.01
        
        else:
            halldetect = 0
            timer = 0

        print(halldetect)
        time.sleep(0.01)
        
except KeyboardInterrupt:
    GPIO.cleanup()

SIDF92
Posts: 17
Joined: Mon Jul 25, 2016 8:05 am

Re: Help with GPIO Bouncing?

Mon Aug 22, 2016 8:09 am

Hi tekkie1618,

I'm using a simple clever debouncing algorithm from Dr Marty's on my project.
http://drmarty.blogspot.fr/2009/05/best ... -ever.html

Example of my implementation in C language :

Code: Select all

TYPE_B read_debounced_input1(TYPE_B b_input_value)
    {
    static TYPE_B b_sample_B = true;
    static TYPE_B b_sample_C = true;
    static TYPE_B b_sample_D = true;
    static TYPE_B b_sample_E = true;
    static TYPE_B b_sample_F = true;
    static TYPE_B b_sample_G = true;
    static TYPE_B b_sample_H = true;    
    static TYPE_B b_last_debounced = true;    
    
    b_last_debounced = (b_last_debounced &
        (b_input_value | b_sample_B | b_sample_C | b_sample_D |
        b_sample_E | b_sample_F | b_sample_G | b_sample_H) |
        (b_input_value & b_sample_B & b_sample_C & b_sample_D &
        b_sample_E & b_sample_F & b_sample_G & b_sample_H));
    
    b_sample_H = b_sample_G;
    b_sample_G = b_sample_F;
    b_sample_F = b_sample_E;
    b_sample_E = b_sample_D;
    b_sample_D = b_sample_C;
    b_sample_C = b_sample_B;
    b_sample_B = b_input_value;

    return (b_last_debounced);
    }
Have fun !
Regards.
SIDF92

User avatar
joan
Posts: 14935
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Help with GPIO Bouncing?

Mon Aug 22, 2016 8:19 am

Isn't that a sampling problem? No evidence of bounce.

See viewtopic.php?p=632413#p632413 for a practical experiment.

Return to “Beginners”