Arduino Leonardo error

I got my Arduino Leonardo connected to my computer for the first time and it's coming up with an error...

I ignored the error and used the Arduino compiler to upload the code to the Leonardo

Here is the code:

void setup()
{
  pinMode(2, INPUT);
  digitalWrite(2,HIGH);
  
  Keyboard.begin();
  
  while(digitalRead(2))
  {
  }
  
  Keyboard.println("Arduino Leo Keyboard function test");
  delay(1000);
  Keyboard.println("Test version 1.0.1");
  delay(1000);
  Keyboard.write(48); // character 0
  delay(2500);
  Keyboard.press(KEY_BACKSPACE);
  delay(1000);
  Keyboard.releaseAll();
  
}

void loop()
{
}

When I try to upload, an error message pops up: "Couldn't find a Leonardo on the selected port. Check that you have the correct port selected. If it is correct, try pressing the board's rest button after initiating the upload." (There is a reset button but it doesn't seem to work... :cold_sweat: or I'm not using it right)

When I click "Serial Port Monitor" on "Tools" bar, again an error message pops up: "Serial port 'COM6' not found. Did you select the right one from the Tools > Serial Port Menu?"

Questions:

  1. How do I rest the Arduino Leonardo board?
  2. How do I upload any code to Arduino Leonardo board?

Immediately after starting the upload press and release reset on the Leonardo. With luck it should upload correctly then.

Still comes up with that same error... :frowning: (I've tried many different USB ports on my laptop...)

Same problem in a different thread: Arduino Forum
The solution there fixed this problem for me too.

When uploading the programs ,press the reset button immediately. And then ,in this moment ,find Tool > port > COM4(the port you used) before uploading be done. It's work!!

why is that happening with Leonardo ? - I've never had such problems with 328-based and Mega arduinos ?

I have a similar happening, I have a Leonardo clone (A-Star 32U4 Prime LV microSD).
I was using 1.6.? and with simple read analog test prog. The Serial Monitor would only occasionally work - most of the time it would not even open.
So - foolishly - I upgraded to 1.8.2 which promptly blew up - would not even upload the compiled code.
The averdude looked for COM5, didn't find it, found COM4 started upload and blew on Java unhandled error - several pages of it. At least 1.6.? uploaded OK, just would not do serial monitor most of the time - when it did it found it on com4 - when it was unhappy it just looked at com5 and refused to open.
Not very happy, but is that the price I pay for not buying a real Leonardo ?
I could clutter this post with the java error log, but will only give it if asked.
Regards Mike

Have you selected the correct port from the Tools > Port menu?