Serial connection and USB

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
K212
 
Posts: 11
Joined: Wed May 18, 2022 11:42 am

Serial connection and USB

Post by K212 »

I want to connect 2 Itsy Bitsy boards (ProductID: 3677) in serial, having one of them powered with the USB cable, and power the 2nd board through the 1st board.
Wired up both, and got the powering part working. I have an LCD connected to each boar, and that's working fine.
(Both boards are interconnected using USB C to USB C breakout boards.)

However, I can't seem to get them to talk to each other.

On the sender side, I trigger the following code when I press a button, and I can see that the LED is switched on.

Code: Select all

Serial.write(1);
digitalWrite(LED, HIGH);
On the receiving side, I have the following code.

Code: Select all

if (Serial.available() > 0)  {
    digitalWrite(LED, HIGH);
    char number = Serial.read();
  }
The inner part of the if statement is never executed, as the LED never turns on.
I have tested putting the LED logic outside the if statement to check that everything is wired correctly, and it is.

The boards have their 5V and GND pins connected to each other and the RX of one board to the TX of the other board and vice versa.

-------------------------------

I have the following hypothesis, based on things I read online, but unsure if it's correct;
RX/TX pins are even used for the USB serial connection between the PC and the board connected to it, and RX/TX can therefore not be used for what I'm trying to do?
Some boards seem to have additional serial ports on other pins, and in those cases you would use Serial1, Serial2, etc?

Is this correct? Is it impossible to use the RX/TX pins for other stuff if you're powering the board through USB?
If so, how do I get around the problem?
I don't need to be able to use the use the serial port monitor, I can debug using the LCDs I have connected to each board.
The boards will act as an HID (split) keyboard.

User avatar
adafruit_support_bill
 
Posts: 84988
Joined: Sat Feb 07, 2009 10:11 am

Re: Serial connection and USB

Post by adafruit_support_bill »

The 32U4 is a native USB device and the primary serial port "Serial" is the USB/Serial port. The Rx and Tx TTL Serial pins are accessed via "Serial1": https://learn.adafruit.com/introducting ... ns-2979410

User avatar
K212
 
Posts: 11
Joined: Wed May 18, 2022 11:42 am

Re: Serial connection and USB

Post by K212 »

If I use Serial1, it doesn't seem to work. If I use a single board and run the following code, it prints

Nr of bytes: 63
Value read: 0
Nr of bytes: 63
Value read: 0
Nr of bytes: 63
Value read: 0
Nr of bytes: 63
Value read: 0
....

Code: Select all

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);

   while (!Serial) ;

   delay(3000);
}

void loop() {
  readData();

  delay(1000);
}

void readData()
{
  int nrOfBytes = Serial1.available();
  if (nrOfBytes > 0)
  {
    Serial.print("Nr of bytes: ");
    Serial.println(nrOfBytes);
    int value = Serial1.read();
    Serial.print("Value read: ");
    Serial.println(value, DEC);
  }
}

User avatar
adafruit_support_bill
 
Posts: 84988
Joined: Sat Feb 07, 2009 10:11 am

Re: Serial connection and USB

Post by adafruit_support_bill »

Please post photos showing how you have everything connected. And post the code you are running on each board.

User avatar
K212
 
Posts: 11
Joined: Wed May 18, 2022 11:42 am

Re: Serial connection and USB

Post by K212 »

The full code is above, and I'm running that on a single board connected to my PC without any other connections

User avatar
K212
 
Posts: 11
Joined: Wed May 18, 2022 11:42 am

Re: Serial connection and USB

Post by K212 »

Also, I tested running the code on both boards, I get the exact same behaviour

User avatar
adafruit_support_bill
 
Posts: 84988
Joined: Sat Feb 07, 2009 10:11 am

Re: Serial connection and USB

Post by adafruit_support_bill »

The full code is above, and I'm running that on a single board connected to my PC without any other connections
You cannot expect meaningful serial data with floating inputs.

User avatar
K212
 
Posts: 11
Joined: Wed May 18, 2022 11:42 am

Re: Serial connection and USB

Post by K212 »

By floating inputs, do you mean the RX and TX pins aren't connected to anything?

User avatar
adafruit_support_bill
 
Posts: 84988
Joined: Sat Feb 07, 2009 10:11 am

Re: Serial connection and USB

Post by adafruit_support_bill »

Yes. The Rx pin will be in high-impedance mode and with no connection will be susceptible to EMI.

User avatar
K212
 
Posts: 11
Joined: Wed May 18, 2022 11:42 am

Re: Serial connection and USB

Post by K212 »

Alright, I will try again connecting it to another board. The reason I did it this way was that I was having similar issues and I wanted to try to reproduce it with minimal components and code, but I will use 2 boards again then

User avatar
K212
 
Posts: 11
Joined: Wed May 18, 2022 11:42 am

Re: Serial connection and USB

Post by K212 »

Now I read 0 bytes instead...

Code below (just switching the isSender flag to true/false depending on which board it is)

Code: Select all

bool isSender = false;
int nrOfTimesSent = 0;

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);

   while (!Serial) ;
   while (!Serial1) ;

   delay(3000);
   
   Serial.print("Start ");
   Serial.println(isSender, DEC);
}

void loop() {
  if (isSender)
  {
    sendData();
    delay(3000);
  }
  else
  {
    readData(); 
    delay(1000);
  }
}

void sendData()
{
  Serial1.write('1');
  nrOfTimesSent++;
  Serial.print("Sent ");
  Serial.println(nrOfTimesSent, DEC);
}

void readData()
{
  int nrOfBytes = Serial1.available();
  Serial.print("Nr of bytes: ");
  Serial.println(nrOfBytes);
  if (nrOfBytes > 0)
  {
    int value = Serial1.read();
    Serial.print("Value read: ");
    Serial.println(value, DEC);
  }
}
Result:

Start 0
Nr of bytes: 0
Nr of bytes: 0
Nr of bytes: 0
Nr of bytes: 0
Nr of bytes: 0
Nr of bytes: 0
Nr of bytes: 0
Nr of bytes: 0

I can't seem to attach an image, it just complains it's too large.
Either way, the connections are like this;

Board 1 Board 2
RX (0) TX (1)
TX (1) RX (0)
5V 5V
GND GND

Receiver board is connected through USB to my PC. Sender board is powered by receiver board.

User avatar
adafruit_support_bill
 
Posts: 84988
Joined: Sat Feb 07, 2009 10:11 am

Re: Serial connection and USB

Post by adafruit_support_bill »

Are both boards connected to USB? Your code as written would require both boards connected to USB.

User avatar
K212
 
Posts: 11
Joined: Wed May 18, 2022 11:42 am

Re: Serial connection and USB

Post by K212 »

No they are not, only the receiver. Why does the code require both of them to be connected to USB?

User avatar
K212
 
Posts: 11
Joined: Wed May 18, 2022 11:42 am

Re: Serial connection and USB

Post by K212 »

Nvm, I got it, due to the

Code: Select all

while (!Serial) ;

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”