Newb alert !

In the C API doco for gpioSetAlertFunc there is a comment "Do not use gpioRead. ".

Does this mean for the GPIO trigger that caused to callback to be called ? That makes sense as the data is already in the parameters, and pin state may have subsequently changed. Or does it mean don't use it at all (specifically I need to read other GPIO pins) ?

I am restoring an old computer control panel that has 24 lamps, 7 buttons, and 22 switches. There are a few complicated use cases, in particular when the RUN button is pressed, you need to read the bank of "instruction" switches (all 18 of them).

I did try an experiment of this in Python, with a button and a few switches wired to GPIO pins, but the gpioRead (of the switches) didn't appear to work, I totally accept that a wiring error on my part may be the most likely cause.

I've since moved on and am implementing in C now as there are a lot of libraries to call (for the panel operations), and want to be sure what is the best approach.

Should I exit the callback as soon as possible, and say set a Global button-pressed variable and then inside a main loop State Machine read the 18 switches (now on I2C) ? or is there a way I can do this within the callback function (mainly to simplify the logic of my program) ?

(I have read the pigpio doco, examples and posts here)

1 Answer 1


To make sure you have a consistent switch state have all the GPIO you are interested in generate a callback.


GPIO_of_interest = [2, 3, 4, 5, 7, 11, 12, 15]
GPIO_level = [0]*32

def my_callback(GPIO, level, tick):
   GPIO_level[GPIO] = level
   # now do anything else needed for that GPIO

# set up callback for all GPIO I want to read

for g in GPIO_of_interest:
   pi.callback(g, pigpio.EITHER_EDGE, my_callback)

# set the initial level of each GPIO I want to read

init_level = pi.read_bank_1()
for i in range(32):
   if init_level & (1<<i):
      GPIO_level[i] = 1
  • Thanks Joan, I think I can translate that to C.
    – jalapeno
    Sep 29, 2021 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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