Skip to content

Commit b0f2ef1

Browse files
authored
Merge cfd8995 into 89fd1a0
2 parents 89fd1a0 + cfd8995 commit b0f2ef1

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed
818 KB
Loading
432 KB
Loading

content/hardware/03.nano/boards/nano-r4/tutorials/01.user-manual/content.md

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2290,6 +2290,137 @@ When working with HID on the Nano R4, there are several key points to keep in mi
22902290
- The Nano R4 can function as both a keyboard and mouse simultaneously, allowing for complex automation sequences that combine typing, shortcuts and mouse control.
22912291
- Remember that different operating systems may have slightly different keyboard layouts and shortcuts, so test your HID projects on your target platform to ensure compatibility.
22922292

2293+
## External Interrupts
2294+
2295+
The Nano R4 features external interrupt capability through the RA4M1 microcontroller's ICU (Interrupt Control Unit). Interrupts allow your Nano R4 board to immediately respond to pin state changes by temporarily pausing the main program to execute an Interrupt Service Routine (ISR), then returning to where it left off. This makes interrupts essential for time-critical applications like button detection, encoder reading, and sensor monitoring.
2296+
2297+
### Interrupt Specifications
2298+
2299+
The Nano R4 board's external interrupt capabilities offer the following technical specifications:
2300+
2301+
| **Parameter** | **Value** | **Notes** |
2302+
|:----------------------:|:---------:|:----------------------------------------:|
2303+
| Hardware Channels | 9 | Channels 0-7, 9 available |
2304+
| Interrupt-capable Pins | 13 pins | 7 digital + 6 analog pins |
2305+
| Trigger Modes | 4 | `RISING`, `FALLING`, `CHANGE`, and `LOW` |
2306+
| Response Time | < 1 μs | Typical interrupt latency |
2307+
2308+
### Interrupt-Capable Pins
2309+
2310+
The following pins support external interrupts on the Nano R4 board:
2311+
2312+
| **Arduino Pin** | **Interrupt Channel** | **Primary Function** | **Notes** |
2313+
|:---------------:|:---------------------:|:--------------------:|:--------------------------:|
2314+
| `D0` | Channel 6 | Digital I/O | Shares channel with `A1` |
2315+
| `D1` | Channel 5 | Digital I/O | Dedicated channel |
2316+
| `D2` | Channel 0 | Digital I/O | Recommended for interrupts |
2317+
| `D3` | Channel 1 | Digital I/O, PWM | Shares channel with `A4` |
2318+
| `D8` | Channel 9 | Digital I/O | Dedicated channel |
2319+
| `D12` | Channel 3 | Digital I/O, MISO | Shares channel with `A6` |
2320+
| `D13` | Channel 4 | Digital I/O, SCK | Dedicated channel |
2321+
| `A1` | Channel 6 | Analog, OPAMP+ | Shares channel with `D0` |
2322+
| `A2` | Channel 7 | Analog, OPAMP- | Dedicated channel |
2323+
| `A3` | Channel 2 | Analog, OPAMP OUT | Shares channel with `A5` |
2324+
| `A4` | Channel 1 | Analog, I²C SDA | Shares channel with `D3` |
2325+
| `A5` | Channel 2 | Analog, I²C SCL | Shares channel with `A3` |
2326+
| `A6` | Channel 3 | Analog input | Shares channel with `D12` |
2327+
2328+
***__Important note__: Pins sharing the same interrupt channel cannot be used for interrupts simultaneously. For example, `D3` and `A4` both use channel 1, so only one can be configured for interrupt functionality at a time.***
2329+
2330+
### Interrupt Trigger Modes
2331+
2332+
The Nano R4 board supports four interrupt trigger modes:
2333+
2334+
| **Mode** | **Trigger Condition** | **Typical Use Cases** |
2335+
|:---------:|:------------------------------------:|:----------------------------------:|
2336+
| `RISING` | Pin transitions from `LOW` to `HIGH` | Button press detection (pull-down) |
2337+
| `FALLING` | Pin transitions from `HIGH` to `LOW` | Button press detection (pull-up) |
2338+
| `CHANGE` | Pin changes state (either direction) | Encoder reading, pulse counting |
2339+
| `LOW` | Pin remains at `LOW` level | Level-triggered events |
2340+
2341+
***__Important note__: The `HIGH` trigger mode is not supported by the hardware. If specified, it will behave as `RISING` mode (detecting only the `LOW`-to-`HIGH` transition, not the continuous `HIGH` state). For continuous `HIGH` level detection, use polling with `digitalRead()` instead.***
2342+
2343+
You can attach interrupts using the dedicated Arduino functions:
2344+
2345+
```arduino
2346+
attachInterrupt(digitalPinToInterrupt(pin), ISR_function, mode);
2347+
detachInterrupt(digitalPinToInterrupt(pin));
2348+
```
2349+
2350+
The following example demonstrates basic interrupt usage:
2351+
2352+
```arduino
2353+
/**
2354+
External Interrupt Example for the Arduino Nano R4 Board
2355+
Name: nano_r4_interrupt_example.ino
2356+
Purpose: This sketch demonstrates how to use external interrupts
2357+
to detect button presses.
2358+
2359+
@author Arduino Product Experience Team
2360+
@version 1.0 01/06/25
2361+
*/
2362+
2363+
const int buttonPin = 2;
2364+
const int ledPin = LED_BUILTIN;
2365+
2366+
// Variables shared with ISR must be volatile
2367+
volatile bool buttonPressed = false;
2368+
volatile unsigned long pressCount = 0;
2369+
2370+
// Interrupt Service Routine - keep it short!
2371+
void buttonISR() {
2372+
buttonPressed = true;
2373+
pressCount++;
2374+
}
2375+
2376+
void setup() {
2377+
// Initialize serial communication and wait up to 2.5 seconds for a connection
2378+
Serial.begin(115200);
2379+
for (auto startNow = millis() + 2500; !Serial && millis() < startNow; delay(500));
2380+
2381+
pinMode(buttonPin, INPUT_PULLUP);
2382+
pinMode(ledPin, OUTPUT);
2383+
2384+
// Attach interrupt to button pin
2385+
attachInterrupt(digitalPinToInterrupt(buttonPin), buttonISR, FALLING);
2386+
2387+
Serial.println("- Arduino Nano R4 - External Interrupt Example started...");
2388+
Serial.println("- Press the button to trigger interrupts");
2389+
}
2390+
2391+
void loop() {
2392+
if (buttonPressed) {
2393+
buttonPressed = false;
2394+
digitalWrite(ledPin, !digitalRead(ledPin));
2395+
2396+
Serial.print("- Button pressed! Count: ");
2397+
Serial.println(pressCount);
2398+
}
2399+
2400+
delay(10);
2401+
}
2402+
```
2403+
2404+
To test this example, connect a push button to the Nano R4 board as follows:
2405+
2406+
- Connect one leg of a push button to pin `D2`
2407+
- Connect the other leg of the push button to `GND`
2408+
- No external components needed (using built-in LED and internal pull-up)
2409+
2410+
![Interrupt pins test circuit on the Nano R4 board](assets/interrupt-pins-1.png)
2411+
2412+
Open the Arduino IDE's Serial Monitor (Tools > Serial Monitor) to see the interrupt count increase with each button press.
2413+
2414+
![Arduino IDE Serial Monitor output for the basic interrupt example sketch](assets/interrupt-pins-2.png)
2415+
2416+
When working with interrupts on the Nano R4 board, there are several key points to keep in mind for successful implementation:
2417+
2418+
- Keep ISR functions short and fast: Avoid `delay()`, `Serial.print()`, or complex calculations inside ISRs as they block other interrupts.
2419+
- Use volatile variables: Always declare variables shared between ISRs and main code as volatile to prevent compiler optimization issues.
2420+
- Manage channel conflicts: Verify that pins don't share the same interrupt channel when using multiple interrupts.
2421+
- Consider debouncing: Mechanical switches may cause multiple triggers (add a 100nF capacitor or implement software debouncing).
2422+
- Protect shared multi-byte variables: Disable interrupts temporarily when accessing them.
2423+
22932424
## Qwiic Connector
22942425

22952426
The Nano R4 board features an onboard Qwiic connector that provides a simple, tool-free solution for connecting I²C devices. The Qwiic ecosystem, developed by SparkFun Electronics, has become an industry standard for rapid prototyping with I²C devices, allowing you to connect sensors, displays, and other peripherals without soldering or complex wiring. This makes it perfect for quickly building sensor networks, adding displays, or expanding your project's capabilities with minimal effort.

0 commit comments

Comments
 (0)