Serial communication between two Nano over USB

Hi guys,

I'm trying to achieve a serial communication between 2 Arduino Nano (v3 - with micro-usb), over the USB connectors (micro-usb to micro-usb).

I've tested the code of both Arduinos, individually, with the Serial Monitor on the OS X. All good.
It seems Arduino - OS X via micro-usb works, but not the Arduino - Arduino.

I'm basically trying to send a char from one arduino and receive it on the other, using a micro-usb cable.
Found nothing online, I'm starting to think this might not be possible, which doesn't quite make sense.

Please let me know your thoughts :cry:

Receiver Arduino code:

int ledPin = LED_BUILTIN;

void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);

Serial.begin(115200);
}

void loop() {
if (Serial.available()) {
char received = (char)Serial.read();
Serial.print(received);

if (received == 'a') {
digitalWrite(ledPin, HIGH);
} else if (received == 'x') {
digitalWrite(ledPin, LOW);
}

delay(1000);
}
}

Transmitter Arduino code:

int ledPin = LED_BUILTIN;
int buttonPin = 4;
char commandSent = '-';

void setup() {
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);

pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, LOW);

Serial.begin(115200);
}

void loop() {
if (digitalRead(buttonPin) == HIGH && commandSent != 'a') {
digitalWrite(ledPin, HIGH);
Serial.print('a');
Serial.write('a');
commandSent = 'a';
}
if (digitalRead(buttonPin) == LOW && commandSent != 'x') {
digitalWrite(ledPin, LOW);
Serial.print('x');
Serial.write('x');
commandSent = 'x';
}

delay(10);
}

Thank you very much,
Marius

unstopp:
Found nothing online, I'm starting to think this might not be possible, which doesn't quite make sense.

Why doesn't it make sense?

.

ieee488:
Why doesn't it make sense?

.

I find it strange not to be able to communicate via USB between 2 Arduinos. Have you succeded ?

unstopp:
I find it strange not to be able to communicate via USB between 2 Arduinos. Have you succeded ?

No. I have not tried.

Both Nanos are USB Slaves. If one was a Host, like a PC is, then they could talk.

You can have them talk by connecting Rx to Tx, Tx to Rx, and Gnd to Gnd, then use Serial on each to send data back & forth.

USB needs a host; you can recognise hosts by the A-type connector (like on a PC). You can connect multiple 'slaves' (mouse, keyboard, Arduino etc) to a host. You can not connect slaves to slaves and you can not connect hosts to hosts and get a working setup.

CrossRoads:
Both Nanos are USB Slaves. If one was a Host, like a PC is, then they could talk.

You can have them talk by connecting Rx to Tx, Tx to Rx, and Gnd to Gnd, then use Serial on each to send data back & forth.

Ah I see. The desired outcome was to use the micro-usb connectors and not the pins (RX/TX or 10/11, etc.). Thank you for the info!

sterretje:
USB needs a host; you can recognise hosts by the A-type connector (like on a PC). You can connect multiple 'slaves' (mouse, keyboard, Arduino etc) to a host. You can not connect slaves to slaves and you can not connect hosts to hosts and get a working setup.

Right. Makes sense now. Is it possible to set one Nano as master and leave the other one as it is (slave) and use the micro-usb ? (no i2c)
I've just started researching this. Thank you!

unstopp:
Is it possible to set one Nano as master and leave the other one as it is (slave) and use the micro-usb ?

No.

...R
Serial Input Basics - simple reliable ways to receive data.

Robin2:
No.

...R
Serial Input Basics - simple reliable ways to receive data.

Right.
I will try one more thing. Add an USB TTL to one of the Nano, hopefully this way I can invert the RX/TX. I think that's the issue - both Nanos are wired RX-RX, TX-TX when simply bridging them with a cable via their own micro-usb connector.

Something like this:

If this fails, I guess I'll go for a solution involving BLE. Problem is there is also a Phone involved, so in the end there will be 3 BLEs in the system - not sure how I'll handle that atm.

You need to connect the TX of the one Nano to the RX of the other Nano and vice versa.

sterretje:
You need to connect the TX of the one Nano to the RX of the other Nano and vice versa.

Yes, exactly. That’s what I’m going to try to achieve with that USB TTL piece. Will let you guys know, I’ll try later today.

unstopp:
Yes, exactly. That's what I'm going to try to achieve with that USB TTL piece. Will let you guys know, I'll try later today.

It's just wiring; you don't need additional stuff for that.

unstopp:
I will try one more thing. Add an USB TTL to one of the Nano, hopefully this way I can invert the RX/TX

Sorry, but this is just nonsense.

All you need are 3 pieces of wire to connect the Rx pin of one Nano to the Tx pin of the other one, Tx to Rx and GND to GND.

...R

sterretje, Robin2,

I know guys, don't get me wrong.

But this is not a good solution for the end product. It has to be easy for the end user to connect.
I don't want to add additional connectors or custom-made wire cord, that's why I keep trying to use the "default" connectors (micro-usb with standard micro-usb cable).

In the end there will be 2 devices (a controller and an ECU) that will connect via either micro-usb cable or BLE (I'm researching both solutions). Hope this shed some light on what I'm trying to achieve.

Appreciate your thoughts, thank you!

unstopp:
I don't want to add additional connectors or custom-made wire cord,

I don't see that you have any choice.

...R

unstopp:
I know guys, don't get me wrong.

But this is not a good solution for the end product. It has to be easy for the end user to connect.
I don't want to add additional connectors or custom-made wire cord, that's why I keep trying to use the "default" connectors (micro-usb with standard micro-usb cable).

Have you found an "of-the-shelf" micro-usb-to-micro usb cable?

As Robin said, you don't have much choice. If you insists on using USB, you will need an USB host; there are shields (like the one that you showed) that have the A-type connector instead of the micro B-type; you will need additional libraries and rewrite your code to make use of them.

First two hits in google

https://www.elecrow.com/wiki/index.php?title=USB_Host_Shield_for_Arduino

Note:
I have no experience with them so I have no idea how to program them as a virtual serial port.

Just a wild thought ...

Maybe you could use a RaspberryPi as a USB Host between the two Nanos?

However I think 3 pieces of wire would make more sense :slight_smile:

...R

sterretje:
Have you found an "of-the-shelf" micro-usb-to-micro usb cable?

As Robin said, you don't have much choice. If you insists on using USB, you will need an USB host; there are shields (like the one that you showed) that have the A-type connector instead of the micro B-type; you will need additional libraries and rewrite your code to make use of them.

First two hits in google
About Us
USB Host Shield for Arduino - Elecrow

Note:
I have no experience with them so I have no idea how to program them as a virtual serial port.

I plan on fabricating the micro-usb to micro-usb cable in China. I only found max. 2-3 meters online and indeed they are pretty rare. I might need like 5m, just to be sure.

Regarding what you said with the shield, I am waiting for a usb female adaptor so I can try out the circuit I mentioned before (with the usb-ttl thing). Here is a quick sketch:

Robin2:
Just a wild thought ...

Maybe you could use a RaspberryPi as a USB Host between the two Nanos?

However I think 3 pieces of wire would make more sense :slight_smile:

...R

I'm afraid not. It's an end product that has to be manufactured. Adding a Rasp will increase like.. everything (costs, time, size, etc.).
That's why I'm reluctant to "3 wires", because this means I will have to add a 2x additional micro-usb myself and wire them.

I'll first try with only 1 usb-ttl (like I showed you in a previous post), if it doesn't work, I'll try with 2. Adding one for each Nano.Like this: