top of page
Writer's pictureTasnemul Hasan Nehal

Arduino Interrupt

Objective:

We will use an interrupt signal to show Room Temperature on Serial Monitor. We will use the LM35 temperature sensor for this practical.


Theory:

Interrupting the Arduino means telling it to suspend what it has been doing. For example- let's say, an Arduino is continuously blinking LED1 at 1-sec interval. "Blinking LED1" is a job that is known as the "Main Line Program (MLP)" or simply saying the main job.


Now, interrupting the Arduino by pushing the external switch we can ask the Arduino to suspend the MLP ("Blinking LED1") and then blink LED2 only 3 times at 2-sec interval and then resume the MLP. Here this "Blinking LED2" is a side job for our Arduino which is known as "Interrupt Service Routine (ISR)".


Whenever any interrupt signal occurs this " ISR() " will run automatically suspending all the other tasks.



Hardware Setup:



Full Code:

volatile bool flag = false;


void setup() 
{
  Serial.begin(9600);       //Serial Monitor & MCU communication starts
  pinMode(A0, INPUT);                              //LM35 reading input
  pinMode(2, INPUT_PULLUP);                        //Switch input

  attachInterrupt(digitalPinToInterrupt(2), ISRINTZ, LOW);
  
  analogReference(INTERNAL);            //select LM35 reference voltage
} 



void loop() 
{
  if( flag == true )                        //interruption found signal
  {
    float data=analogRead(A0);                      //reading from LM35
    float t=100*(1.1/1024)*data;                        //LM35 equation
    Serial.println(t, 2);
  }
  flag = false;                                   //reset for next loop
}



void ISRINTZ()
{
   flag = true;                                 //confirms interruption
}



Code Explanation:

volatile bool flag=false;

By using "volatile" we specified/fixed a memory location for "flag" which is a "bool" type variable, so next time when we need to access this variable compiler will know the exact location, the compiler won't need to reload and recheck variable's current/new memory location, unlike regular "int, float, char, bool" type variable. Learn more about it.

attachInterrupt(digitalPinToInterrupt(2), ISRINTZ, LOW);  

We installed an interrupt signal at Dpin-2. Whenever any interruption will occur or any signal found at Dpin-2 "ISRINTZ" function will run automatically ignoring other work. "LOW" is a triggering option. When the pushbutton is pressed "ISRINTZ" function runs and through the "flag" variable, it tells the loop function that an interruption just occurred so stop doing other tasks and show the temperature on Serial Monitor.

analogReference(INTERNAL); 

To get a reading from LM35, we first need to set a reference voltage which helps to change the sensor's reading resolution. More resolution means more accurate results Here INTERNAL = 1.1V. If we dont mention it, the reference voltage will be DEFAULT =5V.

Serial.println(t, 2);  

Prints only 2 digits after floating-point.

Comentários


Os comentários foram desativados.
bottom of page