1

I have some sample code (taken out of my larger program) that regularly (but not always) missing the first rising edge:

#!/usr/bin/python
import pigpio, time

def callback(g, l, t):
  print("Callback trigger "+ str(g)+ " " +str(l)+ " " +str(t))

pi = pigpio.pi()
if not pi.connected:  # Check connected
  print("Not connected to PIGPIO Daemon")
else:
  print("Connected to PIGPIO Daemon")
  pinStartGate = 20

  pi.set_mode(pinStartGate, pigpio.INPUT)
  pi.set_pull_up_down(pinStartGate, pigpio.PUD_DOWN)  
  pi.set_noise_filter(pinStartGate, 50000, 50000)  # Debounce switch
  # pi.set_glitch_filter(pinStartGate, 50000)  # Debounce switch

  cb1 = pi.callback(pinStartGate, pigpio.FALLING_EDGE, callback)


while(True):
  print("Direct trigger" + str(pi.read(pinStartGate)))
  time.sleep(1)

running this, with pin 20 initially not connected, and then connecting to 3.3v or 5v gives:

Direct trigger0
Direct trigger0
Direct trigger1
Direct trigger1
Direct trigger0
Direct trigger0
Callback trigger 20 0 2414139526
Direct trigger1
Direct trigger1
Direct trigger0
Direct trigger0
Callback trigger 20 0 2418808728
Direct trigger1
Direct trigger1

You can see that the first trigger, does not trigger the Callback.

I am also suprised by the way around that I need to use 'pigpio.FALLING_EDGE' as in electrical terms it is the Rising edge that I am triggering based upon (going from not connected, so pigpio.PUD_DOWN = 0v to externally connected to 3.3v or 5v?

Running Raspbian Stretch, Python 3.5.3 and pigpio v64

Thanks very much Kevin

  • Editing the code to set pigpio.PUD_UP, setting the trigger on a pigpio.RISING_EDGE, and pulling the pin down to Gnd has the same issue. It doesn't work reliably the first press of the button, but then works from then on. – Kevin W Apr 30 at 21:09
0

pigpio reports level changes via Python callbacks.

NEVER connect more than 3V3 to a Pi GPIO (you will eventually destroy the GPIO and then the Pi).

I would not use the noise filter for switch debounce. If you suffer from switch bounce use the glitch filter.

Your output does look odd. I suggest while testing you change the callback to either edge and print out pi.get_current_tick() in the direct read so we can see how the events are interleaved.

#!/usr/bin/python

import pigpio, time

def callback(g, l, t):
  print("Callback " + str(l) + " " + str(t))

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

pin = 20

pi.set_mode(pin, pigpio.INPUT)
pi.set_pull_up_down(pin, pigpio.PUD_DOWN)  

cb1 = pi.callback(pin, pigpio.EITHER_EDGE, callback)

while True:
  print("Direct " + str(pi.read(pin)) + " " + str(pi.get_current_tick()))
  time.sleep(1)

This gave the following output when I set GPIO 20 up/down with pigs in another terminal window.

Direct 0 3068153032
Direct 0 3069154220
Direct 0 3070156011
Callback 1 3070974455
Direct 1 3071157718
Direct 1 3072159517
Direct 1 3073161135
Direct 1 3074162919
Direct 1 3075164671
Direct 1 3076165956
Direct 1 3077167750
Callback 0 3077950413
Direct 0 3078169474
Direct 0 3079171260
Direct 0 3080173127
Direct 0 3081174929
Direct 0 3082176712
Direct 0 3083178524
Direct 0 3084180331
Callback 1 3084236329
Direct 1 3085182138
Direct 1 3086183936
Direct 1 3087185751
Direct 1 3088187540
Direct 1 3089189351
Callback 0 3089332637
Direct 0 3090191219
Direct 0 3091192091
  • Adding get_current_tick I get: 'Direct trigger 2028024685 1 Direct trigger 2029026990 1 Direct trigger 2030029240 0 Direct trigger 2031031419 0 Direct trigger 2032033671 0 Callback trigger 20 0 2032701268 Direct trigger 2033035978 1 Direct trigger 2034038260 1 Direct trigger 2035040632 1 Callback trigger 20 1 2035259794 Direct trigger 2036042891 0 Direct trigger 2037045130 0' – Kevin W May 1 at 13:56
  • Added code and output to answer. – joan May 1 at 14:31
  • Thanks Joan, I see it worked for you, not sure why it does not work for me. I have taken your exact code and changed two things - changed pin to 16, and changed PUD_DOWN to PUDUP as my hardware is currently wired to pull pin 16 to low based on the switch push and I still get the same error. Pigpio_version returns 64 and python version 2.7.13 or 3.5.3 make no difference. Output is: Direct 1 303628226 Direct 1 304630530 Direct 0 305632592 Direct 0 306634798 Direct 0 307637008 Callback 0 307952655 Direct 1 308639197 Direct 1 309641566 – Kevin W May 5 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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