0

Morning,

I'm a newb to this, so suspect i'm just doing something silly, or don't quite understand what i have written. Either way... here we go.

I have a Pi with an IO Pi Plus board attached (https://www.abelectronics.co.uk/p/54/io-pi-plus) and am attempting to utilise all the pins in Bus 1 to interface with some buttons i have wired up, from pin to ground.

from __future__ import absolute_import, division, print_function, \
                                                    unicode_literals
import time

try:
    from IOPi import IOPi
except ImportError:
    print("Failed to import IOPi from python system path")
    print("Importing from parent folder instead")
    try:
        import sys
        sys.path.append('..')
        from IOPi import IOPi
    except ImportError:
        raise ImportError(
            "Failed to import library from parent folder")


def main():
    '''
    Main program function
    '''

    busin = IOPi(0x20)

    # Set port 0 on the busin bus to be inputs with internal pull-ups enabled.
    busin.set_port_pullups(0, 0xFF)
    busin.set_port_direction(0, 0xFF)
    # Invert the port so pins will show 1 when grounded
    busin.invert_port(0, 0xFF)
    # Set the interrupts default value for port 0 to 0x00 so the interrupt
    # will trigger when any pin registers as true
    busin.set_interrupt_defaults(0, 0x00)
    # Set the interrupt type to be 1 on each pin for port 0 so an interrupt is
    # fired when the pin matches the default value
    busin.set_interrupt_type(0, 0xFF)
    # Enable interrupts for all pins on port 0
    busin.set_interrupt_on_port(0, 0xFF)
    # Reset the interrupts
    busin.reset_interrupts()

    while True:

        # read the interrupt status for each port.

        if (busin.read_interrupt_status(0) != 0):
            # If the status is not 0 then an interrupt has occured
            # on one of the pins so read the value from the interrupt capture
            value = busin.read_interrupt_capture(0)

            # write the value to port 0 on the busout bus
            print("button pressed: ",  value)
        # sleep 200ms before checking the pin again
        time.sleep(0.2)


if __name__ == "__main__":
    main()

This is the code i am running, now it seems that only pins 1 through 8 seem to give me a response, rather than pins 1 through 16 which is what i had expected. Wondered what i was doing wrong? sorry again, this is my first rodeo with python and the pi..

thanks

Andrew

4
  • I can see where you have set the 8 pins connected to port 0 to be inputs. I can see no code for the pins connected to port 1.
    – joan
    Nov 16, 2022 at 9:48
  • So which is the line that describes only 8 pins? as opposed to the 16 i'd like? This is a hacked example from here abelectronics.co.uk/kb/article/1087/… where I only care about buttons, not about the LED portion..
    – Andy
    Nov 16, 2022 at 13:05
  • At a guess there are 8 pins per bus. You access the other 8 pins from the other bus.
    – joan
    Nov 16, 2022 at 16:11
  • This appears to be expansion board based on a MCP23017 port expander. These are normally addressed as 2 8 bit ports but can be addressed as a 16 bit (which requires 2 I2C transfers). I have c code which does this and there are few other examples. Your real problem seems to be understanding the library you are using but this not really a Pi problem.
    – Milliways
    Nov 16, 2022 at 21:25

0

Your Answer

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

Browse other questions tagged or ask your own question.