Leonardo USB won't stay connected

I have a Leonardo board and was happily uploading sketches, no problem.

Then after one upload (I'm not sure it finished successfully) my leonardo now automatically disconnects from windows (win 7, 64 bit) after about 12 seconds. Too quickly for me to upload another sketch. The COM port comes up, but it's like the device itself crashes and takes down the USB connection with it. (Maybe having a seperate USB processor was a good thing afterall :)

There was nothing being printed out to the serial port in the sketch so I can't tell if that's working (nothing appears on the serial monitor)

The orange L LED glows up and down as normal after booting (about 10 seconds), then goes blank. A few seconds later the USB disconnects. The green ON LED stays on. Power is still being supplied down 5v etc (I have a gameduino attached, my code appears not to be executing, though it was the first time non-sample code was uploaded) Disconnecting everything from the board makes no difference (so it's not to do with the gameduino)

I've tried a couple of other USB ports on my machine, pressing reset just causes it to re-connect to windows... and disconnect again.

Does anyone know if there is something I can debug, some USB logs in windows? some way I can flash the arduino again? (maybe a command line tool might be faster with a very-blank pre-compiled sketch?)

Okay, with a bit of luck and good timing, I managed to get blink to compile & upload before it disconnects and the problem has gone away!

Clearly my code did mess up the USB connection in someway...

SoylentGraham:
Okay, with a bit of luck and good timing, I managed to get blink to compile & upload before it disconnects and the problem has gone away!

Clearly my code did mess up the USB connection in someway…

Not likely. If it happens again, try putting a USB Hub in-between the PC and the Leonardo. It smooths these glitches for me.

I have boiled my code down to a vague area and can re-create the issue. (Still getting it down to specific code)

Everytime I upload this sketch it causes the issue listed above, and uploading the Blink sketch fixes it again.

I'll try it with a hub inbetween later when I investigate this some more. (and find specific code which is causing this)

FYI, this seems to occur when I have a variable too large on the stack.

In this case 4k/4096 bytes...

Well, that would throw a wrench into the upload ;) At least you have that extra 0.5kb SRAM over the Atmega328 :p

Nice troubleshooting!

Hey mine goes off at 5 sec interval not even able to upload the blink sketch!!! What should I do?

That is one fundamental disadvantage of using a Leonardo type board Vs a standard UNO type board. The Uno uses a separate chip dedicated to just providing the USB serial function, and the sketch runs in the separate 328p chip. So a users sketch cannot cause loss of USB functionality no matter how flawed or crashed the code causes. On the Leonardo board the one and only chip has to perform both the serial USB function as well as executing the users sketch, and if the users sketch has logical errors or crashes due to say using too much SRAM it can prevent the USB function to operate and cause PC USB problems.

I could argue that one should only utilize a Leonardo type board only if the project requires interfacing with a USB slave device. Otherwise certainly beginners are better off utilizing a Uno type board. Lefty

ok.. i had the same problem and also with 5 sec time before it disconnects i fixed it by uploading an empty code(i assumed that it will decrease upload time)

-first i connected to pc, waited for arduino to disconnect -then pressed reset button on arduino board and simultaneously pushed code upload button in software -when the sketch was still uploading and arduino disconnected i immediately pushed reset button again and it somehow worked for me(after 2-3 time)

now my board is working again

!just for those who encounter the same problem! try to play around with /reset button/empty code/uploading code in the right time

I experienced the same issue. But the problem was I used to much power so untill a full led strip was lit, it worked. My solve was to disconnect all hardware and upload an empty script and the board started working.

  • Led strip was 2m Adafruit NeoPixel

Eventually I just outputted 50 percent of power per led and the problem was fixed.

Yup. Somehow a weird bit of code can get in the way of the USB connection staying up. I was able to do the RESET reboot and very quickly upload an empty sketch. THat seems to fix the issue. Now to find the bit of code causing the issue in the first place…