Leonardo fails to be detected with attachInterrupt() and Serial.begin()

I am trying to make an Arduino count pulses via an interrupt and output the number over serial line (USB) to a computer.
I got everything to work with an Uno, but since it resets every time something connects to the USB I decided to use a Leonardo.
Here I understand that both interrupts and serial ports are slightly different. I still don't understand why this simple sketch causes problems. I have removed any attempts at actually output data on the serial port in the example code.
Pin 13 is the LED, interrupt 0 should be pin 3 on the Leonardo. No pins are connected to anything yet, so I can trigger the interrupt
just by touching the solder around the pin on the board.

// pin for LED
#define LEDpin 13

// pin for interrupt
#define IRintr 0

// state change
volatile int state = HIGH;

// the rotation counter - number of rotations seen
volatile int rotations=0;


void setup() {

  // enable LED
  pinMode(LEDpin, OUTPUT);

  // use pin 0 for interrupts, triggered on falling level
  attachInterrupt(IRintr, blink, LOW);

  Serial.begin(9600); // serial output
}

void loop() {

  digitalWrite(LEDpin, state);

}


void blink()
{
    state = !state;
    rotations++;

}

When I upload the sketch, my syslog shows that the Leonardo is no longer recognized after a few seconds:

Feb 23 17:04:58 linux1 mtp-probe: checking bus 4, device 82: "/sys/devices/pci0000:00/0000:00:1d.2/usb4/4-1"
Feb 23 17:04:58 linux1 mtp-probe: bus: 4, device: 82 was not an MTP device
Feb 23 17:05:06 linux1 kernel: [251091.472084] usb 4-1: USB disconnect, device number 82
Feb 23 17:05:06 linux1 kernel: [251091.808075] usb 4-1: new full-speed USB device number 83 using uhci_hcd
Feb 23 17:05:06 linux1 kernel: [251091.932060] usb 4-1: device descriptor read/64, error -71
Feb 23 17:05:07 linux1 kernel: [251092.160064] usb 4-1: device descriptor read/64, error -71
Feb 23 17:05:07 linux1 kernel: [251092.376057] usb 4-1: new full-speed USB device number 84 using uhci_hcd
Feb 23 17:05:07 linux1 kernel: [251092.500063] usb 4-1: device descriptor read/64, error -71
Feb 23 17:05:07 linux1 kernel: [251092.728059] usb 4-1: device descriptor read/64, error -71
Feb 23 17:05:07 linux1 kernel: [251092.944063] usb 4-1: new full-speed USB device number 85 using uhci_hcd
Feb 23 17:05:08 linux1 kernel: [251093.360042] usb 4-1: device not accepting address 85, error -71
Feb 23 17:05:08 linux1 kernel: [251093.472056] usb 4-1: new full-speed USB device number 86 using uhci_hcd
Feb 23 17:05:08 linux1 kernel: [251093.888059] usb 4-1: device not accepting address 86, error -71
Feb 23 17:05:08 linux1 kernel: [251093.888102] hub 4-0:1.0: unable to enumerate USB device on port 1

Happens every time I reset, the delay appears to be from the Arduino is plugged in to the program (sketch) is running.
I can press the reset button and upload a new sketch before the error occurs.

How can I configure one pin for interrupts and output to USB serial on the Arduino Leonardo?

No pins are connected to anything yet, so I can trigger the interrupt

How? The interrupt is supposed to be triggered by setting the pin HIGH or LOW, depending on the interrupt type.

just by touching the solder around the pin on the board.

With what?

  attachInterrupt(IRintr, blink, LOW);

CHANGE makes sense. RISING makes sense. FALLING makes sense. LOW does not.

Let's ignore the problem of actually using the interrupt for something, I'll get there once I don't lose contact with /dev/ttyACM0 as soon as the program is running. Any idea why that is happening?
I wanted to show that if you upload the sketch into an Arduino Leonardo it stops behaving like it used to seen from the computers USB point of view, and that's where I don't understand how I went wrong.

You don't have the
While (!Serial)
;
Loop recommended for Leonardo serial use...

I added the loop for the serial port, and while that is a helpful comment, it unfortunately doesn't help against the disappearing USB device.
Same symptom; I plug the Arduino in and once the program is running I get the messages in syslog that I initially posted and can no longer talk to it. If I try to open the Serial Monitor in the IDE there is no port for it to communicate with.

Anyone else with some suggestions how I can use interrupts and USB serial in the same sketch on Leonardo?

When I upload the sketch, my syslog shows that the Leonardo is no longer recognized after a few seconds:

When you upload a sketch or otherwise reset the arduino, the USB serial connection WILL shut down and get re-discovered after the sketch finishes initializing. I don't think there is any way to avoid this; it's part of the way that USB works.
Are you complaining about that behavior, or are you saying that the serial port doesn't come back (even after several seconds)?

Correct. It is not re-discovered as a valid USB device. Same issue on two Leonardo boards.

In my initial post, the sketch has been uploaded and the Arduino reset.
At 17:04:58 it is detected as a USB device, but the following syslog lines from 17:05:06 until 17:05:08 are errors from the USB layer in Linux when the program starts up. After this I no longer have a /dev/ttyACM to talk to.

Hi there,

just encountered the same problem. no answer yet? :o Only Interrupt 0 on the Leonardo is somehow in conflict with the USB communication. So, do not use Interrupt 0 which is pin 3 on the Leonardo.

Any other interrupt works fine. So use one of these pins on the Leonardo: pin 2 (interrupt 1), pin 0 (interrupt 2), pin 1 (interrupt 3) and pin 7 (interrupt 4).

Have fun! :slight_smile:

Try this

void setup() {

  Serial.begin(9600); // serial output
  While (!Serial);  // [b]This need ONLY FOR LEONARDO Boards ! ! ![/b]

  // Rest code here

}