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.

I am using Proximity Sensor for counting rotations.


import time

import pigpio


pi = pigpio.pi()

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

wind_cb = pi.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

I want to count pulses for two conditions:

(i) One for all rotations from the beginning to End of program

(ii) Count Pulse based on other Pin Status Condition for example start counting if PIN A is HIGH.

How to achieve above mentioned condition using PIGPIO module

  • 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
    Commented Aug 26, 2019 at 8:47
  • 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? Commented Aug 26, 2019 at 8:49
  • 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
    Commented Aug 26, 2019 at 9:02
  • 1
    @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
    Commented Aug 26, 2019 at 9:20
  • @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
    Commented Aug 26, 2019 at 9:23

1 Answer 1


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:
  • Yes i changed that pigpio.start and all that before testing tht code... Didnt use deprecated version Commented Aug 26, 2019 at 11:45
  • I think you need to edit your question and include clear photos of the connections between the Pi and the sensor. I have used Hall sensors to count motor shaft rotations and had no problems in the 9k+ RPM region.
    – joan
    Commented Aug 26, 2019 at 11:48
  • The above is also the one you provided on raspberry pi forums, it gave random count when i tested manuaaly with pins but gave correct count with 1-2 bounce when used with pnp no proximity sensor Commented Aug 26, 2019 at 11:50
  • @LalitVerma I have told you what you need to do. I can provide no further help.
    – joan
    Commented Aug 26, 2019 at 11:56
  • Updated questiin @joan Commented Aug 28, 2019 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.