Counting pulses, debouncing etc

Active today
Viewed 11 times

I am using the below code to capture the pulses and count pulses for a specified interval but the output not as what i expected. For every pulse it is giving uncertain value.

The pulse i want to count is from a motor that will generate one pulse per rotation.


import time

import pigpio



pigpio.set_mode(WIND_GPIO, pigpio.INPUT)
pigpio.set_pull_up_down(WIND_GPIO, pigpio.PUD_UP)

wind_cb = pigpio.callback(WIND_GPIO, pigpio.FALLING_EDGE)

old_count = 0

while True:


   count = wind_cb.tally()
   print("counted {} pulses".format(count - old_count))
   old_count = count


I found this code on https://www.raspberrypi.org/forums/viewtopic.php?p=507103

  • Perhaps your motor is too noisy and bouncy. Increase debouncing time might help. I usually recommend newbies to use a stupid, simple, time wasting loop to count the pulses, instead of interrupts. Once you can count using a loop, you can modify the loop program to interrupt program. It is also a good idea to experiment counting human button pressing pulses, before trying motor pulses. – tlfong01 40 mins ago  
  • like for one pulse given it is increasing the count for uncertain value, is it because of noise or i need to set bouncetime to prevent calling callback multiple times? – Lalit Verma 38 mins ago
  • I don’t know how you are managing to run that code. That version of the Python module was deprecated five years ago. What does pigs pigpv report? – joan 25 mins ago
  • @Lalit Verma, Yes, you might like to check out the below tutorial for newbies. It starts with a loop program example, showing the bouncing problem which causes fake pulse counting. Then it explains how to use the interrupt based/event driven programming approach to solve the problem. (Software debouncing solution is not described in this tutorial though.) raspberrypihq.com/use-a-push-button-with-raspberry-pi-gpio – tlfong01– tlfong01 7 mins ago    
  • @Lalit Verma, software debouncing, however, can be very simple but not very reliable, or very complicated, for sure not for the faint of heart: embedded.com/electronics-blogs/break-points/4024981/…. This tutorial describes how to cure bouncing and also EMI noise in one go, for your noisy motor. 🙂 – tlfong01 4 mins ago  Edit   


You are using a long deprecated version of the pigpio Python module. Where did you find it?

I suggest you download the latest version of pigpio from the Raspbian repositories.

Try this code.

!/usr/bin/env python

import time

import pigpio # http://abyz.me.uk/rpi/pigpio/python.html

pi = pigpio.pi()
if not pi.connected:

cb = pi.callback(4)

while True:

Categories: Uncategorized

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: