I'm working on a production counter that would count the material as it would hit the switch and that would be counted as 1. The issue I am having is that the code I wrote is not doing anything when I click run.

Essentially what I would want the code to do is count as the microswitch gets hit with a time interval of 10 seconds per click. I currently have it set at ms I have the hardware set up where I have... NO connected to the GPIO 17 Common to 3V3

import time
import pigpio

#setup vars
gpio = 17 #where the switch is connected
debounce = 1000 #debounce time, in us
clickPeriod = 5000 #control period for clicks. in ms. 
clickCount = -1 #number of clicks in the period

def intCallback(g, level, tick):
     global clickCount
     if level == pigpio.HIGH:
    #button release
           if clickCount == -1:
        #this is the starting click. Setting up watchdog
               pi.set_watchdog(g, clickPeriod)
           clickCount += 1
     elif level == pigpio.TIMEOUT:
         #kill watchdog and print result
               pi.set_watchdog(g, 0)
               print("Total clicks in {}ms period: {}".format(clickPeriod, clickCount))
               clickCount = -1

pi = pigpio.pi()
pi.set_mode(gpio, pigpio.INPUT)
pi.set_glitch_filter(gpio, debounce)
pi.set_pull_up_down(gpio, pigpio.PUD_UP) #this depends on how the switch is connected. In this case it is between GPIO and GND
cb = pi.callback(gpio, pigpio.RISING_EDGE, intCallback)

   while True:
 except (KeyboardInterrupt, SystemExit) as e:
    print("Clean exit")

except Exception as e:
   print("Bad exit")


enter image description here enter image description here enter image description here

  • Post a photo of your wiring.
    – CoderMike
    Commented Dec 27, 2021 at 22:34
  • Your description implies normally open microswitch connected between GPIO17 and 3V3 yet your code is pulling up the input - surely it should be pulled down?
    – CoderMike
    Commented Dec 27, 2021 at 22:58
  • Yes it should be a pulled down. Ive posted pics
    – Kevinh2
    Commented Dec 27, 2021 at 23:32

1 Answer 1


As others have pointed out you are using PUD_UP but state "NO connected to the GPIO 17 Common to 3V3". Connect to Gnd

NOTE is is poor practice to connect a switch to 3.3V. Those who do this sooner or later end up with dead devices, particularly if they use loose exposed wiring.

If you are going to use Physical Computing you need to learn the art of debugging hardware and software independently.

It is unclear WHY you appear to have a resistor of indeterminate value in series with your switch. (It is often a good idea to have a series resistor on GPIO input, but this requires a separate pullup.)

  • So I've changed the Common to GND and used the code I created to test the switch and the switch doesn't respond now while in the 3V3 it responds. I have 10k ohm resistor connected to it. Keep in mind I'm a 2nd year engineering studying Computer engineering and still a beginner in software and hardware applications and creating this project to learn....
    – Kevinh2
    Commented Dec 28, 2021 at 16:02

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.