2

I have a PI setup with 3 sensors (reed switches) and 2 "outputs" to a relay. They are on pins (8, 10, 12)

I have pin 3 setup to drive the first relay and pin 5 setup to drive the second relay.

I register GPIO.RISING and a callback on the input pins, and when I "pulse" the output pin (3 or 5) I get all 3 sensor methods called, instead of just 1.

These are motorized door switches and contacts, so when I "pulse" the button one door opens (and one contact should change only). I see this:

  • all contacts are state 0
  • pulse input 3
  • all 3 callbacks are called
  • contacts 10 and 12 are 0 and contact 8 is 1 - indicating the correct state - one door open

My issue is that all the callbacks are called. Why? I tried not using the same function (creating 3 different functions) and I still get the same results.

Any ideas why?

Here's the code to read the pins and the callback functions:

#!/usr/bin/python3

import time
import sys
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)

channels = [(8, GPIO.PUD_UP), (10, GPIO.PUD_UP), (12, GPIO.PUD_UP)]

def my_callback8(channel):
    print('Edge detected on channel %s'%channel)
    print('State is %s' %(GPIO.input(channel)))
    print('\n\n')

def my_callback10(channel):
    print('Edge detected on channel %s'%channel)
    print('State is %s' %(GPIO.input(channel)))
    print('\n\n')

def my_callback12(channel):
    print('Edge detected on channel %s'%channel)
    print('State is %s' %(GPIO.input(channel)))
    print('\n\n')

for ct in channels:
    (channel, pull) = ct
    GPIO.setup(channel, GPIO.IN, pull)
    GPIO.add_event_detect(channel, GPIO.RISING, bouncetime=200)
    if channel == 8:
        GPIO.add_event_callback(channel, my_callback8)
    elif channel == 10:
                GPIO.add_event_callback(channel, my_callback10)
    elif channel == 12:
                GPIO.add_event_callback(channel, my_callback12)
    print("Channel %d setup" %(channel))

while True:
    print("Waiting")
    for ct in channels:
        (channel, pull) = ct
        print("Channel %d is %s" %(channel, GPIO.input(channel)))
    time.sleep(15)

Here is the output on the button press:

Waiting
Channel 8 is 0
Channel 10 is 0
Channel 12 is 0
## HERE IS THE OUTPUT PULSE TO PIN 3 - RELAY PULSES AND DOOR OPENS ##
Edge detected on channel 8
State is 0



Edge detected on channel 12
State is 0



Edge detected on channel 8
State is 0



Edge detected on channel 8
State is 1



Waiting
Channel 8 is 1
Channel 10 is 0
Channel 12 is 0

Your Answer

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

Browse other questions tagged or ask your own question.