Arduino Interrupts Tutorial

Home/Arduino Projects/Arduino Interrupts Tutorial

Arduino Interrupts Tutorial

Dear friends welcome to this Arduino Interrupts Tutorial. In this video we are going to learn how to use interrupts with Arduino, an advanced but extremely useful feature of the Arduino. There is a lot to cover, so without any further delay let’s get started!

Intro to Arduino Interrupts Tutorial

Hello guys, I am Nick and welcome to educ8s.tv, a channel that is all about DIY electronics projects with Arduino, Raspberry Pi, ESP8266 and other popular boards. Subscribe to the channel now if you don’t want miss any future video. In this video we are going to learn how to use interrupts in our projects by building two simple projects. Be sure to watch until the end of the video because the trick we use in the second example is very useful for our future projects.

What is an interrupt?

But what is an interrupt? Most microprocessors have interrupts. Interrupts let you respond to external events while doing something else. Suppose you are sitting at home waiting for the new ESP32 board, you have ordered a few days ago, to arrive at your mailbox. You are very excited so you check your mailbox every ten minutes to see if the board has arrived. This procedure is called polling, and we were using this technique a lot in our projects. But what if we had told the mailman to ring the doorbell at his arrival? This way, we are free to do anything we want and at the time the board arrives at the mailbox we get notified and we can use it at once. This Arduino Interrupts Tutorial example explains exactly how an interrupt causes a processor to act.

The parts needed in order to build this Arduino Interrupts Tutorial are the following:

——————–
WHERE TO BUY
——————–

Arduino Uno: http://bit.ly/Cheap_Uno

Arduino Nano: http://bit.ly/CheapArduinoNano

Buttons: http://bit.ly/100Buttons

Small PIR sensor: http://bit.ly/SmallPirSensor

Small Breadboard: http://bit.ly/SmallBreadboard

LEDs: http://bit.ly/375LEDS

Resistors: http://bit.ly/ResistorsB

Jumper Wires: http://bit.ly/JumperWiresB

3 in 1 wires: http://bit.ly/Wires3InOne

Power Bank: http://bit.ly/PowerBank_XiaoMi

Full disclosure: All of the links above are affiliate links. I get a small percentage of each sale they generate. Thank you for your support!

The Arduino program is running and performing some function. However, when an interrupt occurs the main program stops executing and another function is called. When this function finishes, the processor goes back to the main program again and resumes its execution from the point it was stopped. The function that the processor executes when an interrupt happened is the ISR, the Interrupt Service Routine. Interrupts can come from various sources. In this video we are going to learn about hardware interrupts which are triggered by a state change on one of the digital pins. In other words, an interrupt can be triggered when a digital pin goes from LOW to HIGH or from HIGH to LOW.

With most Arduino boards, you can use only certain pins as interrupts. Interrupts are referred to by an ID number that correspond to particular digital pin. So, interrupt 0 and an Arduino Uno corresponds to digital pin 2. The Arduino Uno, the Arduino Nano and the Arduino pro mini support only two external interrupts, on digital pins 2 and 3. The Arduino Mega supports 6 external interrupts whereas the ESP8266 chip can support 16 external interrupts.

Let’s now see how to use an interrupt on a digital pin of the Arduino with an example. We are going to use an Arduino Nano which is actually a small Arduino Uno. You can use an Arduino Uno if you wish, the code and the connections are exactly the same. In this first example, we are going to build a simple project in which, each time we press the button, an interrupt will be triggered and the ISR will change the state of the LED. I have connected the LED to digital pin 13, and the button to digital pin 2 which supports hardware interrupts.

In order to make a digital pin to interrupt the main Arduino sketch we use the attachInterrupt function. The first argument is the interrupt ID so if we are using an Arduino Uno, the interrupt 0 corresponds to digital pin 2. The next argument is the function that is going to be executed when the interrupt is triggered. In other words, this is the ISR function. In this example we name the ISR buttonPressed. The last argument tells the Arduino what triggers the interrupt. RISING means that an interrupt will be triggered when the state of Digital Pin 2 goes from LOW to HIGH. We can also use the word FALLING which means that an interrupt will be triggered when the state of the pin goes from HIGH to LOW. Another option is to use the word CHANGE which will trigger an interrupt whenever a change in the state of the pin occurs.

Now that we have enabled the interrupt, we need to create the ISR. We can name the ISR buttonPressed. The function checks turns the LED ON or OFF depending its state. In order to remember the LED state it uses a Boolean global variable. Each variable the ISR function uses must be declared as volatile. There are some things to have in mind when you are writing an Interrupt Service Routine:

  • Keep it short (This is very important)
  • Don’t use delay
  • Don’t do serial prints
  • Make variables shared with the main code volatile

——————–

LIBRARIES
——————–

📥 Low Power Library: https://github.com/rocketscream/Low-Power

 

Download the example code for the Arduino Interrupts Tutorial below:

——————–

CODE OF THE PROJECT
——————–

Download

 

 

——————–

SUBSCRIBE ON YOUTUBE

——————–

Never miss a video: Subscribe to educ8s.tv[/fusion_builder_column][/fusion_builder_row][/fusion_builder_container]

2017-03-28T12:43:53+00:00
Close