How to Use Interrupts on the Arduino - Ultimate Guide to the Arduino #24

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the three in one smart car and iot learning kit from Sun founder is a Hands-On all included Electronics kit that is perfect for anyone who wants to learn how to master the Arduino the kit comes with an Arduino 22 different sensors and modules thread boards jumper wires and everything else you need to build a bunch of fun and interesting projects learn about robotics by building a remote controlled smart car that can be controlled with an infrared remote controller or drive on its own and avoid obstacles or Follow The Line learn about the internet of things with a project that lets you monitor the temperature humidity and light level of a room from an app on your smartphone [Music] and build a plant monitor that tracks the temperature humidity light intensity and soil moisture and displays it on your smartphone so you can keep your plants water remotely it's a super cool kit and I had lots of fun building all the projects in it so click the link in the description below to order the kit from some founder foreign [Music] we'll learn how to use two different types of interrupts Hardware interrupts which are triggered by an external event like the Press of a button or a signal from a sensor and timer interrupts which are interrupts that are triggered by one of the arduino's internal timers but before we learn how interrupts work and how to use them I just want to show you a quick example that demonstrates why they're useful in this circuit the yellow LED will blink on and off repeatedly the green LED is controlled by a push button the yellow LED is connected to pin 12 via current limiting resistor and the green LED is connected to pin 11 via current limiting resistor as well one side of the push button is connected to ground and the other side is connected to digital pin 7. I'll be using the arduino's internal pull-up resistor so I don't need to connect one here here's the sketch in the top half of the sketch we declare variables for the button input pin the button LED and the blink LED we set the pin modes in the setup function the button input is using the internal pull-up resistor so I set the mode to input underscore pull up down in the loop function this block of code turns the green LED on when the button is pressed and down here is the code that blinks the yellow LED on and off it's commented out for now just so I can show you how the button should respond to pressing it so the button turns on and off the green LED each time I press it okay now let me show you what happens when I uncomment the code that makes the yellow LED blink the yellow LED is blinking on and off just fine but when I press the button the Arduino misses a lot of the presses sometimes the green LED lights up and sometimes it doesn't this is what can happen when you ask the Arduino to do two things at once when the Arduino gets to one of the delay functions it pauses and can't do anything else until the delay is over while it's delaying it stops reading the button pin so it misses some of the button presses let's fix this problem by using an interrupt to make an interrupt you first need to write a special function called an interrupt service routine or ISR for short the internet service routine will contain all the code you want to be executed when the interrupt is triggered the interrupt service routine is created by typing void then the name you want to give it in the blinking LED example from earlier I wanted the button to control the green LED while the yellow LED was blinking so the button press will trigger the interrupt I'm going to name this button interrupt now we can write the code that reads the button and switches the green LED on and off up here in the ISR interrupt service routine should be as short and fast as possible if you use multiple interrupts be aware that only one can be run at a time also the Millis micros And Delay functions all depend on interrupts themselves so they won't work inside of an interrupt service routine and serial print doesn't always work inside of an ISR either because serial data is transferred with an interrupt however if you need a delay in your ISR you can use the delay microseconds function to achieve the same effect interrupt service routines can't take inputs or return values if you have any variables in the ISR like this one here for button state it should have the volatile keyword in front of it declaring a variable is volatile prevents the compiler from optimizing it and make sure that it's stored in the arduino's SRAM instead of a storage register like other variables otherwise the variable's value might not always be accurate for example if your program is currently running inside of an ISR and the variable changes in the loop section the variable inside the ISR might not get updated to the new value the volatile keyword ensures that the variable is updated if it gets changed in another part of the sketch now to trigger the interrupt service routine we use the attach interrupt function it goes in the setup section the attach interrupt function takes three parameters the first parameter is the interrupt number the Arduino Uno has two interrupts interrupt zero and interrupt one interrupt 0 is connected to digital pin 2 and interrupt 1 is connected to digital pin 3. a 1 or a zero here will indicate which interrupt you want to use but to make it easier to remember which interrupt is connected to which pin there's a function called digital pin to interrupt that you can use instead of the interrupt number the digital pin to interrupt function takes the digital pin number either two or three so in the previous blinking sketch I had the button connected to digital pin 7. so I'll need to switch that over to one of the interrupt pins let's say pin 2. so here I've moved the button input from pin 7 to pin 2. now I need to change my button pin variable from 7 to 2. pin 2 is stored in the button pin variable so I'll enter that in here foreign the next parameter used by the attach interrupt function is the name of the interrupt service routine I called it button interrupt so I'll put that here you don't need to include the parentheses the last parameter is the internet mode the interrupt mode defines the type of signal that will trigger the interrupt there are four options hello Rising falling and change if the internet mode is set to low the interrupt will be triggered whenever the interrupt pin is low if it's set to Rising the interrupt will be triggered when the signal goes from low to high if it's set to Falling the interrupt will be triggered when the signal goes from high to low and if it's set to change the interrupt will be triggered when the signal goes either high to low or low to high to switch on and off the button LED we need to know when the button press goes both high and low so I'll use change here so now all that's left in the loop function is the code that makes the yellow LED blink the code that detects the button press and turns the green LED on and off is up here in the interrupt service routine let's see how this makes the LEDs react foreign the yellow LED is blinking fine now I'm going to press the button so that's looking good every time I press the button the green LED turns on the Arduino isn't missing any of the button presses okay now let's take a look at timer interrupts Hardware interrupts are triggered by an external event like a button press but timer interrupts are triggered by the arduino's internal clock before we can really understand how timer interrupts work we need a little background information on timers timers are electronic circuits built into the microcontroller that allow you to count the time timer interrupts are usually used to read or write to pins at regular intervals for example you could use a timer interrupt to get the reading from a humidity sensor every 5 Seconds you could also use timer interrupts to make a speedometer by measuring the time between clicks of a rotary encoder they can also be used to generate highly accurate pulse width modulation signals more accurate than what the analog write function can provide the Arduino has three timers timer 0 timer 1 and timer 2. timer 0 is an 8-bit timer that's used for the delay Millis and micros functions you probably don't want to mess with timer zero or it might break those functions timer 1 is a 16-bit timer and timer 2 is an 8-bit timer it's okay to use these two all three timers are based on the clock frequency of the Arduino which is 16 megahertz each clock cycle counts as one tick of the timer one is a 16-bit timer which means that it can hold a maximum of 2 to the 16 or 65 536 counts before it gets full when it reaches 65 535 the timer resets to zero and starts counting again timer 0 and timer 2 are 8-bit timers so they can hold a maximum of two to the eight or 256 counts the speed of the timer can be slowed down by using a prescaler value by changing the prescaler value you can adjust the length of the timer the Arduino timers have two modes pulse with modulation mode and clear timer on compare match or CTC mode pulse with modulation mode can be configured to Output a plus with modulation signal clear timer on compare match mode can be used to trigger an interrupt when the timer reaches a preset value to demonstrate how to use timer interrupts I'm going to show you a project that has two blinking LEDs this is what the circuit will look like One LED will blink at a rate of 500 milliseconds and the other LED will blink at a rate of 5 Seconds the typical method to Blink LEDs uses the delay function but that won't work in this case so we need to use a timer interrupt to control one of the LEDs there's a red LED with the current limiting resistor connected to digital pin 10. and a yellow LED with the current limiting resistor connected to digital pin 5. the yellow LED will blink on and off at a rate of 500 milliseconds and the red LED will blink on for 5 seconds and off for 5 seconds let's take a look at the sketch to make the programming easier we're going to use a library called timer 1. you can go to this link to download the library zip file at the top of the sketch we include the library the yellow LED will be blinking on and off fairly quickly once every 500 milliseconds but the red LED will be switched on and off every 5 Seconds a much longer time so we'll set up the timer to control the red LED in the setup section we set Arduino pins 10 and 5 as outputs the timer 1 Library can only use pins 9 and 10 for timer interrupts so I'm going to use pin 10 for the timer interrupt and I'll use pin 5 in the loop initialize is the function that initializes the timer the argument of this function sets the length of time before the interrupt is triggered in microseconds 5 million microseconds is 5 Seconds with the timer interrupt Library the minimum interrupt period is one microsecond and the maximum interrupt period is 8 388 480 microseconds next we attach the interf service routine with timer one dot attach interrupt the attach interrupt function only takes one argument the name of the interrupt service routine down here I have the ISR and it's called long blink so I put that in the parentheses so every time the timer reaches 5 seconds it's going to call this long blink interrupt service routine with timer interrupts like Hardware interrupts it's best to keep your interrupt service routine short here I have the digital write function that toggles the LED on and off normally we would use the delay function to Blink an LED on and off but only one interrupt service routine can run at once and since the delay function itself uses an ISR it can't be used inside of another ISR so we need a different way to turn on and off the PIN we already know that the digital write function takes two arguments the first argument is the PIN number and the second argument is the value to write either high or low however you can also take a reading from a pin and use that as the value to write remember that the exclamation point is the Boolean operator for not so this says if the digital state of pin 10 is high write it low and if it's low write it high so each time this interrupt is called it will read the pin's current state then write the opposite value it's a pretty useful way to toggle an LED and it only uses one line of code now in the loop section we have the code that blinks the yellow LED this is how we're used to blinking an LED using the delay function to pause between digital rights this block of code will be running on its own and the timer interrupt will be running on its own too let's see if it works foreign so the yellow LED is blinking once every half second and the red LED is blinking on for 5 seconds and off for 5 seconds so this is obviously a really simple example but timer interrupts are useful in lots of other ways too instead of using the interrupt service routine to turn on an LED you could use it to read a sensor at a timed interval or you could use it to trigger a 5 volt relay while continuously reading a sensory in the next video we're going to use interrupt to read the clicks from a rotary encoder [Music] Sun founder is my go-to source for sensors modules and other parts for the Arduino and Raspberry Pi they have a huge selection of stem Robotics and iot kits and lots of useful sensors and modules every product has an online tutorial with wiring diagrams and example code they also offer free shipping on all orders with no minimum give them a try at www.sunfounder.com next time you need to order some parts
Info
Channel: Circuit Basics
Views: 2,813
Rating: undefined out of 5
Keywords: arduino, raspberry pi, circuit projects, electronics projects, circuits, circuit basics, arduino projects, raspberry pi projects, IOT, robotics
Id: C4TKgULwpg8
Channel Id: undefined
Length: 20min 3sec (1203 seconds)
Published: Fri May 05 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.