Arduino Workshop - Chapter 5 - Interrupts

Core Electronics
16 Mar 201710:46

Summary

TLDRThis script introduces the concept of interrupts in microcontroller programming, contrasting them with the polling method for handling button inputs. It explains how interrupts allow the microcontroller to be notified of changes without constant checking, making them more efficient for complex programs. The video covers the basics of setting up interrupts in Arduino, including defining interrupt service routines (ISRs) and the importance of keeping ISRs short to avoid interfering with other tasks. It also provides practical tips and best practices for using interrupts effectively.

Takeaways

  • ๐Ÿ”„ Polling is a method where a program frequently checks the state of an input like a button, but it's inefficient for complex programs.
  • ๐Ÿ› ๏ธ Interrupts provide a more efficient way to handle input changes; they allow the microcontroller to be notified of state changes without constant checking.
  • ๐Ÿ‘ฎโ€โ™‚๏ธ An interrupt is a hardware feature that can monitor an input pin and 'interrupt' the microcontroller to signal a change.
  • ๐Ÿ“ฌ The analogy of expecting a letter from the postman illustrates how interrupts allow you to be notified instantly of an event, freeing you to do other tasks.
  • โš ๏ธ There are three types of interrupts: change, rising, and falling, each triggered under different conditions of signal transition.
  • ๐Ÿ›‚ The Interrupt Service Routine (ISR) is a specific function run when an interrupt condition is met, and it should be kept as short as possible.
  • ๐Ÿšซ ISRs cannot return values or take parameters, and certain functions like delay() do not work inside them due to their interrupt-based nature.
  • ๐Ÿ” The 'volatile' modifier is used for variables changed inside an ISR to indicate they can change at any time, independent of the main program flow.
  • ๐Ÿ”Œ Only certain Arduino pins have interrupt capabilities, and not all chips support all types of interrupts on all pins.
  • ๐Ÿ”„ When using interrupts, attachInterrupt is used to set up the interrupt on a specific pin with a designated ISR to be executed upon change, rising, or falling.
  • ๐Ÿ”ง In practice, using interrupts can bypass the need for continuous polling in the main loop, making the program more efficient and responsive.

Q & A

  • What is the primary method discussed in the script for detecting button presses in simple programs?

    -The primary method discussed is polling, which involves constantly checking the state of the button at a high frequency to detect presses or releases.

  • Why is polling not efficient for complex programs?

    -Polling is inefficient for complex programs because it requires the microcontroller to frequently check the button state, even when it is often the same, which interrupts other tasks and processes.

  • What is an interrupt in the context of microcontrollers?

    -An interrupt is a hardware feature on a microcontroller that can monitor the state of an input pin and can interrupt the microcontroller's current task to signal a change in state.

  • How do interrupts differ from polling in terms of efficiency?

    -Interrupts are more efficient because they allow the microcontroller to perform other tasks and only check the button state when there is an actual change, rather than constantly polling.

  • What are the three basic types of interrupts mentioned in the script?

    -The three basic types of interrupts are change, rising, and falling. Change interrupt triggers on either a rising or falling signal, while rising and falling interrupts trigger specifically on transitions from low to high or high to low, respectively.

  • What is an Interrupt Service Routine (ISR)?

    -An ISR is a specific function that runs when an interrupt condition is met, allowing the microcontroller to respond to the interrupt event.

  • Why should the code in an ISR be kept as short as possible?

    -The code in an ISR should be short to minimize the time the microcontroller is occupied with the ISR, ensuring it can quickly return to its main tasks and not interfere with other processes that require precise timing.

  • Why are the delay and Millie's functions not suitable for use inside ISRs?

    -Delay and Millie's functions are not suitable for ISRs because they are based on interrupt functions, and only one interrupt can be executed at a time, which would conflict with the ISR's operation.

  • What is the purpose of declaring a variable as 'volatile' inside an ISR?

    -The 'volatile' modifier is used to declare a variable that can be changed at any time by an interrupt service routine, ensuring the main program is aware of potential changes made within the ISR.

  • Which pins on an Arduino board are typically interrupt-capable?

    -Typically, digital pins 2 and 3 on an Arduino board are interrupt-capable, but this can vary depending on the board model, and a quick Google search can provide specific information for different boards.

  • How can you determine which pins on an Arduino board support interrupts?

    -You can determine which pins support interrupts by searching online for the specific Arduino board model along with terms like 'interrupt pins' or checking the board's datasheet or documentation.

Outlines

00:00

๐Ÿ”„ Understanding Interrupts in Arduino

This paragraph introduces the concept of interrupts as an efficient alternative to polling for input devices like buttons in Arduino programming. It explains how polling can be resource-intensive in complex programs and contrasts it with interrupts, which allow the microcontroller to be notified instantly when a change occurs. The analogy of a mailbox that alerts you to a letter's arrival is used to clarify the concept. The paragraph also outlines the three types of interrupts: change, rising, and falling, and emphasizes the importance of keeping the interrupt service routine (ISR) short to prevent blocking other tasks.

05:02

๐Ÿ›  Implementing Interrupts with Debouncing

The second paragraph delves into the practical application of interrupts, particularly focusing on debouncing a push button. It describes the process of setting up an interrupt with global variables, pin modes, and the use of the 'volatile' modifier for variables that can change within an ISR. The paragraph explains how to attach an interrupt to a specific pin and choose the type of interrupt (change, rising, or falling). It also discusses the importance of keeping the ISR concise and the conditions under which the debouncing function is executed, including the use of a flag variable to indicate an interrupt has occurred.

10:02

๐Ÿ” Best Practices for Using Interrupts in Arduino

The final paragraph provides guidance on using interrupts effectively in Arduino projects. It advises on checking which pins on a specific board are interrupt-capable, either through documentation or a quick online search. The paragraph reinforces the power of interrupts for handling multiple tasks simultaneously and reiterates the best practices discussed earlier, such as keeping ISRs short and ensuring that only certain pins are used for interrupts. It concludes by highlighting the versatility of interrupts and encourages adherence to the established guidelines for successful implementation.

Mindmap

Keywords

๐Ÿ’กPolling

Polling is a method used in programming where the system regularly checks the state of a device, such as a button, to detect changes. In the context of the video, polling is used to determine if a button has been pressed or released. However, the script points out that polling is less efficient for complex programs because it requires constant checking, which can be resource-intensive and interrupt other processes.

๐Ÿ’กInterrupts

Interrupts are a more efficient alternative to polling for handling input in microcontroller-based systems. They allow the hardware to notify the microcontroller when a change in state occurs, such as a button press, without the need for constant checking. The video explains that interrupts free up the microcontroller to perform other tasks and are managed by hardware flags, making them user-friendly in the Arduino environment.

๐Ÿ’กMicrocontroller

A microcontroller is a small computer on a single integrated circuit that is used to control a device or system. In the video, the microcontroller is responsible for monitoring input states and executing code in response to interrupts. It is highlighted that microcontrollers have limited resources, making efficient use of interrupts crucial for complex programs.

๐Ÿ’กInterrupt Vector

An interrupt vector is a memory location or a set of locations that the microcontroller uses to jump to the appropriate interrupt service routine (ISR) when an interrupt occurs. The video script uses the term to illustrate how interrupts can 'interrupt' the microcontroller's current task and direct it to handle the event that caused the interrupt.

๐Ÿ’กInterrupt Service Routine (ISR)

An ISR is a function that is executed in response to an interrupt. The video emphasizes that ISRs should be kept as short as possible to minimize the time the microcontroller is occupied and unable to perform other tasks. The script provides an example of an ISR for a button press that sets a flag to indicate the event.

๐Ÿ’กDebouncing

Debouncing is a technique used to filter out noise or false triggers from switches or buttons. In the video, debouncing is mentioned as part of the process to ensure that a button press is detected cleanly and reliably. The script describes using variables and if statements to implement debouncing in both polling and interrupt-driven scenarios.

๐Ÿ’กVolatile

The 'volatile' keyword in programming is used to declare a variable that can be modified by an ISR without the main program being aware of the change. The video script explains that variables inside an ISR should be declared volatile to ensure the main program recognizes changes made by the ISR, as in the case of the 'button flag' variable.

๐Ÿ’กPin Change

A pin change interrupt is a type of interrupt that triggers when there is any change in the state of a digital input pin, whether it goes from high to low or vice versa. The video script explains that this type of interrupt can be used to detect any change in the state of a button or other input device without the need for specific rising or falling edge detection.

๐Ÿ’กRising Edge

A rising edge in electronics refers to the transition of a signal from a low state to a high state. In the context of the video, a rising edge interrupt is set up to trigger an action when this transition occurs, such as a button being pressed.

๐Ÿ’กFalling Edge

A falling edge is the opposite of a rising edge, where a signal transitions from a high state to a low state. The video script mentions setting up a falling edge interrupt to detect when a button is released.

๐Ÿ’กArduino

Arduino is an open-source electronics platform based on easy-to-use hardware and software. The video script uses Arduino as an example to demonstrate the concepts of polling, interrupts, and ISRs. Arduino is known for its user-friendly approach to programming microcontrollers, making complex concepts like interrupts accessible to beginners.

Highlights

Introduction to the concept of polling as a method for checking the state of a button in a program.

Limitations of polling in complex programs due to the need for frequent checks.

Introduction to interrupts as an efficient alternative to polling for handling digital inputs.

Explanation of how interrupts allow the microcontroller to be notified of changes without constant checking.

Description of interrupts as hardware capabilities that can interrupt the microcontroller's operation.

Comparison of polling to checking a mailbox frequently versus interrupts as a notification system.

Clarification of the three basic types of interrupts: change, rising, and falling.

Importance of keeping the Interrupt Service Routine (ISR) as short as possible.

Explanation of the ISR's role in jumping from the main program to handle the interrupt and then returning.

Guidelines for using interrupts with Arduino, including keeping ISRs short and not using delay functions.

Details on the use of the 'volatile' modifier for variables changed inside an ISR.

Limitation that only certain pins on Arduino have interrupt capabilities.

Information on the different interrupt capabilities of pins, such as change, rising, or falling.

Demonstration of how to set up an interrupt in the Arduino IDE with example code.

Use of a 'button flag' variable declared as volatile to indicate changes detected by the ISR.

Explanation of how the ISR sets the 'button flag' and its role in triggering the debouncing function.

Advantages of using interrupts over polling for efficiency in complex programs.

Practical example of using interrupts with a button and an LED on an Arduino board.

Conclusion emphasizing the power and versatility of interrupts in programming for Arduino.

Transcripts

play00:00

[Music]

play00:05

in all of the sections that we've

play00:07

covered so far we've been using buttons

play00:09

and other Hardware devices as inputs to

play00:11

control changes in the program such as

play00:14

manipulating outputs and in the case of

play00:16

a button we've had to make sure that our

play00:18

program runs a check to see what the

play00:20

button state is really regularly at a

play00:22

high frequency so that we don't miss a

play00:24

press or a release this method is known

play00:27

as polling as it involves constantly

play00:29

polling or checking the state of the

play00:32

button this is all well and good for

play00:34

simple programs with little else going

play00:36

on however once your program starts to

play00:38

grow and become more complex polling

play00:41

doesn't work as well because you've got

play00:42

heaps of other stuff going on at the

play00:43

same time it requires your

play00:45

microcontroller to stop whatever it's

play00:47

doing and frequently check the state of

play00:48

the button even if most of the time it's

play00:51

exactly the same as it was

play00:53

before fortunately there is a much more

play00:55

efficient way to read the state of a

play00:57

digital input using interrupts you may

play01:00

have heard of interrupts before and

play01:02

imagined them as a scary complicated

play01:03

procedure but Arduino does a really good

play01:06

job of making them incredibly user

play01:08

friendly so what is an interrupt well an

play01:11

interrupt is a section of hardware on

play01:13

the microcontroller which is capable of

play01:15

monitoring the state of an input pin by

play01:17

itself and can literally interrupt the

play01:20

microcontrol in whatever it's doing to

play01:22

let it know that there is an interrupt

play01:24

Vector ready that the state of Ain has

play01:25

changed in this case or that it is high

play01:27

or low the beauty of this this is is

play01:30

that the interrupts are all taken care

play01:32

of with Hardware flags and some very

play01:34

lowlevel microcontroller instructions

play01:37

which means you can be doing other

play01:38

things and not have to worry about

play01:40

constantly checking the button St a good

play01:43

way to think of interrupts is imagine

play01:45

you're expecting a letter from the

play01:46

postman you could go out and check the

play01:48

letter box every 2 minutes or more

play01:50

regularly to see if it's been delivered

play01:52

which wouldn't allow you to get much

play01:53

done in between or you could have a

play01:56

mailbox that makes a loud noise when a

play01:58

letter is delivered so you can forget

play02:00

about it but be notified instantly as

play02:02

soon as it arrives leaving you free to

play02:04

do whatever you like until that point as

play02:07

mentioned before there are three basic

play02:09

types of interrupts change rising and

play02:12

falling when you tell the Arduino that

play02:14

you wish for a PIN to have the

play02:16

corresponding interrupts for it enabled

play02:18

you tell it under what condition you

play02:20

wish to trigger the interrupt on a

play02:22

rising Edge a signal going from low to

play02:24

high on a falling Edge which is a signal

play02:26

going from high to low or either which

play02:29

is rising or falling known as change

play02:32

when this condition is met your uino

play02:34

will will run a specific function that

play02:36

is unique to that interrupt this

play02:39

function is known as the interrupt

play02:40

service routine or ISR as we'll refer to

play02:42

it the key to interrupts is that the

play02:45

code that executes in your ISR should be

play02:48

as short as possible because as soon as

play02:51

the interrupt is triggered it will jump

play02:52

from whatever wherever your

play02:54

microcontroller was in your program and

play02:57

service the interrupt and complete the

play02:58

ISR before it go back to where it left

play03:00

off and that means that it can't do

play03:03

anything else while it's running the ISR

play03:05

so if you're using an LCD display or

play03:07

connecting to a Wi-Fi network or

play03:09

something that requires tight timing and

play03:11

integration those processors will often

play03:13

fail because whilst they might appear to

play03:15

be only running in the background if

play03:17

your microcontroller is locked up

play03:19

executing an ISR it can't do anything

play03:22

else it can be a bit confusing trying to

play03:25

remember all that there is to using

play03:27

interrupts so here is a list of some

play03:29

basic dos and don'ts when using

play03:31

interrupts with

play03:33

Arduino keep isrs or the interrupt

play03:36

service routines the function that calls

play03:38

off the interrupt as short as possible

play03:40

usually you're only using this interrupt

play03:42

service routine to set a flag or change

play03:44

a state rather than executing an entire

play03:46

section of

play03:47

code isrs cannot return values or take

play03:52

parameters the delay and Millie's

play03:54

function native to Arduino they will not

play03:56

work inside isrs because they're based

play03:58

on interrupt functions and you can only

play04:00

have one interrupt being executed at a

play04:02

time delay micros seconds which is a

play04:06

subcategory of the delay function except

play04:08

it stores and measures in microsc will

play04:11

work because it's not interrupt based

play04:14

any variable which is changed inside an

play04:16

ISR should be declared with the volatile

play04:19

modifier and only certain pins have

play04:21

interrupt capabilities on the uino you

play04:24

know only digital pins 2 and three have

play04:27

interrupt capabilities and B aware that

play04:30

not all chips support all three kinds of

play04:32

interrupts across all of the pins some

play04:35

pins may only allow for a change

play04:37

interrupt which can determine whether

play04:38

the signal is rising or falling but it

play04:41

will be run on either and you will have

play04:42

to run additional code to determine the

play04:44

state of the input to determine whether

play04:46

it was a rising or falling Edge other

play04:48

pins will have the option for all three

play04:50

so you can set it for a falling Edge a

play04:52

rising Edge or a pin

play04:55

change now that we've talked about what

play04:57

interrupts are let's look at how to use

play04:59

them so we'll take a look at the Arduino

play05:02

IDE so I've got a basic function going

play05:05

on here and it's incredibly similar to

play05:07

the more advanced concept that we use

play05:09

for if statements where we looked at

play05:10

debouncing a push button and using some

play05:12

if logic and some variables to keep

play05:14

track of the state using debouncing to

play05:16

ensure that our switch is pressed

play05:17

cleanly and if statements to determine

play05:19

whether it's been held or

play05:22

released so it's almost exactly the same

play05:24

we've got LED pin button pin and some

play05:27

Global variables and the only one that's

play05:28

different here that you notice is this

play05:30

volatile int button flag now button flag

play05:33

we're going to use as a binary one or

play05:35

zero flag variable just like any other

play05:38

except we've used the modifier volatile

play05:40

which declares that this integer can be

play05:42

changed without uh without the rest of

play05:45

the Arduino body knowing about it to put

play05:47

in perspective the whole microcontroller

play05:50

is has its mind focused on executing

play05:52

this interrupt service Vector sorry

play05:54

interrupt service routine and not all

play05:57

the rest of those functions aren't

play05:58

running at all so you need to declare it

play06:00

as volatile to make sure the Arduino

play06:02

knows that this uh this variable could

play06:04

change at any time inside an interrupt

play06:06

service routine we have our debounce uh

play06:10

variable as normal declaring some pin

play06:11

modes and here we can see that we're

play06:14

initializing this interrupt to the pin

play06:16

so we use attach interrupt then inside

play06:19

there we use digital pin to interrupt

play06:21

and in Brackets the pin that you're

play06:23

using so we could change that from two

play06:25

to button pin but two makes it easier to

play06:27

remember that we're using a specific

play06:29

spefic interrupt capable pin then a

play06:32

comma and you declare the name of the uh

play06:34

the name of the function that you wish

play06:36

to run so ISR button is the general rule

play06:39

of thumb for declaring functions that

play06:41

you're running as isrs so interrupt

play06:44

service routine button and then we want

play06:46

it to run on a change meaning whether it

play06:49

transitions from a high to a low signal

play06:51

or from a high uh from a low to a high

play06:53

signal and vice versa so you don't

play06:55

always have to use digital pin to

play06:57

interrupt there's a couple of different

play06:59

uh syntax formats for doing this inside

play07:01

Arduino however digital pin to interrupt

play07:03

is the safest it largely just depends on

play07:06

the model of Arduino board that you're

play07:08

using but generally speaking if you use

play07:10

digital pin to interrupt rather than

play07:12

just the pin itself most boards are

play07:14

going to work without a

play07:15

hitch now in this void Loop we have the

play07:18

exact same set of instructions that

play07:20

we'll using for standard debouncing so

play07:22

we won't go through that too much the

play07:24

only difference is we've added an extra

play07:26

condition to determine whether our

play07:28

Arduino enters into this whole

play07:30

debouncing function as normal before we

play07:32

had mli minus last press is greater than

play07:35

debounce time if that is true then we're

play07:37

going to run this section of code that

play07:38

was ensuring that it could only be run

play07:40

at a certain frequency filtering out

play07:42

that noise now we're also adding an

play07:44

extra condition for button flag and

play07:46

button flag has to be true or one in

play07:47

order uh for this to run at all and the

play07:50

reason for this is that it's going to

play07:52

bypass all of this because the if

play07:54

statement isn't met and so the loop is

play07:56

just going to iterate doing nothing and

play07:58

the only way that button State H sorry

play08:00

button flag can be equal to one is if

play08:03

the isrb button function the interrupt

play08:06

service routine for pin 2 is run in

play08:09

which case all it does is set button

play08:11

flag and that that process of changing a

play08:13

variable uh changing the state of of a

play08:16

flag variable like that is so short it's

play08:18

a really good example of how to use

play08:20

interrupt service routines we could put

play08:22

all of that code inside the interrupt

play08:24

service routine if we wanted and that

play08:26

would work perfectly fine in this

play08:28

example but if we had a lot of other

play08:29

background process is running it it

play08:31

would chew up more time that the Arduino

play08:32

is stuck in that service routine because

play08:35

as it is if it's only running through

play08:37

instructions in the void Loop if you're

play08:39

especially if you're running a whole

play08:40

bunch of other libraries as we talked

play08:42

about especially um for you know timing

play08:44

specific Hardware or Services it's going

play08:47

to be running the void Loop Shore but

play08:48

every so often you'll Branch off into

play08:50

another hidden interrupt service routine

play08:52

that's running in the background jump to

play08:54

all these different parts of the code

play08:56

that you won't actually realize what

play08:57

going on but if it's inside this ISR it

play08:59

can't so that's why it's really

play09:00

important that it's really short and

play09:03

then so it it can only run that that uh

play09:06

toggle function if the interrupt service

play09:08

routine has already been run in other

play09:10

words if it has uh detected a change be

play09:13

it a press or a

play09:15

release then the only other thing we've

play09:17

added in is button flag is equal to zero

play09:19

in other words when it executes the

play09:21

toggle function including all of the

play09:23

debouncing no matter whether it's a

play09:24

press or a release button flag is set to

play09:27

zero which means it will not run that

play09:28

section of code until our interrupt

play09:30

service routine has run again to reset

play09:33

that flag so let's go ahead and load it

play09:35

up make sure we've got the correct

play09:38

settings hit upload and it's going to

play09:41

work exactly the same as our previous uh

play09:44

toggle button example did because it's

play09:46

achieving the same goal but it's doing

play09:48

it in a much more efficient manner so

play09:50

when we press it it's a nice little

play09:53

toggle switch I'm using pin 3 here but

play09:54

you could also use pin 13 or any other

play09:57

digital pin for the led the important

play09:58

thing is there that if you want to use

play10:00

an interrupt for button pin it can only

play10:02

be pin 2 or three and you can find out

play10:05

what pins are interrupt capable just by

play10:07

Googling the board and saying Arduino

play10:09

you know interrupt pins or tensy

play10:11

interrupt pins or whatever board you're

play10:14

using quick Google will bring up either

play10:16

the data sheet or a documentation or

play10:18

reference page and let you know what

play10:20

they are and which pins can deal with

play10:22

change interrupts or they can deal with

play10:24

all three so Rising falling State

play10:26

changes as well so that's a little bit

play10:28

on using interrupts they're incredibly

play10:30

powerful you can do all kinds of things

play10:32

with them you can attach multiple pins

play10:33

to the same ISR or you can you can use

play10:36

it in however you like as long as you

play10:38

follow those guidelines that we set down

play10:40

before for the basic General uh best

play10:43

practices for using interrupts

Rate This
โ˜…
โ˜…
โ˜…
โ˜…
โ˜…

5.0 / 5 (0 votes)

Related Tags
ArduinoInterruptsPollingEfficiencyMicrocontrollerButton StateDebouncingISRHardware FlagsPin Change