2

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

3
  • 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, 2019 at 21:09
  • I have run into the exact same issue - When I first run this program (mine is very similar to yours as I'm still testing my code) the first falling edge does not get detected. After that everything works as expected. After re-starting the Python script the first falling edge gets detected without fail. I am able to consistenly reproduce the issue by manually bringing the GPIO pin LOW and HIGH and LOW again (using pigs). before re-starting the script. Have you found a solution or work-around for this issue?
    – QNimbus
    Sep 16, 2020 at 20:05
  • Maybe worth mentioning I'm using a Raspberry Pi Zero
    – QNimbus
    Sep 16, 2020 at 20:09

1 Answer 1

1

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
4
  • 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, 2019 at 13:56
  • Added code and output to answer.
    – joan
    May 1, 2019 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, 2019 at 20:38
  • I've run into this issue as well - it seems that someone created a github issue (github.com/joan2937/pigpio/issues/359) addressing this exact problem. Performing a 100microsecond trigger on the GPIO at the start of my script seems to work as a workaround. pi.gpio_trigger()
    – QNimbus
    Sep 16, 2020 at 20:40

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.