Go Down

Topic: Serial communication between two Nano over USB (Read 792 times) previous topic - next topic

unstopp

Jul 22, 2018, 10:38 pm Last Edit: Jul 22, 2018, 10:47 pm by unstopp
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  :'(


Receiver Arduino code:
Quote
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:
Quote
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

ieee488

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?


.

unstopp

Why doesn't it make sense?


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

ieee488

#3
Jul 23, 2018, 01:34 am Last Edit: Jul 23, 2018, 01:34 am by ieee488
I find it strange not to be able to communicate via USB between 2 Arduinos. Have you succeded ?
No.  I have not tried.


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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

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.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

unstopp

#6
Jul 23, 2018, 11:22 am Last Edit: Jul 23, 2018, 11:23 am by unstopp
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!

unstopp

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!

Robin2

#8
Jul 23, 2018, 12:20 pm Last Edit: Jul 23, 2018, 12:21 pm by Robin2
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.
Two or three hours spent thinking and reading documentation solves most programming problems.

unstopp

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.

sterretje

You need to connect the TX of the one Nano to the RX of the other Nano and vice versa.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

unstopp

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.

sterretje

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.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Robin2

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
Two or three hours spent thinking and reading documentation solves most programming problems.

unstopp

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!

Go Up