GPIO vs. I2C- another N00B question/ I2C Interrupt Monitoring?

Hi guys, on to my second RpI project…I purchased an 8-channel I2C relay board (https://store.ncd.io/product/8-channel-general-purpose-spdt-relay-controller-with-i2c-interface/?attribute_pa_relay-amperage=5-amp-spdt&attribute_pa_pluggable-connectors=add-pluggable-connectors ) to build a budget-scale replacement for a hardware PLC that we end up having to replace every few years due to hardware failure. Figure the time is right to put together some python code so we’ll only need $150 worth of hardware instead of $1500…

Anyhow, the first thing I need to figure out is how to simulate the interrupt-driven nature of PLCs. I see that there is a GPIO library that does that very thing. Intro here: https://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio .

BUT my lack of experience with the hardware layers has me stalled getting out of the gate. Will the GPIO library communicate with the I2C board so I can follow the examples in that tutorial? If not, is there some sort of similar functionality in I2C? Or, is there a different GPIO board that I should use that can leverage those libraries?

Guess I should give a brief description of what’s going down: It’s a siren control panel for my volunteer firehouse. When we are dispatched to a call, we have a receiver that has a programmable relay that closes (set for 1 second right now, but it can do whatever). When that happens, a sequence of events starts: A claxon horn in the firehouse sounds for 3 seconds; the roof siren sound for one minute (with the on/off cycles within that minute dictated by the PLC); the lights in the engine room turn on for a pre-programmed interval; and the door switch is unlocked, also for a pre-programmed interval. There are also some manual controls on the control box: turn on all the lights; unlock the door; sound the siren; or start the entire sequence as if a call is received. So at least 4 inputs to watch for, while performing any and all actions when needed.

Thanks!

Hi, you are on the right path…
You will have to combine the GPIO Library with the I2C Code/Library. At a basic level, you can poll the INT Pin using the GPIO Library (as shown in the link you shared). When you detect an interrupt, you can use the I2C Library(that uses the I2C interface of the Rpi) to read the Interrupt register of the MCP23008 to determine the source of the Interrupt and then take the appropriate action.

1 Like

Excellent thanks Talha! I will start coding and hopefully I won’t have too many more questions!

You are always welcome to ask, good luck!

GPIO doesn’t like me calling the i2c pins (neither 27 nor 28). I did setmode.board instead of bcm in the sample project because obviously the i2c pins have no GPIO number! I can’t find any examples of GPIO using the I2C hardware, so I’m stumped again!!!

to expand on that:

pi@raspberrypi:~/plc $ python pullup.py
Traceback (most recent call last):
File “pullup.py”, line 7, in
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_UP)
ValueError: The channel sent is invalid on a Raspberry Pi

It also just dawned on me that I’m using the wrong board for input notification. A relay cannot see and report circuit closure, can it? Maybe what I need to do is get a pass-thru terminal block and use raw GPIO for my inputs and the I2C relays for my outputs?

My bad I didn’t explain it correctly. I thought it was a 4-channel one with 4 free contacts for Inputs.
You are right, you cannot use the MCP Pins in Input configuration since they are connected to the relays.

Yes you can use the same GPIO code, RPi GPIOs can be used for the 4 Inputs whereas, I2C will be used to communicate with MCP and set the Relays. So if you were polling the INT pin on a RPi GPIO pin, now you will be polling/adding events for 4 GPIO pins.

Thanks again Talha! Do you know of any board that will give me both I²C relays as well as screw terminals for the GPIO pins? That would avoid me having to stack an additional board in my finished product. I already have to use the I2C shield (with remote control, now that I learned of its existence!), a GPIO screw terminal board, and the relay board. Don’t know how I’m going to package this all in one enclosure!

You can look into this one


this one has 4 relays and 4 Digital IO.

Thanks

AH, that looks perfect, thanks Bhaskar! Weird that there’s nothing in the range between 4 and 24 relays, though. Right now I only require four, but was thinking of building in some expansion capabilities…thanks again!