Nano 3 external Interrupts

Hey guys. I'm new here so sorry if I ask a dumb question.
I need to connect 3 external interrupts from the Nano. I'm going to use D2, D3 and I would like to know what other pin I can use, as the Atmega328P all pins accept interrupts.

What are the interrupt sources? If they're polled, you can use one pin for multiple sources. Except for pin change interrupts, there are only two pins on the Nano that can be used with attachInterrupt().

Also - often people assume that they need interrupts, when in fact they don't.

Thank you "arrg" for your reply.
The 3 external pulses will never occur at the same time, so their priority can be the same. The falling edges of each pulse will be monitored. I'm opting for interruptions because I can't miss water liter count pulses under any circumstances.

How fast are the pulses? Interrupts are usually required where an instant response is required. That is different than a situation where you have relatively slow pulses and simply can't miss any. You can poll in loop() reliably as long as you don't put any delaying code in loop(), at kilohertz frequencies. Worst case, you can employ a timer to guarantee polling the pulses on a regular basis. That is how IR and 433MHz OOK drivers for the Arduino work. Nothing is ever missed there, and the frequency is much much higher than a water meter pulse train.

Either method would allow you to use as many pins as you like for pulse detection.

By the way, simultaneous transitions on two different interrupt pins will be recognized, regardless of the fact that there is no priority interrupt system on the processor. They just get handled back to back. So you are worrying about that for no reason.

1 Like

The frequency is actually quite low. On the order of 25 to 50Hz.
In this case you don't need the interruption, right? There are some pulse capture functions in Arduino, right? should I use them?

Your problem with using the existing pulse functions, is that they block while they input a pulse, hence can not receive pulses on different pins simultaneously. So you will need to make custom code. millis() or micros() can be used to time things.

Are you doing anything else in your sketch that halts processing in loop() for more than a few milliseconds?

Well, I'm looking to not use anything in Loop() that might block processing. There is only serial communication on 9600 with transmission of a few bytes. Based on what you're telling me, I'm going to try not to block Loop() for anything and use digitalRead to read port D2, D3 and D5 normally, without interruption. I'm going to try this out and see if pulse count losses occur. I believe they will not occur as the frequencies are too low. I take opportunity to thank you for your help. It was valuable a lot. Thanks.

It sounds like you're on the right track. Don't forget, you will need to do some minimal signal processing on the inputs (basically, edge detection and debounce). You can look at switch input examples to get some ideas about that.

Thanks for the tip! As the sensors are hall effect and not reed swtich, I believe there will be no need to debounce. As for edge detection, yes I will need these features. I need to research how to do this. When it was the case of interruption it was clear to me how to do this, but now I don't know how to proceed.

I believe the D2 and D3 external interrupts will trigger on the falling edge, but all the others, including D5, are only pin change interrupts. So D5 will still work, but will need to be coded differently unless there's a library that does all that for you.

Thanks ShermanP. Yes I believe it is possible for me to use interrupts in D5, as the Atmega328P allows interrupts on practically all pins, however I don't have experience to deal with this, especially if the compiler doesn't have a library for this. Because of that, I'll try to work without interruptions, keeping my Loop() without delays, and let's see what happens...

There are several libraries for pin change interrupts available through the library manager.

The one by Nico Hood PinChangeInterrupt.h is very easy because it uses the same syntax as the external interrupts.

"GreyGnome"'s Enable Interrupt is also frequently used.

Thanks Cattledog. Like I said I'm new on the block. I will be verifying your indications.

The switch examples that come with the IDE are examples of edge detection, state change detection in general...

Thanks arrg. I have enough material now to work, learn and research. I really appreciate the collaboration of all of you, and I must confess that I was impressed with the attention given by everyone. Thank you so much. I will inform you of the results soon.