Iam trying to create the error detection application for my battery charger prototype using Rpi.GPIO. I successfully created the add event detect for multiple GPIO pins (totally 8 pins for each error).

My doubt is regarding the assignment of priority for each error. I try to run the call back function depends on the priority list below but unsuccessful. If i give emergency stop signal after earth missing it is not detected. Emergency stop signal gets detected only after earth missing signal. I listed the error according to the priority below.

  1. Emergency stop
  2. Phase reversal
  3. Earth Leakage
  4. Earth Missing
  5. Short circuit
  6. power failure
  7. Input over voltage
  8. Input under voltage

I attached the example code below

import RPi.GPIO as GPIO
from time import sleep

GPIO.setmode(GPIO.BOARD) # We are accessing GPIOs according to their physical location
GPIO.setup(37, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(35, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(33, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(31, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(29, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(40, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(38, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(36, GPIO.IN, pull_up_down=GPIO.PUD_UP)

def errorDetection(channel):
    if(channel == 37):
        while GPIO.input(37) == 0:
            print('Emergency stop occured')
    elif(channel ==  35):
        while GPIO.input(35) == 0:
            print('Phase reverse occured')
    elif(channel == 33):
        while GPIO.input(33) == 0:
            print('Earth leakage occured')
    elif(channel == 31):
        while GPIO.input(31) == 0:
            print('Earth Missing occured')
    elif(channel == 29):
        while GPIO.input(29) == 0:
            print('Short circuit occured')
    elif(channel == 40):
        while GPIO.input(40) == 0:
            print('Power failure occured')
    elif(channel == 38):
        while GPIO.input(38) == 0:
            print('Input over voltage occured')
    elif(channel == 36):
        while GPIO.input(36) == 0:
            print('Input under voltage occured')
GPIO.add_event_detect(37, GPIO.FALLING, errorDetection, bouncetime=2000)
GPIO.add_event_detect(35, GPIO.FALLING, errorDetection, bouncetime=2000)
GPIO.add_event_detect(33, GPIO.FALLING, errorDetection, bouncetime=2000)
GPIO.add_event_detect(31, GPIO.FALLING, errorDetection, bouncetime=2000)
GPIO.add_event_detect(29, GPIO.FALLING, errorDetection, bouncetime=2000)
GPIO.add_event_detect(40, GPIO.FALLING, errorDetection, bouncetime=2000)
GPIO.add_event_detect(38, GPIO.FALLING, errorDetection, bouncetime=2000)
GPIO.add_event_detect(36, GPIO.FALLING, errorDetection, bouncetime=2000)

i = 0;
while i <= 10:
    print("i value",i)
    i = i+1
    if i == 10:
        i = 0
  • 1
    While joan's comments are relevant WHY would you use a single callback function rather than callbacks for each event? Then you don't need any if or other tests.
    – Milliways
    Sep 2, 2021 at 9:48
  • I think i used a single call back function errorDetection for multiple GPIO add events. correct me if iam wrong
    – V_J viji
    Sep 2, 2021 at 10:01
  • 1
    I suggest you try gpiozero which is much simpler than RPi.GPIO and has many excellent examples.
    – Milliways
    Sep 2, 2021 at 10:49

1 Answer 1


You have a while loop for each condition. That means the program flow stops at that position until the condition is removed.

I suggest you change those while to if. That should do what you seem to want.

E.g. rather than

while GPIO.input(37) == 0:


if GPIO.input(37) == 0:

  • i tried using if statement also but the execution immediately return to main while loop even though my input signal is low. My doubt how to make the execution to stay in the within if statement until the GPIO value becomes high again.
    – V_J viji
    Sep 2, 2021 at 9:57
  • This has become just an ordinary programming question. You need to sit down and think about what you are trying to achieve. Personally I would detect rising and falling edges and clear/set a flag for each trigger.
    – joan
    Sep 2, 2021 at 12:55

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.