Can't print to COM port with Leonardo

My Sketch works fine on UNO but results don't print to com port with Leonardo. I purchased both the Leonardo and a USB cable from Arduino.

I am trying to learn about pointers.

Here is my sketch:

void setup() {
  Serial.begin(9600);
  
  int myVar = 10;  // Initialize a variable.
  
  Serial.print("myVar's lvalue: ");
  Serial.println((long) &myVar, HEX);  // Grab myVar's lvalue
  Serial.print("myVar's rvalue: ");
  Serial.println(myVar, DEC);
  Serial.println();
  
  int *myPointer;   // Declare your pointer.
  myPointer = &myVar; //Assign myVar's memory address to pointer.
  
  Serial.print("myPointer's lvalue: ");
  Serial.println((long) &myPointer, DEC);  //myPointer's lvalue
  Serial.print("myPointer's rvalue: ");
  Serial.println((long) myPointer, DEC);  //myPointer's rvalue
}

void loop() {
}

You might want to stick a dirty delay between serial begin and print

The Arduino Uno has RX and TX pins. You can send whatever you want to the TX pin, regardless if that is used or not.

The Arduino Leonardo has a USB CDC device inside the microcontroller. First you have to wait for the Serial Monitor to open in a waiting loop, then you can send data.
If the Serial Monitor is never opened, the sketch might halt in that waiting loop.
Here you see that the Leonardo has a USB CDC device: https://www.arduino.cc/reference/en/language/functions/communication/serial/
Here you see the solution: https://www.arduino.cc/reference/en/language/functions/communication/serial/ifserial/

I tried a delay of 1 second and a delay of 3 seconds, neither works.

I tried

while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB
  }

Still nothing.

Works here

void setup()
{
  Serial.begin(9600);
  while(!Serial);

  int myVar = 10;  // Initialize a variable.

  Serial.print("myVar's lvalue: ");
  Serial.println((long) &myVar, HEX);  // Grab myVar's lvalue
  Serial.print("myVar's rvalue: ");
  Serial.println(myVar, DEC);
  Serial.println();

  int *myPointer;   // Declare your pointer.
  myPointer = &myVar; //Assign myVar's memory address to pointer.

  Serial.print("myPointer's lvalue: ");
  Serial.println((long) &myPointer, DEC);  //myPointer's lvalue
  Serial.print("myPointer's rvalue: ");
  Serial.println((long) myPointer, DEC);  //myPointer's rvalue
}

void loop()
{
}

Output

myVar's lvalue: AFA
myVar's rvalue: 10

myPointer's lvalue: 2808
myPointer's rvalue: 2810

I've moved your topic to a more suitable location on the forum.

That code works on my UNO but not on my Leonardo.

I am wondering if I have a bad USB cable? But, I bought them both from Arduino at the same time.

Close serial monitor, upload sketch, open serial monitor.

What do you see? Any errors in the black output window of the IDE?

Which operating system are you using?

No errors in the black output window. The COM5 window is open but nothing printed to it.
I am using Windows 8.1 operating system.
After some time I get the following error message

Arduino: 1.8.16 (Windows 8.1), Board: "Arduino Uno"

Sketch uses 2010 bytes (6%) of program storage space. Maximum is 32256 bytes.

Global variables use 264 bytes (12%) of dynamic memory, leaving 1784 bytes for local variables. Maximum is 2048 bytes.

avrdude: ser_send(): write error: sorry no info avail

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x89

avrdude: ser_send(): write error: sorry no info avail

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 2 of 10: not in sync: resp=0x89

avrdude: ser_send(): write error: sorry no info avail

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 3 of 10: not in sync: resp=0x89

avrdude: ser_send(): write error: sorry no info avail

avrdude: stk500_recv(): programmer is not responding

the selected serial port avrdude: stk500_recv(): programmer is not responding

 does not exist or your board is not connected

avrdude: stk500_getsync() attempt 4 of 10: not in sync: resp=0x89

avrdude: ser_send(): write error: sorry no info avail

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 5 of 10: not in sync: resp=0x89

avrdude: ser_send(): write error: sorry no info avail

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 6 of 10: not in sync: resp=0x89

avrdude: ser_send(): write error: sorry no info avail

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 7 of 10: not in sync: resp=0x89

avrdude: ser_send(): write error: sorry no info avail

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 8 of 10: not in sync: resp=0x89

avrdude: ser_send(): write error: sorry no info avail

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 9 of 10: not in sync: resp=0x89

avrdude: ser_send(): write error: sorry no info avail

avrdude: stk500_recv(): programmer is not responding

avrdude: stk500_getsync() attempt 10 of 10: not in sync: resp=0x89

processing.app.SerialException: Error opening serial port 'COM5'.

	at processing.app.Serial.<init>(Serial.java:152)

	at processing.app.Serial.<init>(Serial.java:82)

	at processing.app.SerialMonitor$2.<init>(SerialMonitor.java:132)

	at processing.app.SerialMonitor.open(SerialMonitor.java:132)

	at processing.app.AbstractMonitor.resume(AbstractMonitor.java:132)

	at processing.app.Editor.resumeOrCloseSerialMonitor(Editor.java:2126)

	at processing.app.Editor.access$1300(Editor.java:116)

	at processing.app.Editor$UploadHandler.run(Editor.java:2095)

	at java.lang.Thread.run(Thread.java:748)

Caused by: jssc.SerialPortException: Port name - COM5; Method name - openPort(); Exception type - Port not found.

	at jssc.SerialPort.openPort(SerialPort.java:167)

	at processing.app.Serial.<init>(Serial.java:141)

	... 8 more

Error opening serial port 'COM5'.



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

I'm interested in the upload to the Leonardo, not the Uno. Did you select the Leonardo as the board? Is COM5 the Leonardo or the Uno.

In the IDE, you should see something like below if you have both boards connected.

image

If you enable verbose output during upload, you should see the following few lines when the upload starts

Sketch uses 3462 bytes (12%) of program storage space. Maximum is 28672 bytes.
Global variables use 149 bytes (5%) of dynamic memory, leaving 2411 bytes for local variables. Maximum is 2560 bytes.
Forcing reset using 1200bps open/close on port COM13
PORTS {COM1, COM4, COM13, } / {COM1, COM4, } => {}
PORTS {COM1, COM4, } / {COM1, COM4, COM16, } => {COM16, }
Found upload port: COM16

I did see the reference to UNO. But, prior I had Leonardo on COM5. So I chose Leonardo again and it shows Leonardo COM5. I tried it again.
Now I get this message:
Board at COM5 not available.

What does Windows device manager say? Does it show the Leonardo? Should look like this

image

The Windows device manager shows:
Arduino Leonardo (COM4)
Arduino Leonardo(COM5)
Arduino Leonardo Bootloader (COM3)
Arduino UNO (COM5)
Arduino UNO (COM5)

I uninstalled everything, restarted windows and tried again.

Tools shows:
Board "Arduino Leonardo"
Port "COM5 Arduino Leonardo"

When I uploaded the sketch I didn't get an error message until I clicked on Serial Monitor, then I got this message:
Board at COM5 is not available.

Was that all at the same time? Windows seriously screwed up there.

OK, that's how it's supposed to be.

That means that Windows might have assigned a new port; check the IDE (and device manager if needed). Change the port and try to open serial monitor again.

Note:
I forgot to tell you that the verbose output settings are in file -> preferences.

When the Leonardo reboots the USB connection is lost and then remade .
As luck would have it , windows often assigns it a different com port number

1 Like

I restarted my computer, brought up Arduino with the sketch, plugged in the UNO board uploaded the sketch and it worked fine including the print out to Com port. And, when I reset the board, it worked again.

I restarted my computer again, brought up Arduino with the sketch, plugged in the Leonardo board, uploaded the sketch and it worked fine. But, when I reset the Leonardo board it gave and error message COM5 not available.

I restarted my computer again, brought up Arduion with the sketch, plugged in the Leonardo board, uploaded the sketch and it didn't work. I got the error message COM5 not available. The Device manager gave Arduino Leonardo (COM5) and the Tools gave Board: 'Arduion Lenardo" and port: "COM5 (Arduino Lenardo)"

Did you check if the COM port was indeed the correct COM port?

And now I'm at a loss.

I have never done it but maybe it's time to clean the COM stack. In device manager

  1. menu View -> Show hidden devices.

  2. For each greyed out Arduino that you see, right click and select Uninstall device.image

  3. In the popup, click Uninstall; do not tell it to delete the driver
    image

As said, I have never done this so you do this a little bit at own risk. But I think that the approach is safe.

One note: you don't have to load a sketch every time, once it is loaded it will stay in the Arduino.

Hey sterretje,
Thank you for your help! I appreciate your effort.
Yes I made sure I had the correct COM port. And,
I have tried uninstalling all the COM ports but, that didn't seem to help solve the problem. I just think my Windows 8.1 just doesn't like my Arduino Leonardo.

I have contacted Arduino customer support, I hope I can return the Leonardo and trade it in for another UNO. I am a very old retired ME and I don't need the hassle. The UNO seems to work great.

Must confess , I bought a Leonardo as it’s the same processor in the “ Latte Panda” - never liked it as the com port kept changing !

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