Software Serial on Teensy 4.0

Hi all,

Seem to be having issues utilizing software serial to write on pins 3 and 4 on a teensy 4.0. Checked connections (RX to TX and vice versa), shared grounds, etc. Here is the code. I know hardware serial is better but project will require usage of those for other things. Any help or insights are much appreciated.


Code:
#include <SoftwareSerial.h>

SoftwareSerial mySerial(4, 3); // RX is pin 4 (not used), TX is pin 3

void setup() {
  mySerial.begin(9600);
  Serial.begin(9600);
  while (!Serial);
  Serial.println("Starting communication on SoftwareSerial TX pin 3...");
}

void loop() {
  mySerial.println("Hello from Teensy");
  Serial.println("Debug: Hello from Teensy");
  delay(1000);
}
 
Your best option is FlexIO_t4. But by default it can't do slow baud rates, so you need to set it to use a slower clock before calling begin(9600).

Give this a try.

Code:
#include <FlexIO_t4.h>
#include <FlexSerial.h>

FlexSerial mySerial(-1, 3);

void setup() {
  mySerial.setClockUsingVideoPLL(921600);
  mySerial.begin(9600);
}

void loop() {
  mySerial.print("Test");
  delay(100);
}
 
I believe software serial on teensy 4.x boards, only works on hardware serial port pins. Pins 4 and 3 are not hardware serial pins. you can probably use flexio to create an emulated serial port. See the flexio_t4 library for details
 
I fixed the SoftwareSerial transmit code.


Looks like we tried to port the Teensy 3.x bitbang transmit years ago by changing the register pointer type. But it never worked because that code was assuming the register was a bitband address, which is a Cortex-M4 feature we sadly don't have in Cortex-M7. To work on Teensy 4.x we need to write the actual bitmask to the bitwise set & clear registers. This should make it "just work" (at least for transmitting) in the future.

You're still much better off to use FlexIO_t4, which is actual hardware serial rather than software emulated. Not all the pins can be controlled by FlexIO, but fortunately pin 3 and 4 can.
 
Back
Top