Teensy 4 : Sketch does not run unless Serial Monitor is open

jimmie

Well-known member
I compiled a sketch and it is running fine.

I then close the Arduino IDE and disconnect power to the Teensy 4 (It is powered via Vin).

When I reconnect power to the board, nothing happens until I reopen the Arduino IDE AND start the Serial Monitor.

What may be causing this?

Thanks in advance for the community's help.
 
Posting the sketch would allow a good answer … a good guess is that there is something like :: while (!Serial); in setup that won't pass until the Serial Monitor connects.
 
Thank you @defragster. I appreciate your prompt help to this frustrating problem.

Here is my code. BTW, when USB cable is connected, the board does pull an IP Address.

Code:
void setup()
{
  //------------------------------------------------------------
  Serial.begin(115200);
  delay(2000);
  Serial.println("Starting ...");
  //------------------------------------------------------------
  //https://www.pjrc.com/store/wiz820_sd_adaptor.html#init
  //https://github.com/PaulStoffregen/Ethernet

  pinMode(9, OUTPUT);
  digitalWrite(9, LOW);    // begin reset the WIZ820io
  pinMode(10, OUTPUT);
  digitalWrite(10, HIGH);  // de-select WIZ820io
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);   // de-select the SD Card
  digitalWrite(9, HIGH);   // end reset pulse
  //------------------------------------------------------------
  Serial1.addStorageForRead(buf1, sizeof(buf1));
  Serial2.addStorageForRead(buf2, sizeof(buf2));
  Serial3.addStorageForRead(buf3, sizeof(buf3));
  //------------------------------------------------------------

  for (int i = 0; i < 2; i++)      // for (int i = 0; i < 3; i++)
  {
    leddar[i].init();
    sumTime[i] = 0;
  }

  //------------------------------------------------------------
  Serial.println("Ethernet WebServer Example");
  Ethernet.begin(mac, ip);
  if (Ethernet.hardwareStatus() == EthernetNoHardware) {
    Serial.println("Ethernet shield was not found.  Sorry, can't run without hardware. :(");
    while (true) {
      delay(1); // do nothing, no point running without Ethernet hardware
    }
  }

  if (Ethernet.linkStatus() == LinkOFF) {
    //Serial.println("Ethernet cable is not connected.");   //not working?
  }
  server.begin();
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());

  //------------------------------------------------------------
  Serial.print("Initializing SD card...");
  if (!SD.begin(chipSelect)) {
    Serial.println("initialization failed!");
    //return;
  }
  Serial.println("initialization done.");
  write2SD();
  readSD();

  //------------------------------------------------------------
  u8g2.begin();
  u8g2_prepare();
  String myVal = "jimmie";
  showU8G2(11, 12, myVal);
  delay(2000);
  //------------------------------------------------------------

  Serial.println("------------->>>>  Starting");
}
 
Last edited:
That is just code for setup() , not a complete sketch - the typical problem is the noted while(!Serial) which doesn't show there.

It may be that one of the .init()/.begin() functions stops to wait for !Serial to exist?

Without the Serial Monitor printing it will be hard to say where it stops without adding blink output to LED_BUILTIN (#13) or to a UART or something for an indication of where the code stops to figure out why.
 
Thank you @defragster. Your input helped me debug further.

You were right about the culprit.

I found out that the library I am using has the Leonardo while (!Serial)

Thanks again.
 
Last edited:
Your setup code includes: "Serial.println("Starting ...");" which uses the Teensy USB-serial device. Currently on T4 I believe if you send any characters to the USB-Serial device (using Serial.X instead of Serial1.X etc for a real UART) then the serial interrupt system is disabled until the USB endpoint is accessed and the data is read out by the USB host PC. I'm not sure it is supposed to work that way, I don't think other Teensy models do that; but apparently for now, it does work that way. This was reported here: https://forum.pjrc.com/threads/5732...s-incrementing?p=214149&viewfull=1#post214149
 
Thank you JBeale.

The culprit was the
Code:
while(!Serial)
statement in the library I was using.

The only problem remaining now is the SD behavior on the T4 .... :).
 
Your setup code includes: "Serial.println("Starting ...");" which uses the Teensy USB-serial device. Currently on T4 I believe if you send any characters to the USB-Serial device (using Serial.X instead of Serial1.X etc for a real UART) then the serial interrupt system is disabled until the USB endpoint is accessed and the data is read out by the USB host PC. I'm not sure it is supposed to work that way, I don't think other Teensy models do that; but apparently for now, it does work that way. This was reported here: https://forum.pjrc.com/threads/5732...s-incrementing?p=214149&viewfull=1#post214149

This isn't a case of Serial.available not incrementing. I ran a sample and that problem is real - but that is reading input - which works - just not having .available() be reliable yet.

Teensy 4 is perfectly good starting up and printing when Serial is not online so as found the problem was the embedded " while (!Serial) " This have been working well through T4 Beta period.
 
Thanks for that correction defragster, I did misinterpret the problem. I would correct or delete my earlier post but the forum no longer seems to allow that.
 
Thank you JBeale.

The culprit was the
Code:
while(!Serial)
statement in the library I was using.

The only problem remaining now is the SD behavior on the T4 .... :).

Hi @jimmie I’m facing the same error as you were facing earlier can you please tell me that how you fixed the issue, which library did you use to solve the issue currently I’m using Ethernet.h and SPI.h.
 
We don't know which libraries you use and can't help you.
You can open libraries (.h and .c or .cpp files) in an editor of your choice.
 
I’m using Ethernet.h and SPI.h.
I’ll try your solution by finding and ending the .h file using vscode.
Thank You soo much for the help.
 
Without seeing code we can not help you.. so it would be best to post your program.

Both libraries do not have this line.
Or, perhaps you're using an old version?
 
I have included the code below, please let me know if there’s any in my code.

=======================================
Code:
#include<SPI.h>
#include<Ethernet.h>
 
int led = 8;
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xEF, 0xED};

EthernetServer server(80);
String readString;
 
void setup() {
  Serial.begin(9600);
  pinMode(led, OUTPUT);
  Ethernet.begin(mac);
  server.begin();
  Serial.print("Server is at ");
  Serial.println(Ethernet.localIP());
}
 
void loop() {
  EthernetClient client = server.available();
 
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        Serial.print(c);
 
        if (readString.length() < 100) {
          readString += c;
        }
 
        if (c == '\n') {
          Serial.print(readString);
          client.println("<HTTP/1.1 200 0K>");
          client.print("<Connection-Type: text/html>");
          client.println("<Connection: close>");
          client.println();
 
          client.println("<!DOCTYPE html>");
          client.println("<html>");
          client.println("<head>");
          client.println("<title>AUTOMATIC LOCK</title>");
          client.println("</head>");
          client.println("<body>");
          client.println("<a href=\"/?button1on\"\"><button>LOCK ON</button></a>");
          client.println("<a href=\"/?button2off\"\"><button>LOCK OFF</button></a>");
          client.println("<body style=background-color:black>");
 
          delay(1);
          client.stop();
 
          if (readString.indexOf("?button1on")>0) {
            digitalWrite(led, HIGH);
          }
 
          if (readString.indexOf("?button2off")>0) {
            digitalWrite(led, LOW);
          }
          readString = "";
        }
 
      }
 
    }
 
 
  }
}
 
Last edited by a moderator:
Hm the code looks ok, and it it should start, at least.
Maybe try to add a line

delay(2000); Serial.print("Program started");

as first line in setup. But I don't think it will work much, as you wrote it works if you open the serial monitor (quote: "same issue").

Maybe the other user here have an idea what's wrong here?
 
You might as well get used to the teensy way of debugging. Lots of print statements, sometimes with a Serial.flush() followed by a delay.
 
For the sake of testing, please program this onto your Teensy. After you get the printing problem fully resolved, then go back to the more complicated code.

Code:
unsigned int count = 0;

void setup() {
  pinMode(13, OUTPUT);  // LED on solid before serial monitor open
  digitalWrite(13, HIGH);
  while (!Serial) ; //wait for Arduino Serial Monitor
}

void loop() {
  Serial.print("count = ");
  Serial.println(count++);
  digitalToggle(13);  // LED blinks after serial monitor open
  delay(1000);
}

As you can see in the comments, this turns the LED on solid before you have opened the serial monitor, and then it should blink. This can help you get some info from the Teensy about whether the serial monitor has actually been opened on your PC. If the wrong port is selected in Tools > Ports, for example, you might be seeing nothing because you're connecting to some other port which isn't Teensy at all.

But that's just a blind guess, because I can't see your screen and all the info you're given us says virtually nothing about what you're actually experiencing, other than nothing prints. Please, if this doesn't solve the problem, take some screenshots and post the images in your messages, so we can actually see what's really on your screen. And be specific, to tell us what you are actually clicking. When all we know is "it doesn't work" and we can't see and can't know what you did, helping you is very difficult. Please, I want to help. So does everyone here. Let us help you by giving us screenshots and details, so we're not just blind guessing.
 
Hi!
...
As far as I can tell, none of the libraries we use have a "while (!Serial)", and I also don't believe that is the issue because the code does run even after the Monitor is no longer connected.

Any ideas on how to resolve this?

Perhaps build with a USB type without Serial support? Seems there was a NONE option not showing now ... maybe Keyboard or some other?

No idea what if(Serial) does if there is no USB:Serial ...
 
@Edward1 - this post was deleted as resolved - leaving a note as to the resolution might help others ...

Hi!
...
As far as I can tell, none of the libraries we use have a "while (!Serial)", and I also don't believe that is the issue because the code does run even after the Monitor is no longer connected.

Any ideas on how to resolve this?
 
Back
Top