Serial comunication between a teensy3.5 and esp32

MarbleMad

Member
Hi, I'm trying to simply connect an esp32 and a teensy3.5 over serial but can't for the life of me get it to work. I think the problem lies on the Teensy end as i have been able to connect the esp32 to other devices. Can anyone offer some guidance? Thanks.

Code:
/*
 * Teensy 3.5 Serial2
 * ON Board LED PIN 13
 * Serial  ESP32 > Teensy 3.5 Serial2
 * TXD > RX2 pin 9
 * RXD > TX2 pin 10
 */

int LED = 13;
int inByte =2 ;

void setup() {
  pinMode(LED, OUTPUT);
  Serial2.begin(9600);
    digitalWrite(LED, HIGH);   // turn the LED on (HIGH is the voltage level)
}

void loop() {
//testSend();
testRecieve();
}

void testSend(){  
  delay(500);
  Serial2.write(1); digitalWrite(LED,HIGH);
  delay(500);
  Serial2.write(0); digitalWrite(LED,LOW);
  }
  
void testRecieve(){
  if (Serial2.available() > 0) {
    inByte = Serial2.read();
if(inByte == 1){  digitalWrite(LED, HIGH); }
if(inByte == 0){  digitalWrite(LED, LOW);  }
  }}


Code:
/*
 * ESP32 Serial 
 * ON Board LED GPIO 2
 * Serial  ESP32 > Teensy 3.5
 * TXD > RX2 pin 9
 * RXD > TX2 pin 10
 */

#define LED 2
int inByte = 2 ;

void setup() {
  Serial.begin(9600);
  pinMode(LED,OUTPUT);
}

void loop() {
 testSend();
// testRecieve();
}

void testSend(){  
  delay(500);
  Serial.write(1); digitalWrite(LED,HIGH);
  delay(500);
  Serial.write(0); digitalWrite(LED,LOW);
  }
  
void testRecieve(){
    if (Serial.available() > 0) {
    inByte = Serial.read();
if(inByte == 1){  digitalWrite(LED, HIGH);}
if(inByte == 0){  digitalWrite(LED, LOW); }
    }  }
 
Last edited:
I think the problem may be that you are using print() to send from Teensy and read() to receive on ESP32. When you use Serial2.print(0) (or 1), what gets written to the UART is the ASCII representation of the number 0, which is the character '0', which has value 0x30 or 48. On the ESP32 side, you are using Serial.read(), which returns the byte value, which is 48. You then test for equality with 0 or 1, which is always false. Try replacing Serial2.print() with Serial2.write().
 
Oh, good catch although that's not the problem. Had code that responded if ANYTHING was received and no joy, also fixed that error and still nothing.

I also tried a loop back test, just connecting pins 9 and 10 to see if it'd talk to its self, nothing. :-(
im i getting something horribly wrong or am i looking at a dead teensy? serial over the usb appears to work.
 
I also tried a loop back test, just connecting pins 9 and 10 to see if it'd talk to its self, nothing. :-(
im i getting something horribly wrong or am i looking at a dead teensy? serial over the usb appears to work.

I took your code and made 3 quick changes

- add Serial.begin() to setup() so the program not only blinks the LED but also writes to SerMon
- uncomment testReceive(), so the program does both send and receive
- comment out the writes to the LED in the send function (only one can toggle the LED)
- add Serial.print() statements in the receive function

With those changes, and pins 9-10 connected, this program works for me. Are you sure you are looping back pins 9 and 10 per the Teensy pin numbering, which is not the standard pin numbering?

Code:
/*
 * Teensy 3.5 Serial2
 * ON Board LED PIN 13
 * Serial  ESP32 > Teensy 3.5 Serial2
 * TXD > RX2 pin 9
 * RXD > TX2 pin 10
 */

int LED = 13;
int inByte =2 ;

void setup() {
  Serial.begin( 9600 );
  while (!Serial && millis() < 3000) {}  
  pinMode(LED, OUTPUT);
  Serial2.begin(9600);
    digitalWrite(LED, HIGH);   // turn the LED on (HIGH is the voltage level)
}

void loop() {
  testSend();
  testRecieve();
}

void testSend(){  
  delay(500);
  Serial2.write(1); //digitalWrite(LED,HIGH);
  delay(500);
  Serial2.write(0); //digitalWrite(LED,LOW);
}
  
void testRecieve(){
  if (Serial2.available() > 0) {
    inByte = Serial2.read();
    if(inByte == 1){  digitalWrite(LED, HIGH); Serial.print(1); }
    if(inByte == 0){  digitalWrite(LED, LOW);  Serial.print(0); }
  }
}
 
"Are you sure you are looping back pins 9 and 10 per the Teensy pin numbering, which is not the standard pin numbering?"

AH, I'm an idiot, yes. the problem is I can't count to 10. I started counting at 0 but I see it goes GND, 0, 1, 2, etc.
THANKS!
All working now.
 
It's very easy to make this mistake. I frequently need to put Teensy under a bright light so my older eyes can read the pin numbers.
 
Back
Top