Leonardo won't initiate serial communication

Hi,

I'm trying to build a MIDI controller using an Arduino Leonardo.
I use a simple code to send MIDI notes to the Serial port and from there to a Serial->MIDI driver, then it goes to a loopback MIDI driver and into MAX MSP.

I tried on the Arduino Duemilanove and it was flawless, but the Leonardo doesn't seem to work no matter how I connect it, or what I do. :frowning:

It is probably because of the way the Leonardo communicates with the computer over serial, and maybe has to do with the reset procedure.

I would really like to make this work with the Leonardo
Thanks!

Hello omni96,

I'm experiencing a similar problem. I have this script:

void setup() {
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }  
  Serial.println("Hello Computer");
  Wire.begin();
  
  my3IMU.init(true);
  establishContact();  // send a byte to establish contact until receiver responds 
}

void loop() {
  
  my3IMU.getYawPitchRoll(ypr);
  
  float yaw = ypr[0];
  float pitch = ypr[2];
  float roll = ypr[1];

  if (Serial.available() > 0)      // Only send data when requested by FaceTrackNoIR
  {
    inChar = Serial.read();        // Read a character

    //
    // Assemble the string, before sending.
    //
    strcpy(str, "!roll\t");
    ftoa(strValue, roll, 2);
    strcat(str, strValue);
    strcat(str, "\tpitch\t");
    ftoa(strValue, pitch, 2);
    strcat(str, strValue);
    strcat(str, "\tyaw\t");
    ftoa(strValue, yaw, 2);
    strcat(str, strValue);
    strcat(str, "\t!\n");

    Serial.print(str);
  }

  delay(1);
}

void establishContact() {
  while (Serial.available() <= 0) {
    Serial.println("0,0,0");   // send an initial string
    delay(300);
  }
}

When I upload it to an Arduino Uno, the program works fine: it receives one byte from my PC, then sends the requested data.

Now I am running the same script on a Leonardo, which seems to have a different way of handling Serial communication (and has a different virtual COM driver).
When the script is loaded, my PC sends one byte messages to get data from the Arduino. However, the Leonardo does NOT respond (as did the Uno...).

Now comes the foggy part: I stop the program on my PC and start the Serial Monitor. Then I send one byte, using the button "Send". Voila! The Arduino comes to live!
When I now stop the Serial Monitor and start my program on the PC, everything works fine! Even stopping and starting my program does not confuse the Leonardo.

So my questions are:

  • Why does the Leonardo not respond like the Uno?
  • What does the Serial Monitor do, to revive the Serial port?

Any help is appreciated!

Hi V4Friend!

Actually I forgot about this post, and while I forgot about it, I actually stumbled upon the same problem!
Serial monitor seems to "release" the connection and let Arduino communicate with the PC.

I have requested support from the Arduino team on email, and if I'll have any news, I'll update here.

Or.

Hi omni96,

Thanks for your response. I'm checking this a bit late (forgot to press the Notify button :*).

Anyway: I figured out why it didn't work. I started a Windows command-prompt and with 'mode COM4' I got more info on the serial port. I noticed that, when I started Serial Monitor, the values of DTR and RTS changed!
I don't know why, but it seems like the Uno doesn't bother and the Leonardo does...

DTR must be set to ON and RTS must be OFF. I changed the code in my Serial class and voila: it works fine :sweat_smile:

Hope this helps you too!

Hi V4Friend,

I have been scratching my head on how to adjust the DTR and RTS bits in the serial class. Would you have an example?

omni96:
Hi V4Friend!

Actually I forgot about this post, and while I forgot about it, I actually stumbled upon the same problem!
Serial monitor seems to "release" the connection and let Arduino communicate with the PC.

I have requested support from the Arduino team on email, and if I'll have any news, I'll update here.

Or.

If you are using the serial port on pins D0 and D1 on the Leonardo, I think you need to use serial1.begin etc...

hgmjr

spcomputing:
Hi V4Friend,

I have been scratching my head on how to adjust the DTR and RTS bits in the serial class. Would you have an example?

Hi spcomputing,

Sorry, but it seems my previous post was not clear enough :*. I don't think you can adjust DTR and RTS from the Arduino-side.
I am communicating between my PC and the Leonardo and I have edited the PC-code, to adjust the settings.

@hgmjr: I'm not using the pins, but the default USB-connection...

Hope this helps?

Can anyone here shed some light into how to get the Arduino Leonardo communicating via Serial with another program?
I don't quite understand the method described above and I have been trying for 4 days now with no luck.

I have also tried Serial1() rather than Serial() with no success.

Thank you.

The leonardo only sends out data if the program/os on the pc sets either DTR or RTS, (or both) high. If you use a program like putty or the serial monitor in the IDE to communicate with the leonardo, there is no problem because these programs set DTR and/or RTS. If you do serial communications from your own program you must make sure yourself to set DTR/RTS.

I have been trying for 4 days now with no lunck.

Can you tell us more about what you try to accomplish? And on which os, version, sp...?
You may observe the same problem as what is described in this thread but it might be something completely different as well.

I am trying to get Analog input into Processing.
Running Arduino 1.0.3 and Processing 1.5.1 on Windows 8.
I should mention, I am trying this with an Arduino Micro, but I was told it is basically the same as the Leonardo and there is much more documentation out there on the Leonardo (so I have been searching forums for Leonardo related posts).

I am looking at the pin diagram right now, I do not see DTR or RTS pins... I see RST on the board, but cannot make it which pin it actually is.

Sorry for the lack of knowledge about this board. I picked it up without realize it was much different than the Uno. I appreciate the help.
Thank you.

I just finished a thread with a similar problem. Are you using 1.0.3? My problems cleared up when I installed the newer version.

I am looking at the pin diagram right now, I do not see DTR or RTS pins... I see RST on the board, but cannot make it which pin it actually is.

You are using the leonardo's virtual com port, so DTR and RTS are no real pins. The pc can however send control messages to "set" or "clear" these "virtual" DTR and RTS signals. And the leonardo's core will only send out data if DTR or RTS are set by the pc.

I am trying to get Analog input into Processing.

So you're processing app should set DTR/RTS. I don't know whether the Processing framework does this for you or how you can do this yourself.

Maybe it is best that you first try to run a simple app that sends out some serial data and see whether you can receive the bytes from the Arduino-1.0.3 serial monitor. Maybe use wmassano's sketch:http://arduino.cc/forum/index.php/topic,137032.0.html

I just tried out serial comms between processing and the leonardo, myself. On the pc I did not need to do anything special to set DTR/RTS, so apparently processing (or the underlying rxtx) does this for you.

I have a better test sketch (for the leo/micro):

static const int led = 13;
unsigned char x;

void setup() {
  Serial.begin(9600); // Start serial communication at 9600 bps
  pinMode(led, OUTPUT);
  digitalWrite(led, Serial ? HIGH : LOW);
}

void loop() {
  Serial.write(x & 1);
  digitalWrite(led, Serial ? HIGH : LOW);  
  delay(1000);  
  x++;
}

It alternately sends out 0 and 1. Also, via the arduino led, it lets you observe DTR/RTS: Serial (or rather Serial's boolean operator) returns true when either DTR or RTS are high.

Run this sketch on the leo.
As long as the PC does not open the comm port, the leo's (or micro's) arduino led and the tx led remain dark. The leo runs its loop but all written bytes are dropped by the core.

Then, on the PC, fire up the standard processing example Libraries>serial>SimpleRead.
The arduino led lights up and the tx led flashes.
(and SimpleRead alternately shows a gray or black rectangle).

Note the behavior is different if you run the sketch on an Uno or Due (programming port). For these boards the tx led keeps flashing even if SimpleRead does not run (port not opened by the pc). (though I find that the leo's behavior is ok in this situation).

I tried this on linux 3.0.0-12 (kubuntu) and on windows 7 SP1. On windows I had to dowload the 32 bit version of processing because Serial does not run in 64 bit mode.

Could try whether this basic setup works for you?

Thank you all for your help. I am mobile right now so I can't test anything out yet, but I will as soon as I can. I'll post back when I do try it out.

We have to transmit data from arduino uno to arduino leonardo. We are facing trouble in receiving the data on leonardo. We checked out with basic led blink code also. Same trouble in this case as well. Arduino uno is transmitting the data. There is no trouble in that. we checked using Serial1 as well. We checked by creating object(using software serial) as well. Please help.
Thank you

I realise this is an old thread, but I'm just commenting to thank V4Friend for the hint about DTR.

I have an Arachnid Tsunami (Leonardo based device) and I wrote an extensive vb.net program to control it.

I did all my initial testing on an Arduino Nano and everything worked fine.

As soon as I switched to the Tsunami (Leonardo), I was unable to receive data from the serial port (sending seemed to be ok).

This is my vb.net serial initialisation code which seems to work fine now.

                SerialPort1.PortName = comPORT
                SerialPort1.BaudRate = 115200
                SerialPort1.DataBits = 8
                SerialPort1.DtrEnable = 1     ' enable DTR *****
                SerialPort1.Parity = Parity.None
                SerialPort1.StopBits = StopBits.One
                SerialPort1.Handshake = Handshake.None
                SerialPort1.Encoding = System.Text.Encoding.Default
                SerialPort1.ReadTimeout = 10000

Also, in the Arduino I added this:

  Serial.begin(115200);
  while (!Serial)
  {}