# Leonardo Serial Commuincation works only in Serial monitor

**URL:** <https://forum.arduino.cc/t/leonardo-serial-commuincation-works-only-in-serial-monitor/346778>\
**Category:** Networking, Protocols, and Devices\
**Created:** [November 15, 2015, 2:53pm UTC](https://forum.arduino.cc/t/leonardo-serial-commuincation-works-only-in-serial-monitor/346778 "2015-11-15T14:53:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![zgravity](https://avatars.discourse-cdn.com/v4/letter/z/d9b06d/32.png) [@zgravity](https://forum.arduino.cc/u/zgravity)\
**Post date:** [November 15, 2015, 2:53pm UTC](https://forum.arduino.cc/t/leonardo-serial-commuincation-works-only-in-serial-monitor/346778/1 "2015-11-15T14:53:22Z")

</div>

Hi all,

This maybe is an old issue. I tried searching in the forum but in vain.

I am trying to communicate a Leonardo with an UNO through USB.  
Leonardo - OTG USB - USB - Uno(Type B connector)

Both the devices seem to work in the serial monitor of Arduino IDE(running in a Windows8).

But when i connect them, i am not able to get the serial data.

Kindly find my sketches attached.  
Uno sketch:

```cpp
#include <LiquidCrystal.h>

const char len_sTx = 8, len_sRx = 8;
byte sTx[len_sTx], sRx[len_sRx];

LiquidCrystal lcd(8, 9, 10, 11, 12, 13);

void setup() {
  // put your setup code here, to run once:
  Serial.begin(262144);
  lcd.begin(16,2);
}

void loop() {
  char i;
  if(Serial.available() > 0)
  {
    Serial.readBytes(sRx, len_sRx);
    sTx[0] = sRx[7];
  }
  lcd.setCursor(0,0);
  lcd.print("Rx:");
  for(i = 0; i < len_sRx; i++)
  {
    lcd.setCursor(i+3,0);
    lcd.print(sRx[i]);
    i++;
  }
  for(i = 1; i<len_sTx; i++)
  {
    sTx[i] = sTx[i-1] + 1;
  }
  Serial.write(sTx, len_sTx);
  lcd.setCursor(16,1);
  lcd.print("Tx:");
  for(i =0; i < len_sTx; i++)
  {
    lcd.setCursor(i+19,1);
    lcd.print(sTx[i]);
    i++;
  }
  delay(500);
}

```

Leonardo sketch:

```cpp
const char len_sTx = 8, len_sRx = 8;
byte sTx[len_sTx], sRx[len_sRx];
char flag_sTx = 0;

void setup() {
  // put your setup code here, to run once:
Serial.begin(262144);
while(!Serial){
  ;
}
}

void loop() {
  char i;
  // put your main code here, to run repeatedly:
  if(flag_sTx == 0)
  {
    sTx[0] = 49;
    flag_sTx = 1;
  }
  else
  {
    if(Serial.available() > 0)
    {
      Serial.readBytes(sRx, len_sRx);
      sTx[0] = sRx[7];
    }
  }
  for(i = 1; i<len_sTx; i++)
  {
    sTx[i] = sTx[i-1] + 1;
  }
  Serial.write(sTx, len_sTx);
  delay(500);
}

```

I am not sure what I am doing wrongly here.

The sketches does the nearly same job. Gets the array from a serial, uses the last element alone as the first element, increments the values of other elements and sends it back.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 15, 2015, 3:16pm UTC](https://forum.arduino.cc/t/leonardo-serial-commuincation-works-only-in-serial-monitor/346778/2 "2015-11-15T15:16:03Z")

</div>

```cpp
  Serial.begin(262144);

```

That's an odd baud rate. Why are you using that?

```cpp
    Serial.readBytes(sRx, len_sRx);

```

That function read some bytes, but did not NULL terminate the array. It told you how many bytes it read. You decided to just throw that value away. Why?

What is a "OTG USB"?

---

<div class="post-metadata">

**Author:** ![zgravity](https://avatars.discourse-cdn.com/v4/letter/z/d9b06d/32.png) [@zgravity](https://forum.arduino.cc/u/zgravity)\
**Post date:** [November 15, 2015, 3:33pm UTC](https://forum.arduino.cc/t/leonardo-serial-commuincation-works-only-in-serial-monitor/346778/3 "2015-11-15T15:33:23Z")

</div>

I have planned for a future application at the baud rate(250K). I also tried with the standard baud rate of 9600 but still no response.

Regarding the array, i thought it won't be the source of this problem. I also tried by sending a single byte.

Sorry about the OTG confusion. It is a micro-B.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 15, 2015, 3:50pm UTC](https://forum.arduino.cc/t/leonardo-serial-commuincation-works-only-in-serial-monitor/346778/4 "2015-11-15T15:50:38Z")

</div>

So, you have a USB to micro-B cable connected to the Leonardo, with the USB end free. You have a USB to B cable connected to the UNO, with the USB end free. And, you what? Tied them in a knot?

---

<div class="post-metadata">

**Author:** ![zgravity](https://avatars.discourse-cdn.com/v4/letter/z/d9b06d/32.png) [@zgravity](https://forum.arduino.cc/u/zgravity)\
**Post date:** [November 15, 2015, 4:49pm UTC](https://forum.arduino.cc/t/leonardo-serial-commuincation-works-only-in-serial-monitor/346778/5 "2015-11-15T16:49:43Z")

</div>

Like the attachment

 ![IMG_20151115_213215.jpg](https://europe1.discourse-cdn.com/arduino/original/4X/c/c/1/cc13bba85cd47e2d6d3264e5032f3b8e3cffb5b4.jpeg)

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 16, 2015, 12:28pm UTC](https://forum.arduino.cc/t/leonardo-serial-commuincation-works-only-in-serial-monitor/346778/6 "2015-11-16T12:28:35Z")

</div>

If you are having trouble sending data back and forth, you should be using MUCH simpler code, until you resolve that problem.

Both Arduinos are USB slaves. It may not be possible to get them to communicate via a USB cable. But, connecting TX of one to RX of the other, and connecting grounds should work.

---

<div class="post-metadata">

**Author:** ![liuzengqiang](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/liuzengqiang/32/213605_2.png) [@liuzengqiang](https://forum.arduino.cc/u/liuzengqiang)\
**Post date:** [November 16, 2015, 9:07pm UTC](https://forum.arduino.cc/t/leonardo-serial-commuincation-works-only-in-serial-monitor/346778/7 "2015-11-16T21:07:51Z")

</div>

That should not work at all. It should not work just because you connected the USB ports together. You can probably tie two USB flash drives with a USB A-A cable but that won't make them copy files into each other.

---

<div class="post-metadata">

**Author:** ![zgravity](https://avatars.discourse-cdn.com/v4/letter/z/d9b06d/32.png) [@zgravity](https://forum.arduino.cc/u/zgravity)\
**Post date:** [November 17, 2015, 12:23am UTC](https://forum.arduino.cc/t/leonardo-serial-commuincation-works-only-in-serial-monitor/346778/8 "2015-11-17T00:23:24Z")

</div>

I thought the Leonardo can work as a USB Host

My mistake

---

<div class="post-metadata">

**Author:** ![liuzengqiang](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/liuzengqiang/32/213605_2.png) [@liuzengqiang](https://forum.arduino.cc/u/liuzengqiang)\
**Post date:** [November 17, 2015, 2:12am UTC](https://forum.arduino.cc/t/leonardo-serial-commuincation-works-only-in-serial-monitor/346778/9 "2015-11-17T02:12:30Z")

</div>

> zgravity:  
> I thought the Leonardo can work as a USB Host
> 
> My mistake

Possibly not. The 32U4 is only able to function as a device (slave):  
Spec sheet says:  
USB 2.0 Full-speed/Low Speed Device Module

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 10:57am UTC](https://forum.arduino.cc/t/leonardo-serial-commuincation-works-only-in-serial-monitor/346778/10 "2021-05-06T10:57:24Z")

</div>

Closed on May 6, 2021, 10:57am UTC


