How to send data from Teensy 3.5 to Arduino Mega 2560

Status
Not open for further replies.

tutu

Member
Hi I'm trying to send just two bytes (a char and a integer) from a Teensy 3.5 to an Arduino Mega 2560. How can I do it? Do I need a library? Could you help me with the code (seem like a simple one)?
 
Assuming you are talking about sending across a wire and not wireless... Can you just connect serial ports between the MCU together and call a Serial.print() from the Teensy, then do a Serial.read() on the Arduino?
 
Yes, serial is by far the simplest way, especially if you only with to send in 1 direction and don't need to build in any sort of return confirmation or acknowledgement.

On the Teensy side, use Serial1.write(mybyte). On the Mega side, use Serial1.available() and Serial1.read() to receive it. For only single bytes, that's as easy as you can get.

For a multi-byte message, usually some sort of encoding is done, where a certain byte begins or ends the message, and the data is turned into a sequence of bytes which can't contain the special byte that marks where the messages begin & end. If you're new to all this, even if you know you need a multi-byte message, please do yourself a huge favor to get the simpler single byte way working first. Don't make the message format more complicated until you're certain the communication is working.

When connecting the wires, you only need to connect 2 lines. GND to GND, and TX1 to RX1. This may sound overly obvious, but don't make the common mistake of connecting same-name signals. It won't work if you connect TX to TX and RX to RX. Many novices have fallen into that trap, because with other things like I2C & SPI you do connect the names together. With Serial, you must connect a transmit pin from the sending side to the receive pin on the receiving side.

Both sides also need Serial1.begin() in their setup() functions, and it only works if both sides use the same baud rate.
 
It's not receiving/printing when I send a byte to print in the Mega's Serial Monitor.

MEGA:

byte b;


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


}


void loop() {


while(Serial1.available()){
b = Serial1.read();
Serial.println(b);
}


}

TEENSY:

void setup() {

Serial1.begin(9600);


}


void loop() {
byte b =1;
Serial1.write(b);


//delay(2000);
}
 
If that's all the code in your sketches, it will definitely not work, Arduino and Teensy initialize their serial ports differently. For Arduino, many tend to rely on SoftwareSerial library to indicate what pins are for the serial, but Teensy has a #define and deep in the library the pin definition is known.


In Arduino you will need something like

Code:
#include <SoftwareSerial.h> // spelling may be wrong

SoftwareSerial MySerial(RX_PIN, TX_PIN); (pins 0 and 1 for the UNO, cant' remember for the Mega)


// then your Setup and loop stuff follow



In Teensy you will need something like

Code:
#define MySerial Serial1 

// then your Setup and loop stuff


Hope this helps.
 
Status
Not open for further replies.
Back
Top