T4.1 wont appear as a serial USB device unless I put analogRead in the code.

Status
Not open for further replies.

JaredReabow

Well-known member
In the code below, if I do not put the analogRead function in the code, the Teensy wont show up via USB and I have to press the reset to upload new code.
However if I put the analogRead in, it functions without issue.
Any help would be appreciated.


Code:
#define randomVar 16

void setup() 
{ 
 Serial.begin(9600); 
 Serial4.begin(57600);  //Baud Rate for AT-command Mode.  
 analogRead(randomVar );
 Serial.println("*** ONLINE ***"); 
} 

void loop() 
{ 
 if (Serial4.available()){
   Serial.println(Serial4.readString()); 
   
 }
 if (Serial.available()){
   Serial4.write(Serial.read());
   tone(13,1000,40);
  } 
}
 
I verified your sketch on a T4.1 and with or without the analogRead(randomVar ); line, it shows a serial USB device [COM port].
Did you set Menu > Tools > USB Type to Serial?

Using Arduino 1.8.13 & Teensyduino 1.53 on Windows 10.

Regards,
Paul
 
Maybe Teensy is transmitting "*** ONLINE ***" before your PC is able to complete USB detection, and then you interpret the lack of that message as the USB device not working? But that doesn't explain needing the press the button to reprogram.

I can confirm I commented out the analogRead line and I was able to repeatedly upload without pressing the pushbutton.

To get "*** ONLINE ***" to appear in the serial monitor, use this:

Code:
#define randomVar 16

void setup() 
{ 
 Serial.begin(9600);
 while (!Serial) {
   // wait for Arduino serial monitor to open
 }
 Serial4.begin(57600);  //Baud Rate for AT-command Mode.  
 //analogRead(randomVar );
 Serial.println("*** ONLINE ***"); 
} 

void loop() 
{ 
 if (Serial4.available()){
   Serial.println(Serial4.readString()); 
   
 }
 if (Serial.available()){
   Serial4.write(Serial.read());
   tone(13,1000,40);
  } 
}
 
I am in serial mode, I also tried whilst in mtp mode.

It is not that it doesn't print to serial, it is that the device doesn't even show up on USB or run blink
 
Just to repeat, I did indeed run the code you shared on a Teensy 4.1 here. I was able to upload several times without pressing the pushbutton. I can’t explain the behavior you have described. But I did test with a board here and it definitely did not do what you have said.
 
Last edited:
Might really help to understand what exactly is your setup?

Windows? Linux? MAC? ... What version of Arduino are you running? What version of Teensyduino are you running?

Anything changed on your machine, like using different versions of Core or ...

Also understand there is a significant difference between running as USB type of Serial and running on USB type of MTP.

With MTP you are not using Serial, but instead you are using Serial Emulation.

And for example if you are running on Linux fixed a problem where for example if you built using some USB type that did not use hardware, the Serial monitor would not work nor could you program the board without hitting the program button... This was an issue with udev rules...

But again not sure if this is your issue or not... I am guessing Not:

But again if you are trying to use MTP you might try using the MTP with Serial option and see if that solves it for you...

If this is not the issue, could be simple timing issue as mentioned and your analogRead() really did not do anything except delay a bit of time.

Most of my sketches will have something in it like what Paul put in, except I put in some timeout such that my code an work when USB is not connected

// which will wait up to 5 seconds. Note I do this at the start so mills is sufficient otherwise I would put in an elapsedMillis object set to zero before this and check that value...
Something like: while (!Serial && millis() < 5000) {
 
Even though the while (!Serial) doesn't explain the lockup where pressing the pushbutton was needed, I do believe it's time to reconsider the previously failed attempt to have Serial.begin() wait.

I'm going to put this Serial.begin() delay we previous tried back into the code.

https://github.com/PaulStoffregen/cores/commit/14f143db8e68f1889861b8ad631cd983cc98403e

Before it was 2.5 seconds, which was mostly motivated by the slowness of Windows 7's and weird delays USB drivers. It also just wanted 2.5 seconds in every case, even if no USB cable was connected. Looks like about 0.6 seconds is needed for normal USB enumeration to become apparent.

I want to give this another try for 1.54-beta8 and we'll decide whether it causes too much pain before a 1.54 release. If we can have more programs written for Arduino Uno "just work" and not have to go to the much worse way Uno uses where opening the port hard reboots the main processor, even though it adds startup delay the improvement in usability might be worthwhile.

Or maybe, like before, the extra startup delay will be too painful for too many people? I am listening and this might get pulled out yet again before 1.54 releases... but I do want to eventually find a way to avoid the confusion people experience when running programs written for Uno & Mega and they believe Teensy isn't working before they don't see the first string their code prints.
 
Even though the while (!Serial) doesn't explain the lockup where pressing the pushbutton was needed, I do believe it's time to reconsider the previously failed attempt to have Serial.begin() wait.

I'm going to put this Serial.begin() delay we previous tried back into the code.

https://github.com/PaulStoffregen/cores/commit/14f143db8e68f1889861b8ad631cd983cc98403e

Before it was 2.5 seconds, which was mostly motivated by the slowness of Windows 7's and weird delays USB drivers. It also just wanted 2.5 seconds in every case, even if no USB cable was connected. Looks like about 0.6 seconds is needed for normal USB enumeration to become apparent.

I want to give this another try for 1.54-beta8 and we'll decide whether it causes too much pain before a 1.54 release. If we can have more programs written for Arduino Uno "just work" and not have to go to the much worse way Uno uses where opening the port hard reboots the main processor, even though it adds startup delay the improvement in usability might be worthwhile.

Or maybe, like before, the extra startup delay will be too painful for too many people? I am listening and this might get pulled out yet again before 1.54 releases... but I do want to eventually find a way to avoid the confusion people experience when running programs written for Uno & Mega and they believe Teensy isn't working before they don't see the first string their code prints.

A possible solution could be to enlarge Serial.begin() to Serial.begin(baudRate, timeOut or delay) where Serial.begin(baudRate) /Serial.begin() defaults to a timeout/delay of 2.5 seconds, or whatever is determined to be the better compromise.

If Serial.begin(baudRate,0) is used then there is NO delay nor TimeOut.

The discussion then becomes which is better to use "timeout until usb is up" or simply delay.
I would suggest the latter as it would allow slow hardware to become alive and would be the better and simpler for the less knowledgeable migrating from UNO etc.

If a timeout scenario is required it can still be implemented with
Code:
while (!Serial.begin(9600,0) && (millis()  <= 5000)){}  ;
 
Last edited:
As noted in post#3 by Paul - it works here - using T_4.1 and a known good cable.

Not ever seen such a thing here, other than using the while(!Serial) in some fashion needed before the fist output will show - and there were times when too much output to Serial before it was online would act funny (but not like this) - that was in Beta IIRC.

@JaredReabow - it isn't noted which version of TeensyDuino is in use? And what OS?

Does the code for Serial4 and Serial making the tone work as expected - when USB works? Just fails to do the next Auto Upload?

This really should work and normally does without odd 'Random' code being needed - so anyone trying to help to get to a solution will be puzzled as it seems to work on their system - so there is something missing that may be the cause of the trouble - so various questions to find that missing piece ...

Windows only here. TD 1.53 had good SerMon - something odd ( at least with full speed output ) with TD 1.54 Beta 7 noted on that thread - may not apply here.


@BriComp: the Serial.begin( ## ) is a void placeholder just for compatibility. When the sketch builds with Serial - it comes online by default when the computer connects without calling the .begin(). In which case the note timeout with while (!Serial) in some fashion will detect when the computer connects.
 
The purpose here is to improve compatibility with code designed using Arduino Uno & Mega, even if we can't get to the bottom of what really went wrong with JaredReabow's system.

I do not believe adding a Teensy-specific extension to the inputs of Serial.begin() helps serve that purpose. Since Serial.begin() doesn't actually do anything on Teensy, anyone who would use a Teensy-specific combination of inputs could just as well delete or comment out Serial.begin() from their program.
 
The purpose here is to improve compatibility with code designed using Arduino Uno & Mega, even if we can't get to the bottom of what really went wrong with JaredReabow's system.

I do not believe adding a Teensy-specific extension to the inputs of Serial.begin() helps serve that purpose. Since Serial.begin() doesn't actually do anything on Teensy, anyone who would use a Teensy-specific combination of inputs could just as well delete or comment out Serial.begin() from their program.

Your data on using Serial.begin on this page https://www.pjrc.com/teensy/td_serial.html states that:-

"Serial.begin()
Initialize the Serial object. The baud rate is ignored and communication always occurs at full USB speed."

I can't find anywhere where it is documented as doing nothing on Teensy.
 
Status
Not open for further replies.
Back
Top