Two switches on a single external interrupt? - Arduino Pro Mini

Hello, I'm pretty new to electronics and loving my arduino journey. I'm currently exploring the external interrupt input pins on an arduino pro mini. I understand these are digital pins 2 and 3 which I plan on using both for a project.

I am planning on having three external inputs to wake the board. I will have a PIR sensor to trigger pin 3 and want two switches to trigger pin 2. The issue I'm facing is that depending on which switch I trigger I want specific code to run on wake up. What would be the best way to achieve this on a pro mini?

I'm thinking of using two more additional digital inputs for pin 2 and somehow isolating them but I don't know if I'm on the right track.

I have a attached a basic schematic of my thinking. I'm not too familiar with diodes. Could something like this work?

D1,D2,D4 are all held HIGH

When switch1 is pressed -> D2 == LOW && D1 == LOW
When switch2 is pressed -> D2 == LOW && D4 == LOW

Hopefully you guys can see the general idea. Any advice would be greatly appreciated. Thank you in advance.

the real first question is "do you need interrupts at all?"

You have indeed the two external interrupts on pin 2 and 3 but your arduino pro mini supports also pin change interrupt, so you could use 3 pins, one for each switch

Nothing in what you have said suggests you need an interrupt for the inputs. There are a number of tutorials that help with this, here are the ones I usually suggest but there are others:

See Demonstration for several things at the same time for how to implement a state machine in software rather than in a restaurant.
Also look at Using millis for timing, which is also relevant as frequently millis timing and a state machine are used together.

Without knowing what you are really trying to do I can't help any more than that.

Yes, your diodes will work. But pick a replacement for D1. D0 and D1 are the serial Tx and Rx pins, and it's best to leave them alone if you can. Configure all three pins as INPUT_PULLUP, and make the interrupt FALLING. Then of course you need to consider switch bouncing which would cause multiple interrupts from the same button press.

Also note that you really only need D2 plus one other pin and diode. Connect D2 to one switch, and D4 to the other switch, plus a diode from D2 to D4. If the interrupt triggers but D4 is still high, then it was the D2 switch that triggered the interrupt. Unless D4 is bouncing.

I believe the operative word here is "wake".

There is indeed, no other reason to use an interrupt. Once woken, you just poll the pins.

Hi ShermanP,

Thank you! Yes I'll only use D2 and D4 and ditch D1 - totally makes sense. The circuit works with a 1N4004 diode. Would this diode be a bit overkill for this purpose? What sort of diode would be best?

Thanks again :slight_smile:

Well, as a power rectifier it certainly is "overkill" as a common 1N914/ 1N4148 will do the job just fine.

Bit the 1N4004 will certainly work. The only real difference in performance may be that it has a higher capacitance, but not enough to cause trouble,

As Paul_B said, traditionally you would use a "signal diode" like the 1N4148 for this.

But again I suggest you consider how you might deal with switch bounce. Mechanical switches bounce. It can be dealt with in hardware or software or both. There are a number of helpful Youtube videos on "switch bounce".

And some not so helpful ones.

The take-home message should be - you are using a microcontroller - as such, bounce should be dealt with in software, and should not use interrupts.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.