Leonardo -- Serial Issues

I currently have a sketch that relies on Serial communication to operate. It works with no issues on the Uno and Mega, but I wanted to get it working on the Leonardo...

So far I can't get anything Serial related to work.

As a test I dropped the blink example sketch onto the board and got the blinking LED. Then I added simple serial setup and print to see if that would work... Nothing. The code I'm using is below. You'll notice the "while !Serial" in the setup function is commented out. With that in, I believe the Leonardo is hanging infinitely because with it in the LED does not blink. Also, if I use the serial monitor and send it that sketch data, the RX LED on the board flashes, so it seems to be receiving the serial data... Any help would be really appreciated.

***NOTE: The code below shows "Serial1" calls. Originally I was using "Serial" calls, but both do the same exact thing...

Thanks Dave

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
 */

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  Serial1.begin(9600);
//  while (!Serial) {
//    ; // wait for serial port to connect. Needed for Leonardo only
//  }

  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(500);               // wait for a second
  
  Serial1.println('Hello world');
}

Of couse it hangs infinitely. You start Serial1 and wait for Serial to start. Do you start your car and wait for your truck's motor to make a sound?

Good catch. With that fixed the LED blinks, but I don't get the "Hello World" in the serial monitor.

-Dave

You are sending that print to Serial1, which is not connected to the COM port on your USB connection. Change all instances of Serial1 to Serial, and it should work.

That's how I started out. It doesn't work. The LED doesn't even blink so I assume its stuck in the setup loop. When I send data in the serial monitor the RX led flashed, but I don't see anything printed. I've even run an external serial monitor and I get nothing output from the board.

Here's the code I'm currently using so I can switch between Serial and Serial1 really easy w/o the previous typo...

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
 */

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

#define SERIAL Serial

// the setup routine runs once when you press reset:
void setup() {                
  SERIAL.begin(9600);
  while (!SERIAL) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(500);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(500);               // wait for a second
  
  SERIAL.println('Hello world');
}

SERIAL != Serial, it's Serial.begin.

define SERIAL Serial

I did that so I could switch between two. Quick and dirty test...

daufderh: That's how I started out. It doesn't work. The LED doesn't even blink so I assume its stuck in the setup loop. When I send data in the serial monitor the RX led flashed, but I don't see anything printed. I've even run an external serial monitor and I get nothing output from the board. Here's the code I'm currently using so I can switch between Serial and Serial1 really easy w/o the previous typo...

Your code, as posted in the posting I am replying to, works fine on my Leonardo. There is one thing wrong with it, though.

SERIAL.println('Hello world');

Needs to be

SERIAL.println("Hello world");

Curious. I changed the single to double quotes and still no joy. Same exact behavior. Here's what I'm doing:

  • Holding down the reset button on the Leo
  • Selecting upload in the Arduino IDE to load the exact sketch in this post (with the single quotes changed)
  • When it says upload complete I release the reset button
  • When the LED starts blinking I open the serial monitor
  • I see no "Hello World", but if I send data to the Leo I see the RX light flash
  • I get sad and go get a drink

I'm using Arduino ERW 1.0.5. Does that matter? I doubt it because I started down this road because my "real" app can't communicate with the leonardo either. It can receive data from it (sometimes), but it can't send data to it at all, so the problem is a bit different than what I have here, but my guess would be that it's all related.

Any other suggestions/ideas? Thanks Dave

Holding down the reset button on the Leo
Selecting upload in the Arduino IDE to load the exact sketch in this post (with the single quotes changed)
When it says upload complete I release the reset button

Why? The IDE will reset the Arduino at the appropriate time. Stop f**king with the reset switch. Use the Leonardo as intended.

No need to be getting angry mate or do you enjoy scaring beginners away from this forum?

Daufderh all you should be doing is plugging the board into USB and pressing upload.. There is no need to be touching the reset button during this process.

Oh man... Not scared away, you gotta be use to and filter out the amazingly useful help provided by people like PaulS if you're going to use forums.

As a more helpful response to the last two posts, I've been doing tons of googling and it seems like I'm not the only person with these kinds of serial issues with the Leo. I've yet to read anything that's helped. One of the docs I read talking about the Leo said flashing the way I have been might be needed. How do you think I learned how to do it? I started doing it, because depending on how the sketch is setup, I can't even get blink to work. In fact I take back my last post. The sketch as it is in the last post (with or without the single quote change) doesn't get passed the initial while Serial loop and hangs in the setup call so as is the LED isn't even blinking for me. This was the behavior I had when I first started and until I got a bit further I became worried the sketch wasn't uploading to the board correctly. Thus the reset...

Anyway, my question still stands. Why does the last code block work for lar3ry (with the double quote change), but not for me? That implies the code is fine, but that there's an issue with library versions, the hardware itself, or my pc environment. (which is a mac, running a VM running win7).

Seems most likely to me its a library thing, but my limited knowledge says the libraries come from the IDE install, to which I'm using ERW 1.0.5

-Dave

Does it say something like Setting baud rate to 9600 on COM94 in the console when the upload has completed?

Let's move passed the reset/uploading thing. It's not the problem. The board is being properly updated with the sketch.

Yes, but it happens that the Sketch is sucessfully uploaded to the Leonardo (especially if you reset while uploading), but then no serial connection is established for communication between PC and the Arduino.

No change in behavior.

  • Pluged arduino in
  • Selected com port in IDE
  • Hit upload
  • Upload succeeded
  • LED is NOT blinking
  • No "Hello World" in the port monitor
  • Sending data in the port monitor flashes the RX leds

I did not touch the reset button... If I use Serial1 for everything the LED blinks so I don't get stuck in the setup call, but nothing else changes so its still not working.

-Dave

Ok, it may sound generic, but have you tried rebooting your PC without the board connencted? It alway helps if the Serial connection of my Leonardo refuses to work.

Yeah I've done that. I even tried a second Windows7 machine. This one is native (no VM) and it has the same exact problems. I tried to test it on my mac directly, but I can't get my mac to pickup the Leonardo. It doesn't show up. I'll play around with that a bit more, because I am curious if its OS specific...

I'm seriously considering just not supporting the board. The software I provide works flawlessly on the Uno and Mega. I really don't see a need to use the Leonardo except that a user might already have one...

Try this,

Void setup(){
 Serial.begin(9600);
 while(!Serial);
 Serial.println("test");
}

Void loop(){

}

The while loop never comes back... If I use Serial1 in the begin and while loop it does come back though.