Arduino Uno External Interrupt Tutorial with Exercises

Arduino Uno External Interrupt Tutorial with Exercises

Performance Parameters① Main Control Chip: ATmega328P② Digital Input/Output Pins: 14③ PWM Pins: 6④ Storage (Code Space): 32KB⑤ RAM (Runtime Storage): 2KB; EEPROM (Power-off Save Space): 1KB⑥ Crystal Oscillator: 16MHz Online Simulation: https://wokwi.com/

Arduino Uno Tutorial ①: Installing Arduino IDE

Arduino Uno Tutorial ②: Development Board and LED Test

Arduino Uno Tutorial ③: Button Control LED, with Exercises

Arduino Uno Tutorial ④: Analog Quantity, Using ADC to Measure Voltage

Arduino Uno Tutorial ⑤: External Interrupt, with Exercises

(1) Introduction Arduino Uno interrupts are a special functional mechanism that allows Arduino to immediately respond to specific events or signals while executing other tasks. For example, when the program is running, if you press the stop button to interrupt execution, you need to use the external interrupt function. Definition and Types of Interrupts Definition: An interrupt is a commonly used technique in computer hardware and operating systems that allows a program to immediately interrupt the currently executing code when a specific event occurs and jump to a predefined interrupt handler function to perform specific operations. Types: In Arduino Uno, interrupts are mainly divided into external interrupts and timer interrupts. External Interrupt: Triggered by changes in the level on external pins, commonly used to detect button presses, sensor signal changes, and other external events. The Arduino Uno has two available external interrupt pins, namely pin 2 and pin 3. Timer Interrupt: Triggered by the Arduino’s timer module, it can be set to generate interrupts at specific time intervals, commonly used for periodic tasks such as timed data collection and data transmission.

attachInterrupt(interruptNum, ISR, mode);

interruptNum: This is the external interrupt number; the Arduino board only has two pins that support it, namely GPIO2 (interrupt number 0) and GPIO3 (interrupt number 1). For most Arduino boards, you can directly use the digitalPinToInterrupt(pin) macro to convert the digital pin pin to the corresponding interrupt number. ISR: The function called when the interrupt is triggered. mode: Specifies what type of pin state change will trigger the interrupt. Common modes include: LOW: Triggered when the pin is low (not recommended as it will continuously trigger the interrupt). CHANGE: Triggered when the pin level changes. RISING: Triggered during the rising (low to high) process of the pin level. FALLING: Triggered during the falling (high to low) process of the pin level. The external interrupts of Arduino can be triggered by rising edge, falling edge, low level, and level change trigger modes.(2) Program ① Set the bool1 variable to read the state of pin 2, which is the button state.

int bool1 = digitalRead(2);

② The setup function sets pin 13 to output mode (LED) and pin 2 to input mode (button). Call the interrupt function, digitalPinToInterrupt(2) converts pin 2 to interrupt 0, set a jump function “zhong”, and set the rising edge trigger RISING.

void setup() {  pinMode(13, OUTPUT);  pinMode(2, INPUT);  attachInterrupt(digitalPinToInterrupt(2), zhong, RISING);// Interrupt function, interrupt 0 of pin 2, call the interrupt function "zhong", rising edge trigger}

③ Experimental Program—No Interrupt When there is no interrupt, the code is written like this; pressing the button will not extinguish the LED, because the program has already entered the while loop, so it has no chance to read the program outside of while.

void loop() { while(1){  digitalWrite(13, HIGH);  delay(500); } if(bool1 == HIGH) {  digitalWrite(13, LOW); }}

④ No Interrupt Phenomenon When the button is pressed, there is no response, still executing the while function.

Arduino Uno External Interrupt Tutorial with Exercises

⑤ Experimental Program—Using Interrupt We write a zhong function that turns off the LED. When the program detects the interrupt signal, it will execute the content of the “zhong” function. After the interrupt program is executed, continue executing the while function.
void loop() { while(1){  digitalWrite(13, HIGH);  delay(500); }} void zhong(){  digitalWrite(13, LOW);}

⑥ With Interrupt Phenomenon

Arduino Uno External Interrupt Tutorial with Exercises

(3) Exercises Question: Button controls the light (if the LED is on, turn it off; if the LED is off, turn it on), using external interrupts to control. Answer:

// Define LED pin const int ledPin = 13; // Set pin mode void setup() { // Initialize LED pin as output pinMode(ledPin, OUTPUT); // Initialize button pin as input and enable internal pull-up resistor pinMode(2, INPUT); // Attach interrupt service routine to digital pin 2 (external interrupt 0) attachInterrupt(digitalPinToInterrupt(2), toggleLED, FALLING); // Trigger interrupt when the voltage on pin 2 changes from high to low // Initialize LED state to off digitalWrite(ledPin, LOW); } // Interrupt service routine to toggle LED state void toggleLED() {  digitalWrite(ledPin, !digitalRead(ledPin)); // If the LED is on, turn it off; if the LED is off, turn it on } // Main loop (no need to execute any operation here, as all logic is handled in the interrupt service routine) void loop() { // You can add some debugging information or empty operations }

To be continued…

END

Free Application Development Board

Arduino Uno External Interrupt Tutorial with Exercises

Submission/Promotion/Cooperation/Join Group Please scan the QR code to add WeChat

(Please indicate your intention, and if you want to join the group, please indicate city-name-industry position information)

Arduino Uno External Interrupt Tutorial with Exercises

Arduino Uno External Interrupt Tutorial with Exercises

Leave a Comment

×