Arduino Leonardo Disconnecting During Sketch Upload

Hello guys,

my arduino Leonardo was working perfectly the last days, but today when i tried to upload a sketch it disconnected right after I hit the upload button. I tried with different sketches, but still no luck. I have red about the reset and upload sketch trick but that doesnt fix the problem for me.

I am getting this error:

Connecting to programmer: .
Found programmer: Id = "CATERIN"; type = S
    Software Version = 1.0; No Hardware Version given.
Programmer supports auto addr increment.
Programmer supports buffered memory access with buffersize=128 bytes.

Programmer supports the following devices:
    Device code: 0x44

Which version of the IDE?

Does it disconnect immediately when you click the upload button or when the actual upload starts. The latter is normal.

The output that you posted seems to show normal behaviour for IDE 2.0.

//Edit
If it's IDE 2.0, you have to pay attention to the notifications in the right bottom of the IDE to know if it was succesful or not.

I am using IDE 2

It disconnects when the actual upload starts. The thing is that for simple projects, such as the blink , even though it disconnected , it has uploaded the code cause the led blinks.

But when i try to upload a more complex sketch, I am trying to make leornado a joystick, it doesnt actually upload the code. It disconnects completely and I have to reset it to get it to work again and it is very weird because yesterday it was working perfectaly.

After the last scenario the console prints:

avrdude: ser_open(): can't open device "\\.\COM20": ��� ����� ������ � ������ ��� ������������ ������� ��� �� �������.


Failed uploading: uploading error: exit status 1

Check what happens in Windows device manager; is the port still there? Note that it might have moved.

Part of the code that is uploaded is responsible for board detection and for the detection of the software reset (not the hardware reset by the reset button).

A buggy sketch can overwrite variables used by that part if the code. If that happens, your board is either not detected (your case) or it does not react on the software reset.

Time to show your sketch. Please use code tags; just in case you don't know how, see How to get the best out of this forum.

When I plug my leonardo in my pc, my device manager shows the port. But as soon as I upload the sketch, it disconnects and when i hear the sound of it reconnecting it appears as an uknown usb device in device manager and thus not uploading the sketch successfully and it gives me the error.

I coppied the code from a tutorial I show online, so I dont think that this is the source of the malfunction.

// Testing USB joystick function using Arduino Leonardo
// using MPU6050 as analog x, y and z axis input
// Code written by Pin Chung  
// Sep 22, 2020

#include <Joystick.h>
#include <MPU6050_tockn.h>
#include <Wire.h>

MPU6050 mpu6050(Wire);
Joystick_ Joystick;


#define ResetPin 13
#define Joystick1Pin 9

int lastButtonState=0;
int currentButtonState;

float zeroX=0;
float zeroY=0;
float zeroZ=0;

//upper and lower limit on the angle of the MPU6050 board range.
int upperLimit = 45;
int lowerLimit = -45;

void setup() {
  pinMode(Joystick1Pin, INPUT_PULLUP);
  pinMode(ResetPin, INPUT_PULLUP);
  
  Serial.begin(9600);
  Joystick.begin();

  Wire.begin();
  mpu6050.begin();
  mpu6050.calcGyroOffsets(false); //This is is not a necessary call. works fine without it.

}

void ResetMPU6050(float x, float y, float z){
  //Center the Joystick at current position
  zeroX=x;
  zeroY=y;
  zeroZ=z;
}

void loop() 
{
  mpu6050.update();
   float x=mpu6050.getAngleX();
   float y=mpu6050.getAngleY();
   float z=mpu6050.getAngleZ();
   
  if (!digitalRead(ResetPin)) {
    ResetMPU6050(x,y,z);
    }
    
   currentButtonState=!digitalRead(Joystick1Pin); 
   if (currentButtonState != lastButtonState){
      Joystick.setButton(0, currentButtonState); 
      // 0 is joystick button #1 in Windows 10
      lastButtonState=currentButtonState;  
   }
  
  x-=zeroX;
  y-=zeroY;
  z-=zeroZ;
  
   if (x>upperLimit) x=upperLimit;
   else if (x<lowerLimit) x=lowerLimit;
   if (y>upperLimit) y=upperLimit;
   else if (y<lowerLimit) y=lowerLimit;
   if (z>upperLimit) z=upperLimit;
   else if (z<lowerLimit) z=lowerLimit;


 // Map the angles of x, y and z to joystick position 0 to 1024
 // to reverse the axis, switch the last 2 numbers in the map() function
   Joystick.setXAxis(map(x, lowerLimit,upperLimit, 0,1024));
   Joystick.setYAxis(map(y, lowerLimit,upperLimit, 1024, 0));
   Joystick.setZAxis(map(z, lowerLimit,upperLimit, 1024,0));

}

I have been able to partially reproduce it.

The standard Leonardo's COM port on my system was 20. After uploading your sketch in IDE 2.0 it changed to COM32 (Windows device manager) which was not picked up by the IDE.

I closed the IDE and tried IDE 1.8.5 and it it showed COM32; upload of your sketch succeeded.

After that I closed IDE 1.8.5 and opened IDE 2.0; now COM32 was available and upload of your sketch succeeded.

After that, I toggled between uploading your sketch and another one and the only thing that changed was the COM port (COM20 to COM32 to COM20 etc); the IDE 2.0 followed Windows device manager.

After that I loaded an innocent sketch, rebooted my PC, started the proces again but this time the IDE did pick up COM32 immediately after uploading your sketch; it's happily following what Windows device manager sees. So I can no longer reproduce the issue :frowning:

I have a vague recall of a bug report; maybe @ptillisch's memory is better.

Hi @zankostas. I'm going to ask you to post some additional information that might help us to identify the problem.


:exclamation: NOTE: These instructions will not solve the problem. They are only intended to gather more information which might provide a clue that eventually leads to a solution.


Please do this:

  1. Select File > Preferences from the Arduino IDE menus.
  2. Check the box next to "Show verbose output during: ☐ compilation".
  3. Click the OK button.
  4. Select Sketch > Verify/Compile from the Arduino IDE menus.
  5. Wait for the compilation to finish.
  6. Right click on the black "Output" panel at the bottom of the Arduino IDE window.
  7. From the context menu, click Copy All.
  8. Open a forum reply here by clicking the Reply button.
  9. Click the </> icon on the post composer toolbar.
    This will add the forum's code block markup (```) to your reply to make sure the error messages are correctly formatted.
    Code block
  10. Press Ctrl+V.
    This will paste the compilation output into the code block.
  11. Move the cursor outside of the code tags before you add any additional text to your reply.
  12. Click the Reply button to post the output.

In case the output is longer than the forum software will allow to be added to a post, you can instead save it to a .txt file and then attach that file to a reply here:

  1. Open any text editor program.
  2. Paste the copied output into the text editor.
  3. Save the file in .txt format.
  4. Open a forum reply here by clicking the Reply button.
  5. Click the "Upload" icon (image) on the post composer toolbar:
    image
  6. Select the .txt file you saved.
  7. Click the Open button.
  8. Click the Reply button to publish the post.

Alternatively, instead of using the "Upload" icon on the post composer toolbar as described in steps (5) - (7) above, you can simply drag and drop the .txt file onto the post composer field to attach it.

As soon as you asked what version of IDE I was using, I downloaded IDE 1.8.5 and it worked like a charm. I didnt bother switching again to IDE 2, cause my job was done.

Thank you guys for your help and your quick response!

I used 1.8.5 because it's what I had installed; the latest 1.8.x is 1.8.19 and I'm sure it would have worked equally well.

I'm sure that we would have liked to see the output requested in post #7 :wink:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.