Bidirectional Communication

I've got a board which requires communication between a Teensy 4.1 and a Teensy 4.0. I decided to use Serial to achieve this, but am having trouble with them communicating. Each board has to be able to communicate with the other at the press of a button. The 4.1 should be able to send bytes (or strings) of data to the 4.0, and the 4.0 needs to send data to the 4.1 (strings), each when a button is pressed. I've attached a simplified code for each board. Is there a certain library I have to use to accomplish this, or is communication only 1 way (master/slave only)?

I know I'm stating Teensy's here, but I'm hoping this could work for Arduino boards too.

//Teensy 4.0 Code

#include <Bounce.h>

const int HomeEntpin = 25;
String sendchoice = "Teensy 4.0 command received";
int recchoice = 0;
Bounce H = Bounce(HomeEntpin, 10);

#include <Bounce.h>

void setup() {
  Serial.begin(9600);
  Serial1.begin(9600);
  pinMode(HomeEntpin, INPUT);
}

void loop() {
  if (H.update()) {
    if (H.fallingEdge()) {
      Serial.println("A pressed");
      Serial1.println(sendchoice);
      Serial.println("String sent");
    }
  }
  if (Serial2.available() > 0) {
    Serial.println("Serial 1 Read");
    recchoice = Serial1.read();
    Serial.println(recchoice);
    delay(100);
  }
}
//Teensy 4.1 code

#include <Bounce.h>

const int Apin = 20;
Bounce A = Bounce(Apin,10);
String recchoice = "";
int sendchoice = 1;

void setup() {
  Serial.begin(9600);
  Serial2.begin(9600);
  pinMode(Apin, INPUT);

}

void loop() {
  if (A.update()) {
    if (A.fallingEdge()) {
      Serial.println("A pressed");
      Serial2.write(sendchoice);
      Serial.println("Int 1 sent");
    }
  }
  if (Serial2.available() > 0) {
    Serial.println("Serial 2 Read");
    recchoice = Serial2.read();
    Serial.println(recchoice);
    delay(100);
  }
}

Try a Google search for "serial communication between two arduino".

And/or read this tutorial: Serial Input Basics - updated

1 Like

+1 for Robin2's serial input basics tutorial.

I have google searched that exact term and similar, but with no luck. The results seem to be about single direction communication (master/slave), of which I cannot use. I need both to communicate on their own to each other.

I forgot add that the 4.0 doesnt even register serial data when the 4.1 sends it (serial1.available remains 0 regardless). I attached a logic analyzer to the TX and RX pins on the 4.1, but only the RX is showing data transfer, not the TX. Its as if the Teensy doesnt even attempt to send data. In case the TX was damaged I created a simple program to only transmit and it worked, so it seems this issue only arises when the Teensy is set to receive and transmit data to another board.

blh64, thanks for the link. Theres a lot to go over, but it seems promising. Using '>' characters is something I havent tried before, so I'll try those when I get a chance.

In the first sketch, Serial, Serial1, and Serial2 are referenced but there is no Serial2.begin. Make sure the connections are Tx to Rx and Rx to Tx.

I forgot to change Serial2 to Serial1 in the first sketch. It worked after I changed it.

I just found out why it didnt work for my main code: I accidentally allocated pin 0 (RX) as an regular digital input on my 4.0 board. Its been fixed and now properly works. Sorry for wasting everyones time lol.

I'm surprised the IDE didnt notice, but I guess its lesson learned in case anyone has this same issue.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.