Good day.

I have a specific project need and I spent the past couple of days searching on forum, blogs, etc and I'm still not clear if that's feasible to do with Arduino or not.

I need to monitor the state of ~50 digital inputs and trigger an action when any of the pin changes either from 0 to 1 or from 1 to 0, pretty much the same as pin state interrupt works, only difference is that I need to "listen" to ~50 pins, instead of just a couple. I must be able to identify what pin triggered the action and what was the change (0->1 or 1->0).

My initial idea is to use a MEGA 2560 and have a group of 50 "if" statements on the main loop comparing the previous state or each pin (stored in individual variables) with the current one and triggering the corresponding action if something changed.

I'd like to hear from the experts here what would be a more elegant or smarter/faster way to accomplishing this.

I'm eager to receive your feedback.

Thank you.

  • 50 ifs? define a constant array of pin number and a for loop over it. and define an array of states to remember the last state.
    – Juraj
    Oct 9, 2018 at 5:01
  • 1
    One alternative is to use daisy-chained parallel-in, serial-out shift registers such as 74HC165. Those parts can be bought for less than 30 cents each from ebay or aliexpress.
    – ex-punctis
    Oct 9, 2018 at 5:30
  • 1
    I/O expanders, for example the pcf8574 have an interrupt. You would only have to wait for an interrupt, and then request via i2c which pin has changed.
    – Jot
    Oct 9, 2018 at 7:28

3 Answers 3


I had a project that used 16 rotary encoders and I used the MUX Shield which uses 74HC4067 analog multiplexers. They port to A0 - A3 on the shield which is fine as you can set these pins to Digital Inputs else you can get teh breakout version and connect to any input pins. I also used a Due since ALL pins can be interrupt enabled. Reading was a bit of a pain but worked like this... The method is.. Continually address each input in turn in the loop or an interrupt is not detected, when an interrupt fires, address each input in turn again to find out which pin changed state. Hope that helps.


The Mega 2560 appears to have 24 pin change interrupts (I haven't checked if they're all broken out on the Mega Arduino board).

A combination of as many pin change interrupts and 'manual' scanning could be quicker than scanning them all individually.

Another option would be to group as many as possible onto the same ports and use direct hardware access to looks for changes- with this you'd be able to do 8 inputs at a time. With 8 inputs on each port, you just need to have seven main checks that the port value has changed and then jump off to a handler that deals with what specific pin has changed and what action is needed.


See answers in Good solution for more interrupts on an Arduino. E.g.:

Answer lhballoti:

You can use an I/O expander, for example an MCP23008 and MCP23017. They are available in SPI and I2c. Interrupts can be configured (edge-sensitive, level-sensitive) for each input.

Answer embedded.kyle:

You could use a Programmable Interrupt Controller (PIC) such as the 82C59A. It will cascade if you need more than the eight interrupts it provides. It's a bit old school but I think it will handle all of your requirements.

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.