Troubleshooting Serial Communication between Two Teensy 4.1

Status
Not open for further replies.

Trensicourt

Active member
I'm trying to send "Hello" from the Master Teensy 4.1 to the Slave Teensy 4.1. However, I am not getting any information from the receiver serial monitor. I have connected TX1 to RX1 and RX1 to TX1 from Teensy to Teensy.

Here's the hardware picture:
Screenshot 2021-10-26 170634.jpg

The sender (COM5) is outputting "Hello" but the receiver isn't (COM6). I am using two serial ports, one per each Teensy, but with separate applications of the Arduino IDE open.
Screenshot 2021-10-26 170818.jpg

sender:
Code:
char Mymessage[6] = "Hello"; //String data
int ledPin = 34;


void setup() {
  pinMode(ledPin, OUTPUT);
  // Begin the Serial at 9600 Baud
  Serial.begin(9600);
}

void loop() {
  Serial.write(Mymessage,6); //Write the serial data
  digitalWrite(ledPin,HIGH); //set LED ON
  delay(1000);
  digitalWrite(ledPin,LOW); //set LED ON
  delay(1000);
}

receiver:
Code:
char Mymessage[12];
int ledPin = 34;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin,OUTPUT);
  //Begin the Serial at 9600 Baud
  Serial.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  Serial.readBytes(Mymessage,6);
  //Read the serial data and store in var
  digitalWrite(ledPin,HIGH);
  Serial.println(Mymessage);
  //Print data on Serial Monitor
  delay(1000);
}
 
Last edited:
Serial is USB serial only. You need Serial1 for pins 0 and 1 (which are clearly labelled RX1 and TX1 on the pin-out guide)
 
The code shows read from Serial1 ... then it prints that back out Serial1 ... that would then not appear in the shown Serial Monitor Window.

For USB print sue : Serial.print
 
- To write from either Teensy to the serial monitor, use Serial (USB).
- To write data from Teensy to Teensy, use Serial1 (UART).
- In setup(), you must call both Serial.begin(9600) and Serial1.begin(9600).
 
In your original code, both sketches were only writing to their respective Serial Monitor.
In your latest code, both sketches are writing only to the other Teensy. In the serial_slave, the last print should be Serial.println not Serial1.println

Pete
 
Guessing you come from the average Arduino. On the arduino the Serial pins are also the pins connected to the USB-to-Serial adapter.
So whatever you can send to/from the arduino also pops up on the terminal.

However on many of the more advanced controllers like the Teensy boards you have more than one Serial port. The regular serial only sending data via USB to terminal. While say Serial1 sends out via the serial pins. So in your original code the Sender and Receiver are communicating only with the PC, not with eachother.

Here is how your code ought to look to get them to speak:
Code:
//Sender
char Mymessage[6] = "Hello"; //String data
int ledPin = 34;


void setup() {
  pinMode(ledPin, OUTPUT);
  // Begin the Serial at 9600 Baud
  Serial.begin(9600);   //Open serial port to PC (USB)
  Serial1.begin(9600); //Open serial port to other teensy
}

void loop() {
  Serial.write(Mymessage,6); //Write the serial data to USB
  Serial1.write(Mymessage,6); //Write the serial data to the other teensy
  digitalWrite(ledPin,HIGH); //set LED ON
  delay(1000);
  digitalWrite(ledPin,LOW); //set LED OFF
  delay(1000);
}

Code:
//Receiver:
char Mymessage[12];
int ledPin = 34;

void setup() {
  // put your setup code here, to run once:
  pinMode(ledPin,OUTPUT);
  Serial.begin(9600);
  Serial1.begin(9600);
}

void loop() {
  Serial1.readBytes(Mymessage,6);  //Read the serial data received from the other Teensy and store in var
  digitalWrite(ledPin,HIGH);
  Serial.println(Mymessage);          //Print the serial data to the PC
  delay(1000);
}
 
Status
Not open for further replies.
Back
Top