element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino
  • Products
  • More
Arduino
Arduino Forum interrupts and Serial.println()
  • Blog
  • Forum
  • Documents
  • Quiz
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 7 replies
  • Subscribers 417 subscribers
  • Views 1880 views
  • Users 0 members are here
Related

interrupts and Serial.println()

kjhart0133
kjhart0133 over 13 years ago

Hello all,

 

I've been learning quite a bit about interrupts at the register level.  I have noticed one thing I haven't been able to find any reports of: apparently if you have a Serial.println() statement in loop it adversely affects the operation of the interrupts (at least the external interrupts.)  Below is a small bit of code I lifted from the web.  It works fine if the Serial.println statement in loop is commented out, but if I include it, once I press the pushbutton connected to digital I/O pin 2 (int0) the program crashes/hangs.  I don't think it has to do with switch bounce but I can't say that with 100% confidence.

 

Can anyone verify this or let me know if I'm doing something wrong.

 

Also, I apologize in advance, but my "Correct Answer" and "Helpful Answer" buttons don't seem to show up all the time, so if you do provide an answer, I may not be able to acknowledge it.

 

Here's the code:

 

#include <avr/interrupt.h>

 

int switchpin = 2;  //pushbutton connected to pin 2 (with pullup)

 

void setup(){

  Serial.begin(9600);

  pinMode(12, OUTPUT);

  pinMode(13, OUTPUT);

  pinMode(switchpin, INPUT);   //int0

  digitalWrite(switchpin, HIGH); //enable pullup resistor

 

  sei();

 

  //set up for external interrupt on int0

  EICRA = 0x03; //interrupt on rising edge of input to int0

  EIFR = 0x01; //clear interrupt flag

  EIMSK = 0x01; //enable interrupts

  

}

 

void loop(){

 

  //Serial.println("loop");

  digitalWrite(12, !digitalRead(12));

 

}

 

ISR(INT0_vect){

  Serial.println(random(500));

  digitalWrite(13, !digitalRead(13));

}

  • Sign in to reply
  • Cancel
Parents
  • billabott
    billabott over 13 years ago

    I don't know, but you could try this:

     

    void loop(){

     

      Serial.println("merry go round");

      delay(500);

      digitalWrite(12, !digitalRead(12));

      delay(500);

     

    }

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • kjhart0133
    kjhart0133 over 13 years ago in reply to billabott

    Ah, that would work, but sadly I'm also using timer 1 and timer 0 for this project and I've already found that delay() causes problems when using timer 0 (or vice versa.)

     

    I guess I'm just looking for verification that Serial.println() and external interrupts don't play well together.  I like to use Serial.println() as a debugging tool, placing it here and there in the code to gain some visibility into what the program is doing.  When using external interrupts, however, it looks like that's not an option.  <sigh>

     

    Thanks for taking the time to respond.

     

    Kevin H.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • billabott
    billabott over 13 years ago in reply to kjhart0133

    Okay, then.  Forget about attempting to overfill the output buffer in your tight loop(). 

    Create a long int someINT;

     

    Put this in the loop():  someINT++;

    When you want to print a debug line, include someINT in the output.

     

    And ISR is for doing a little something that is required as quickly as possible.

    IMO, a call to println does not belong in an ISR.

     

    For that matter; why are you making your sketch less readable by using AVR inline C code instead of 100% pure Arduino code?


    attachInterrupt()

     

    detachInterrupt()

    interrupts()

     

    noInterrupts()

     

     

                    Official Arduino Example

    int pin = 13;
    volatile int state = LOW;

     

    void setup()
    {
      pinMode(pin, OUTPUT);
      attachInterrupt(0, blink, CHANGE);
    }

     

    void loop()
    {
      digitalWrite(pin, state);
    }

     

    void blink()
    {
      state = !state;
    }

     

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • kjhart0133
    kjhart0133 over 13 years ago in reply to billabott

    Mr. Abott,

     

    You are 100% correct.  Through some trial and error I realize that Serial.println does not belong and doesn't work in the ISR.  It causes the Arduino to hang. As for using the AVR inline code, I'm using that because I want to learn how the Arduino works at its lowest level.  I did a lot of assembly and machine coding many, many years ago and it really gave me lots of insight as to how the machines I was using really work; and everything else seems simpler by comparison.  I don't expect I'll keep on coding this way once I've got a good fundamental understanding of the inner workings of the Arduino Uno.

     

    Thanks again for the "CORRECT ANSWER."  Sorry I can't click the button to give you credit; they don't show up for some reason.

     

    Kevin H.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • billabott
    billabott over 13 years ago in reply to kjhart0133

    I am just Bill.  Maybe you should try to update your browser or reinstall it.  That usually fixes that kind of problem.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
  • billabott
    billabott over 13 years ago in reply to kjhart0133

    Sir,

        Please pay attentention to the line above the completed post that says:

     

    Is this a question? You have 15 minutes to mark this discussion as a question.

     

    Clicking on the highlighted link will allow you to enable the "Correct Answer" feature of the discussion.

     

    N.B.  I have made an Admin request to have the programming changed to make this question into a mandatory check box befores posting; which, in my view, will be a great improvement.

     

    So, Thank you, Kevin!  You are helping to make e14 a better place.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Reply
  • billabott
    billabott over 13 years ago in reply to kjhart0133

    Sir,

        Please pay attentention to the line above the completed post that says:

     

    Is this a question? You have 15 minutes to mark this discussion as a question.

     

    Clicking on the highlighted link will allow you to enable the "Correct Answer" feature of the discussion.

     

    N.B.  I have made an Admin request to have the programming changed to make this question into a mandatory check box befores posting; which, in my view, will be a great improvement.

     

    So, Thank you, Kevin!  You are helping to make e14 a better place.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
Children
  • kjhart0133
    kjhart0133 over 13 years ago in reply to billabott

    Arrrgh!  That explains it!  Thanks, I'll be more diligent about clicking that in the future.

     

    Kevin

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube