Serial.begin() not needed???

ninja2

Well-known member
This little sketch works normally even though I've commented the first two lines in setup.
In other words Serial.begin() seems pointless ?
Code:
#include "Streaming.h"

#include "TeensyTimerTool.h"
using namespace TeensyTimerTool;

PeriodicTimer t1;

void setup(){
  //Serial.begin(115200);                                // apparently not required ?!
  // while (!Serial && millis() < 1000)             // apparently not required ?!
  Serial << "\n\n----- t4_PeriodicTimer.c -----\n";
  Serial << "Flashing LED at 2Hz ... forever :-)\n";
  pinMode(LED_BUILTIN,OUTPUT);
  t1.begin(callback, 250'000);                      // triggers callback() every 250ms
  Serial <<     "---------- setup done --------\n";
}

void loop(){
}

void callback(){
  digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
}

and:
How does it know to use 115200 ?
Why don't I see the baud rate selection in the TD window these days?

Seems something big has changed in TD or the IDE that I missed ...
 
For most Arduino-style boards, "Serial" is a UART connected to a USB conversion chip. With the Teensy there's no UART and the USB controller is built into the microprocessor - there's no variable baudrate because USB runs at a fixed speed and the initialization is done automatically as part of the regular boot sequence.
 
OK so Serial.begin () is unnecessary for teensy, thanks.
Curious: what is that "fixed speed" ?
From experience the While (!Serial) command is still needed in some cases, including teensy
 
Possibly a related issue:
If I have a T4.1 connected by USB and sending occasional message on Serial, then upload a revised sketch, the Serial line almost always goes silent (but not always) and I have to hit the on/off for > 4 secs to force a reset, then the Serial works.
I think this only happens on T4.1
Is there a way to avoid the reset?
 
Native USB serial doesn't use the baud rate. Documentation here:


It's always been this way (for Teensy and Arduino's native USB boards like Leonardo and MKR1000).

Why don't I see the baud rate selection in the TD window these days?

In the new IDE, at some point Arduino developers made the baud rate drop-down list disappear when it's not needed.

In the old IDE, you get 2 different serial monitor windows depending on whether the Tools > Port menu selects a Teensy Port or Serial Port. This is done by patches the installer adds to the IDE's Java code. Those patches also try to make the serial monitor (Teensy Port version) run faster to it can keep up with Teensy 4.0 sending sustained max speed. Or at least keep the IDE from crashing. Java GUI stuff is incredibly inefficient!


From experience the While (!Serial) command is still needed in some cases, including teensy

If your program runs Serial.print() before your PC is finished detect the USB device or before the Serial Monitor window is open, the incoming text can be lost because Teensy booted up too quick and "printed" before your PC was listening.

The purpose of while (!Serail) is to wait for the serial monitor to be open and ready to receive, so you are certain to receive everything. Most examples with this have a comment explaining the wait.
 
Curious: what is that "fixed speed" ?

The speed is either 12 or 480 Mbit/sec, minus USB protocol overhead and USB bandwidth used by other USB devices.

Flow control also really matters. Conceptually it is similar to traditional serial RTS/CTS flow control but built into the USB protocol rather than an optional add-on. So the effective speed also can be limited by the receiver not being able to process rapidly enough.
 
I can't answer your question because I do not have a clear picture of "On/Off reset to kick Serial off again". I can't see your computer screen or what action you are actually performing. I know know are those words which don't paint a clear picture. I don't even know if you're pressing the Program pushbutton on Teensy 4.1 or if you've connected hardware to the On/Off pin and you're using that, or whether the word "reset" means something else (the reset signal isn't normally accessible). I just can't understand your words well enough to see what you're actually doing and what's really happening, so an answer is impossible (at least from me... maybe someone else could understand?)

However, I can run a quick test here. I converted your program to Arduino syntax, not depending on any extra libraries. I was able to successfully load and run it on Teensy 4.1 over and over, by only clicking the Upload button in Arduino IDE. Each time the serial monitor disconnects and Teensy Loader uploads the program. Then serial monitor automatically reconnects and shows this:

Code:
----- t4_PeriodicTimer.c -----
Flashing LED at 2Hz ... forever :-)
---------- setup done --------

Running on Linux (Ubuntu 24.04) on a fast desktop machine, the entire process from clicking the IDE's Upload button to seeing the message appear in the serial monitor takes about 2 seconds.

Here is the converted code I tested.

Code:
IntervalTimer t1;

void setup() {
  Serial.begin(115200);
  while (!Serial && millis() < 1000) ;
  Serial.print("\n\n----- t4_PeriodicTimer.c -----\n");
  Serial.print("Flashing LED at 2Hz ... forever :-)\n");
  pinMode(LED_BUILTIN, OUTPUT);
  t1.begin(callback, 250000);     // triggers callback() every 250ms
  Serial.print("---------- setup done --------\n");
}

void loop() {
}

void callback() {
  digitalToggle(LED_BUILTIN);
}

If you're having trouble, I would recommend using Arduino IDE with fresh install of Teensyduino 1.59 and this code. If your Teensy has previously loaded code that doesn't listen for USB properly, you may need to press the Program button on Teensy to cause the first upload. Thereafter, uploads should work by only clicking Upload in Arduino IDE.
 
This is not a show-stopper, I'm only trying to work out if I'm using Serial and the buttons (Program/ON-OFF) correctly, and ideally not have to push the On/OFF button and wait 4 - 6 seconds every time I upload.

The sequences below might clarify my issue, for my T3.6, T4.1 and Teensy Micromod (verified today) using IDE 2.3.2 on WIN10
I should emphasis the sequences are not rock solid, e.g. occasionally Serial runs immediately after upload when I don't expect it, other times Serial won't respond to any action and I have to pull the serial cable out/in to reboot.

The Micromod Teensy is mounted in a Micromod main double board which has just two pushbottons labelled RESET and BOOT, The former is connected to the Teensy On/OFF pin and the latter to the PROGRAM pin. This is where my "On/Off reset to kick Serial off again" came from, but I should have left "reset" out. Anyway hopefully it's clear from the sequences below I do understand how the PROGRAM and ON/OFF inputs work.

T3.6 sequence

Open Sketch (any sketch) with the code already running on a T3.6
When running normally the sketch transmits text to Serial Monitor approx 1/sec

Do an innocuous/minor edit to the sketch, and upload:
Serial stream stops once upload starts.
Upload completes, no output on Serial.
Push the Program button on T3.6: upload completes and sketch runs normally.

Choose another sketch, and upload:
Serial stream stops once upload starts.
Upload completes, no output on Serial, although sketch is running (LED flashing etc).
Push the Reset button for 4 secs (ON/OFF) on T3.6 then once more to reboot: sketch runs normally.

Revert to original sketch, and upload:
Serial stream stops once upload starts.
Upload completes, but no output on Serial although Sketch is running (LED flashing etc).
Push the Reset button for 4 secs (ON/OFF) on T3.6 then once more to reboot: sketch runs normally.

T4.1

Open Sketch (any sketch) with the code already running on a T4.1
When running normally the sketch transmits text to Serial Monitor approx 1/sec

Do an innocuous/minor edit to the sketch, and upload:
Serial stream stops once upload starts.
Upload completes, Sketch runs normally and Serial output commences automatically

Choose another sketch, and upload:
Serial stream DOES NOT STOP when the upload starts.
Upload completes, but Serial output is still from previous sketch, that is still running
Push the Program button on T4.1 sketch loads and runs normally.

Teensy Micromod

Open Sketch (any sketch) with the code already running on a Teensy MicroMod
When running normally the sketch transmits text to Serial Monitor approx 1/sec

Choose another sketch, and upload:
Serial stream DOES NOT STOP when the upload starts.
Upload completes. the new sketch has loaded and is running, but Serial output is silent.
Push the RESET button on Micromod Main Board for 4 secs until OFF, then push once more to reboot
(on MicroMod the RESET button is wired to the Teensy ON/OFF pin)
Teensy reboots, the sketch runs with normal Serial output.

Choose another sketch, and upload:
Upload completes. the new sketch has loaded and is running, but Serial output is silent.
Push the RESET button on Micromod Main Board for 4 secs until OFF, then push once more to reboot
(on MicroMod the RESET button is wired to the Teensy ON/OFF pin)
Teensy reboots, the sketch runs with normal Serial output.

repeat ad-infinitum
 
Last edited:
Loss of Serial not likely the fault of the Teensy, but rather the host computer failing to detect and properly respond to the device going offline during reset.
This is an interesting possibility, although I'm using the standard IDE 2.3.2 + WIN10 .... wouldn't others see same issues?
If it is, how to test/fix?
Some of my USB cables are 2m long, but I doubt that has any effect.

I'm still holding fire on using TyCommander, but it's definitely a fallback option if the issues persist
 
This is an interesting possibility, although I'm using the standard IDE 2.3.2 + WIN10 .... wouldn't others see same issues?
If it is, how to test/fix?
Some of my USB cables are 2m long, but I doubt that has any effect.

I'm still holding fire on using TyCommander, but it's definitely a fallback option if the issues persist
Have had reconnect issues - close&open IDE SerMon were a workaround. This was noted - was it tried in any fashion? Seems it was more IDE 2 than 1?

Can't be sure because TyCommander just makes everything better and easier above and beyond issues like this with IDE usage. Multiple Teensy's, other Serial devices are ID'd helpfully, and ESP32's in use all at the same time, And Teensy with Dual or Triple Serial all show up for easy use - and all in one or more separate App windows - not stuffed into edge of the IDE 2.
 
I've often resorted to open/close Serial monitor when things go awry. In an ideal world we shouldn't have too ....
.... TyCommander just makes everything better and easier above and beyond issues like this with IDE usage. Multiple Teensy's, other Serial devices are ID'd helpfully, and ESP32's in use all at the same time, And Teensy with Dual or Triple Serial all show up for easy use - and all in one or more separate App windows - not stuffed into edge of the IDE 2.
:) ... you're making it harder to ignore.

PS: are PlatformIO and TyCommander similar?
 
Last edited:
Indeed, awkward to close&open - but noting that behavior could lead to some understanding for a resolution.

TyComm has many useful upsides and makes the environment much better. Seems a proper ninja wouldn't be without it.

No, PIO is an alternate IDE and TyComm is 'just' a SerMon replacement that can upload - except here where most of the recent T_4.x's on hand are LOCKED from those tests and it can't upload those.
 
For what it's worth, I use PlatformIO with a Teensy Micromod based project. I use Visual Studio Code. The serial monitor in this setup seems to do a pretty good job of automatically closing when the TeensyMM gets flashed then comes back automatically and reasonably quickly - something like 1-2 seconds. So, that can be a viable method.
 
T4.1

Open Sketch (any sketch) with the code already running on a T4.1
When running normally the sketch transmits text to Serial Monitor approx 1/sec

Do an innocuous/minor edit to the sketch, and upload:
Serial stream stops once upload starts.
Upload completes, Sketch runs normally and Serial output commences automatically

Choose another sketch, and upload:
Serial stream DOES NOT STOP when the upload starts.
Upload completes, but Serial output is still from previous sketch, that is still running
Push the Program button on T4.1 sketch loads and runs normally.
First of all you don't say whether you are using PlatformIO or either of the Arduino IDEs, nor whether you are running on Windows, Linux or a MAC. All those variables can/might have an affect upon your outcome.

Ok, let's take a look at the 4.1 and the second part.
When the Teensy tries to program a device it looks for your Teensy on all USB ports and sends a request (serial baud rate or HID feature report) to automatically switch to programming mode.
If previously written to Teensy is not listening for USB communication, automatic entry to programming mode is not possible. A physical pushbutton is provided to allow recovery from bad code. Pressing the button button puts Teensy into programming mode. It is not a "reset button" which restarts your program. The button is dedicated to recovery from bad code. A Program pin also allows external hardware to force entry to programming mode.
This is obviously what is happening in the second part of the T4.1 example above.

I suspect a similar situation exists for the other examples.
Show us your code and we may be able to point out why this is happening.
If the program is similar to that shown in #p1 they you have nothing in Loop and this gives no/little option for the Teensy programming to grab control of the Teensy and re-program it. Hence the need to press the program button.
 
First of all you don't say whether you are using PlatformIO or either of the Arduino IDEs, nor whether you are running on Windows, Linux or a MAC. All those variables can/might have an affect upon your outcome.
you must have missed it, post #10 line 3
Pressing the button button puts Teensy into programming mode. It is not a "reset button
I would have thought it was clear I understand this from what I wrote. Of course the program button is not a reset. However I do think it's reasonable to think of the ON/OFF input as a reset function, albeit with a 4 sec delay.
Show us your code and we may be able to point out why this is happening.
It really doesn't matter which of my many sketches I use. If you would like to see a simple example see post #1 - happy to be proven wrong :)
 
you must have missed it, post #10 line 3
Your right I did miss it apologies, I did search but am suffering from a headache this morning so I will blame that!!
you must have missed it, post #10 line 3

I would have thought it was clear I understand this from what I wrote. Of course the program button is not a reset. However I do think it's reasonable to think of the ON/OFF input as a reset function, albeit with a 4 sec delay.

It really doesn't matter which of my many sketches I use. If you would like to see a simple example see post #1 - happy to be proven wrong :)
See my last sentence in post #18.
 
What is "Port" set to in the Arduino IDE, and do you have more than one Teensy plugged in at the same time?
 
Based on all your responses it seems none of you are experiencing the inconsistencies / symptoms I've explained in post #10, right?

I ask because I'm surprised many others haven't experinced the same issues, given I'm just using the standard IDE+TD on Windows

What is "Port" set to in the Arduino IDE, and do you have more than one Teensy plugged in at the same time?
Teensy port, always. More than one teeny, sometimes, but it happens either way.
 
I ask because I'm surprised many others haven't experinced the same issues, given I'm just using the standard IDE+TD on Windows
It's definitely not normal. Especially this:
Choose another sketch, and upload:
Serial stream DOES NOT STOP when the upload starts.
Upload completes, but Serial output is still from previous sketch, that is still running
Push the Program button on T4.1 sketch loads and runs normally.
makes no sense at all - the IDE can't program a T4.1 while it's still actively running a sketch, so where is the upload going?
 
makes no sense at all - the IDE can't program a T4.1 while it's still actively running a sketch, so where is the upload going?
It makes eminent sense.
Please read below from the PJRC T4.1 page and my following comment.

Programming
  1. Teensy Loader Programming of Teensy's flash memory is done by the Teensy Loader application. Normally the Arduino IDE or other software is used to compose code, and it automatically runs Teensy Loader as needed. If you have compiled code in HEX file format, Teensy Loader can be used stand-alone to write your HEX file into Teensy's flash memory.
  2. Automatic Software Entry to Program Mode While developing with Teensy, loading normally happens automatically after compiling your program. A "teensy_reboot" utility looks for your Teensy on all USB ports and sends a request (serial baud rate or HID feature report) to automatically switch to programming mode.
  3. Program Pushbutton / Pin If code previously written to Teensy is not listening for USB communication, automatic entry to programming mode is not possible. A physical pushbutton is provided to allow recovery from bad code. Pressing the button button puts Teensy into programming mode. It is not a "reset button" which restarts your program. The button is dedicated to recovery from bad code. A Program pin also allows external hardware to force entry to programming mode.

The code used which is causi9ng the problems includes loop{}.
Under this circumstance condition 3 above applies, the program is not listening for USB communication and the Program button has to be pressed.

All the above fits the problem described for T4.1 and I suspect for the other cases.
 
No it doesn't, because the IDE is indicating something is being programmed. If the Teensy wasn't responding to the programming request it would say so.

An empty loop() function doesn't mean the Teensy isn't listening for USB communication. Quite the opposite, it will be spending practically all its time inside yield(). In either case USB communication is handled via an interrupt handler, so unless that interrupt (or all interrupts) is disabled the contents of the sketch is mostly irrelevant. Try it for yourself: program a sketch with empty setup() and loop() and see that you do not need to push the program button to trigger re-programming while it is running.
 
Choose another sketch, and upload:
Serial stream DOES NOT STOP when the upload starts.
Upload completes, but Serial output is still from previous sketch, that is still running
Push the Program button on T4.1 sketch loads and runs normally.
I DO NOT believe that the upload completes during this phase. If it does not but prompts for the Program button to be pressed but @ninja2 misses that all that is described will occur.
I contend that my explanation fits the case.
 
Back
Top