Esp32 & teensy 4.1 not communicating

Ram32

Member
Hello, I'm trying to connect Teensy with esp32. I'm using Esp 32 to communicate with an Iot app and the data from the app need to be transferred to teensy. I was using Arduino mega for the same application but decided to replace it with teensy. I'm connecting the tx from esp 32 to rx on teensy and rx from esp 32 to tx on teensy[Esp 32Tx to pin 7 & Esp32 Rx to pin 8 and GND to GND]. I'm unable to receive any communication btwn the esp32 and teensy. Clueless on how to get both of them working.

This the piece of the code I'm using for the teensy.
Code:
void setup() {

  Serial.begin(9600);

}

void loop() {

  Serial.println("Hello Boss");

  delay(1500);

}

This one is for ESP32

Code:
#define RXp2 16

#define TXp2 17

void setup() {

  // put your setup code here, to run once:

  Serial.begin(115200);

  Serial2.begin(9600, SERIAL_8N1, RXp2, TXp2);

}

void loop() {

    Serial.println("Message Received: ");

    Serial.println(Serial2.readString());

}


I'm following the steps from this tutorial


Any idea how get them both working together. Thanks
 
For Teensy, like ESP32, "Serial" is USB serial. If you are using pins 7 and 8 on Teensy 4.1, you should use Serial2..
I modified the code like this gave a try,

For teensy:

Code:
void setup() {

  Serial.begin(9600);
  // Serial2.begin(9600);

}

void loop() {

  Serial2.println("Hello Boss");
  Serial.println("Hello Boss22");

  //delay(150);
  }

For Esp 32

Code:
// #define RXp2 16

// #define TXp2 17

void setup() {

  // put your setup code here, to run once:

  Serial.begin(9600);

  Serial.begin(9600, SERIAL_8N1, RX, TX);

}

void loop() {

    Serial.println("Message Received: ");

    Serial.println(Serial2.readString());

}
the connections are same.

Teensy is printing few statements and then it's not printing anything after that. I'm seeing nothing at esp32 side. adding the snippets for output on both modules.
 

Attachments

  • esp1.jpg
    esp1.jpg
    141.9 KB · Views: 59
  • teen 1.jpg
    teen 1.jpg
    127 KB · Views: 60
If I can make a suggestion, start by using just ESP32 and connect your TX to RX (loopback). Convince yourself that you can send and read data that way, and then move on to connecting Teensy to ESP32. Your first post showed your Teensy code as using only "Serial". If you change that to "Serial2", the Teensy will send bytes on its UART pins 7 and 8.
 
For the ESP32, you will need something like this.

Code:
#define RX2 16  // Teensy 4.1 is connected to serial port #2
#define TX2 17

void setup()
{
  Serial.begin(115200);   // USB port
  Serial2.begin(9600, SERIAL_8N1, RX2, TX2);  //Port connected to Teensy 4.1
}
 
For the ESP32, you will need something like this.

Code:
#define RX2 16  // Teensy 4.1 is connected to serial port #2
#define TX2 17

void setup()
{
  Serial.begin(115200);   // USB port
  Serial2.begin(9600, SERIAL_8N1, RX2, TX2);  //Port connected to Teensy 4.1
}
I tried it but didn't get any results I think it might be my ESP-32 not working properly. I'll try with a different ESP32(same model unopened) and let you know
 
Teensy will not transmit on pin 8 with Serial2.begin() commented. You need this for anything to work on Serial2.

Looks like the ESP side might also be missing a begin()...
In order to transfer the tx and rx from esp32 to teensy I think need to use the serial2.begin(); right? With Serial2.begin I'll should expect the tx and rx should be happening from teensy pins from 7 & 8 right?!
 
That is correct. Serial2 should use pin 7 RX Pin 8 TX by default

Code:
void setup() {

  Serial.begin(9600);
  // Serial2.begin(9600);

}

void loop() {

  Serial2.println("Hello Boss");
  Serial.println("Hello Boss22");

  //delay(150);
  }

But the code above has: // Serial2.begin(9600);

Where the Serial2.begin is commented out...
 
Your programs have code to send data (text) out over the serial ports but nothing on either MCU to receive (read) the incoming serial data.

You could have code like this:
ESP32
Code:
#define ledPin 8
#define ledPinOn LOW

#define baudRate 500000

void setup() {
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, ledPinOn);
    delay(3000);
    digitalWrite(ledPin, !ledPinOn);
    Serial.begin(115200);
    Serial1.begin(baudRate, SERIAL_8N1, 20, 21);
    delay(2000);
    Serial.print("Now trying to communicate with Teensy");
    Serial1.println("Hello Teensy are you there?");
}
int i = 0;
void loop() {

    Serial1.print("Hello Teensy are you there?  ");
    Serial1.println(i);
    i++;
    delay(500);
    while (Serial.available()) {
        Serial.write(Serial.read());
    }
}

Teensy:
Code:
#define baudRate 500000

void setup() {
    Serial.begin(baudRate);
    Serial1.begin(baudRate);
}
int i = 0;
void loop(){
    while (Serial1.available()) {
        Serial.write(Serial1.read());
    }
    delay(100);
    Serial1.print("Hello ESP32");
    Serial1.println(i);
    delay(100);
}

If you use this code pair, everything that you type into the ESP Serial monitor should appear on Teensy.
 
Last edited:
. I
That is correct. Serial2 should use pin 7 RX Pin 8 TX by default

Code:
void setup() {

  Serial.begin(9600);
  // Serial2.begin(9600);

}

void loop() {

  Serial2.println("Hello Boss");
  Serial.println("Hello Boss22");

  //delay(150);
  }

But the code above has: // Serial2.begin(9600);

Where the Serial2.begin is commented out...
I enabled it and it was communicating. Thanks :)
 
Your programs have code to send data (text) out over the serial ports but nothing on either MCU to receive (read) the incoming serial data.

You could have code like this:
ESP32
Code:
#define ledPin 8
#define ledPinOn LOW

#define baudRate 500000

void setup() {
    pinMode(ledPin, OUTPUT);
    digitalWrite(ledPin, ledPinOn);
    delay(3000);
    digitalWrite(ledPin, !ledPinOn);
    Serial.begin(115200);
    Serial1.begin(baudRate, SERIAL_8N1, 20, 21);
    delay(2000);
    Serial.print("Now trying to communicate with Teensy");
    Serial1.println("Hello Teensy are you there?");
}
int i = 0;
void loop() {

    Serial1.print("Hello Teensy are you there?  ");
    Serial1.println(i);
    i++;
    delay(500);
    while (Serial.available()) {
        Serial.write(Serial.read());
    }
}

Teensy:
Code:
#define baudRate 500000

void setup() {
    Serial.begin(baudRate);
    Serial1.begin(baudRate);
}
int i = 0;
void loop(){
    while (Serial1.available()) {
        Serial.write(Serial1.read());
    }
    delay(100);
    Serial1.print("Hello ESP32");
    Serial1.println(i);
    delay(100);
}

If you use this code pair, everything that you type into the ESP Serial monitor should appear on Teensy.
I appreciate it. I got it communicating. Thanks a lot. :)
 
Back
Top