Hello everyone,
How can I use interrupts on more than two digital pins? For me it only works on pins D2 and D3.
Use a different microcontroller or describe in detail what you want to achieve.
Please see here to get some proposals:
https://docs.arduino.cc/language-reference/en/functions/external-interrupts/attachInterrupt/
Futhermore you can ask yourself:
- Why do I need an external interrupt at all?
see the Digital Pins With Interrupts section under attachInterrupt() which lists the available pins on various boards
The official documentation states:
https://docs.arduino.cc/tutorials/uno-r4-wifi/cheat-sheet/#pins
https://docs.arduino.cc/tutorials/uno-r4-minima/cheat-sheet/#pins
| Pin | Type | Function |
|---|---|---|
| D0 | Digital | UART Receive |
| D1 | Digital | UART Transmit |
| D2 | Digital | GPIO pin, Interrupt |
| D3 | Digital | GPIO pin, Interrupt, PWM |
| D4 | Digital | GPIO pin |
| D5 | Digital | GPIO pin, PWM |
| D6 | Digital | GPIO pin, PWM |
| D7 | Digital | GPIO pin |
| D8 | Digital | GPIO pin |
| D9 | Digital | GPIO pin, PWM |
| D10 | Digital | SPI (CS), GPIO pin, PWM |
| D11 | Digital | SPI (CIPO), GPIO pin, PWM |
| D12 | Digital | SPI (COPI), GPIO pin |
| D13 | Digital | SPI (SCK), GPIO pin, Built-in LED |
| A0 | Analog | Analog In, DAC |
| A1 | Analog | Analog In, OPAMP + |
| A2 | Analog | Analog In, OPAMP - |
| A3 | Analog | Analog In, OPAMP OUT |
| A4 | Analog | Analog In, SDA* |
| A5 | Analog | Analog In, SCL* |
However, looking at the pin layout diagram, there are other pins that have IRQs assigned to them, so I think you may be able to use them by setting the registers directly.
Thanks for your answer,
I would like to use a rotary encoder with its button and also use 3 more buttons.
Why do you think that you need to use interrupts for the buttons ?
Because I don't want to put the pin listening in the loop() function
Why not?
A digitalRead for 3 buttons will take nearly no time.
i've found that not using interrupts for an encoder will often miss states changes when turning an encoder by hand when there is a lot of other foreground processing
Interrupts are a Catch 22 for newbies. You shouldn't use them unless you know what you're doing, but you’ll never learn that until you try and get burned at least once.
Additionally, as already pointed out, using interrupts for buttons is almost always the wrong way to go.
Are saying that nothing happening in the interrupt will have any effect on your code in loop()?
The loop function should perform the activity required by a button push, regardless of whether an interrupt is used.