1

I have a device which is based on Raspberry pi. It is known as ionoPi which is basically an i/o expansion board for Raspberry pi. I am writing a python script which detect signal change on input 16 and doing it using interrupts. Below is the code:

import RPi.GPIO as GPIO

def di1(pin):
    di1_status = (GPIO.input(pin))
    print("Status of {} is {}".format(pin ,di1_status))

def main():
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(16, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)

   GPIO.add_event_detect(16, GPIO.BOTH, callback=di1, bouncetime=500)

   while True:
       time.sleep(0.5)

main()

ionoPi board has been powered by 24v power supply. I have a wire which I use to toggle state of di1 which is input 16. To test it continuously, I have attached a button between di1(ipnut 16) and 24v. I have tested the button which works fine. But the code seems to be showing wrong values for the input, refer below image:

enter image description here

so it shows 1,0,1,0,1,0... which is fine and then suddenly it starts showing 0,0,0,0,0,.... I also have attached multimeter which shows that state is changing perfectly fine but somehow this code is showing wrong values. Can anyone please suggest what I am doing wrong here. Thanks.

1

There are a couple of possibilities.

  1. button bounce
  2. delayed read

In your case a combination of both is the most likely problem.

Mechanical switches quite often make and break contact several times as they are pressed and released. This will be seen as a series of alternating lows and highs. These level changes may be only a few microseconds long and a few microseconds apart. You need an oscilloscope or similar to see them (a multimeter is an averaging device and useless at these speeds).

The delayed read is because Linux notices the interrupt and then arranges for your Python script to continue running. The GPIO.input call will happen at least 50µs after the interrupt and perhaps several milliseconds later. The level may have changed multiple times between the initial interrupt and the GPIO.input call (which reads the current level).

piscope will show and capture the actual level changes as will this script when run in a terminal window (both require the pigpio daemon to be running, sudo pigpiod).

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.