Question on external power, Teensy 4.0

PickyBiker

Well-known member
The Teensy 4.0 runs a program as soon as I plug in the USB cable. However, if it is powered via 5v and GND and the USB cable is not plugged in, it doesn't run the program. I have verified the 5v is present at the teensy and the 3.3v is also present.

Also, if it is powered externally and the USB cable is then plugged in, it immediately starts.

Why won't it run the program when powered externally?

External pwr --- USB cable --- Does it start?
no --- yes --- yes
yes --- yes --- yes
yes --- no --- no
 
Your sketch very likely contains a line in your setup() function like while (!Serial) {}. This line will cause your Teensy to wait forever until a serial connection is available for your Teensy to put out any status. When you connect the USB cable to your PC, you get a serial connection by default, as your PC is supplying that. Without a smart device at the other end of the cable (when using the USB cable to supply power, or when you supply external power), your Teensy is just waiting for something to connect.

Hope that helps . . .

Mark J Culross
KD5RXT
 
In setup() you probably have something like
Code:
Serial.begin(9600);
while (!Serial);
This means that the code will wait forever until the USB serial is present.
What you should have is something like
Code:
    Serial.begin(9600);
    while (!Serial && (millis() < 3000));
This code will wait for up to 3 seconds for the Serial USB to be available. If it is NOT available after 3 seconds your program will continue.
A simple mistake many of us have made!
 
And then, what append when I plug the USB cable during runtime? Are the Serial ports automaticaly activated ? How to check the USB serial ports presence to only use them when PC is connected ??
Something like:
Code:
void loop (void)
{
if(Serial)
  Serial.println("Serial is connected");
else
  DoOtherThings();
}
 
Are the Serial ports automaticaly activated ?
Yes, if USB Serial was included - it will connect when Host is present and running a good SerMon on a good cable.

The " if (Serial) " is the easy proper test.
Though p#4 code as written will send a Large/Fast/Uncontrolled amount of '.println()' to the host and may overwhelm the host.
Code:
void loop (void)
{
if(Serial)
{
  Serial.println("Serial is connected");
  delay(10);
}
else
  DoOtherThings();
}
 
Yes, if USB Serial was included - it will connect when Host is present and running a good SerMon on a good cable.

The " if (Serial) " is the easy proper test.
Though p#4 code as written will send a Large/Fast/Uncontrolled amount of '.println()' to the host and may overwhelm the host.
Typically, you want to do a delay, since sometimes the USB is not connected immediately, and you can miss the first few println's. I.e.

Code:
void loop (void)
{
  // Wait up to 3 seconds for the USB connection to be setup.
  while (!Serial && millis () < 3000)
    ;

  if(Serial)
    {
       // Note, Teensys do not need the begin, but if you run your scripts on boards
       // that don't have an integrated USB connection you will need it.  However on
       // those boards, you need to get the right speed.  9600 or 19200 are two
       // common values
      Serial.begin (19200);

      Serial.println("Serial is connected");
      delay(10);
    }
else
  DoOtherThings();
}

Note, you can print print/println/printf even if the USB connection is not active, and the Teensy libraries will just throw away the output (which is why you can miss the first few printlns).
 
Back
Top