Teensy 3.1 serial stops responding after a minute

Status
Not open for further replies.

overthrow

Member
I'm using a Teensy 3.1 connected to a BNO055 9 DOF IMU from Adafruit.com. My script is fairly straightforward. I'm using their library and I run the following in loop(). I'll get data for anywhere from 30 seconds to 3 minutes and then I get nothing. If the IMU was failing I would at least get the commas printed. It's not Arduino software (1.6.5) because the same thing happens when I read data through a C# script in Unity3D. The only way to get it working again is to unplug the USB cable from it. If I re-flash it I still get no serial data. I'm on Windows 8.1, using COM6. There is a second Teensy 3.1 on COM5 that is running a different script and it's not having any problems. Ideas? Do I have a bad Teensy?

imu::Quaternion quat = bno.getQuat();
Serial.print(quat.w(), 4);
Serial.print(",");
Serial.print(quat.x(), 4);
Serial.print(",");
Serial.print(quat.y(), 4);
Serial.print(",");
Serial.println(quat.z(), 4);
delay(BNO055_SAMPLERATE_DELAY_MS); // currently set at 20ms
 
Usually when we've heard about problems like this before it turns out to be the Teensy is generating output faster than the PC can handle it.
 
When it's in the stopped condition, can you measure the voltage on SDA and SCL. Maybe one or both are stuck low? At least this could help narrow the problem down to the I2C or the USB side...
 
I disconnected the IMU and have another script I'm working on that is acting very strangely. If I upload a basic script that is just Serial.begin and a Serial.println in loop it works ok. If I load this other script that's a bit larger (262 lines) after it uploads and I click Serial Monitor it continuously loops through the Teensy programming continuously reprogramming and not displaying anything in the serial monitor. What could I possibly put in the script that would make the programmer loop like that?

I ran the script on the other Teensy. No issue with reprogramming. Something is off with this board.
 
Last edited:
What could I possibly put in the script that would make the programmer loop like that?

Calling _reboot_Teensyduino_() will do it. That function makes a request for the bootloader to take over. When the Mini54 notices (which could be up to a 1/4 second later), tt asserts the reset signal to stop your program, and then it takes control of the MK20 and causes it to run the bootloader. If Teensy Loader is in auto mode, as it would normally be if you're doing the programming from Arduino, it'll automatically reprogram and reboot the board.

I realize that's probably not what you did. I can't see your 262 line code. But you did ask what could do this, so I tried to answer. If you'd posted the actual 262 lines (eg, the "forum rule"), I would have instead spent this time looking at your actual code in an attempt to help more directly.
 
This code works fine on one Teensy 3.1 but not the other.

Code:
const int ledPin1 = 12;
const int ledPin2 = 10;
const int ledPin3 = 11;
const int ledPin4 = 13;
const int ledPin5 = 26;
const int ledPin6 = 27;
const int ledPin7 = 28;
const int ledPin8 = 29;

unsigned long sensorSetupStartTime;

int pressureBaselineDeviation[8][6];

int currentReading;

int sensorMax[8][6] = {
                {0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0},
                {0, 0, 0, 0, 0, 0}
};

void setup() {
  Serial.begin(115200);
  delay(1000);
  while (!Serial) {
     delay(100);
     };
   
  pinMode(0, OUTPUT);
  pinMode(1, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
   
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
  pinMode(ledPin5, OUTPUT);
  pinMode(ledPin6, OUTPUT);
  pinMode(ledPin7, OUTPUT);
  pinMode(ledPin8, OUTPUT);
 
  Serial.println("OK");
  sensorSetup();
  //digitalWrite(ledPin8, HIGH);
}

void sensorSetup()
{
  int sensorValue;
 
  for (int t = 0; t< 50; t++)
  {
    for (int j = 0; j<6; j++)
    {
        digitalWrite(j, HIGH);
        for (int k = 0; k < 8; k++)
        {
          sensorValue = analogRead(k+1);
          
          if (sensorValue > sensorMax[k][j]) 
          {
            sensorMax[k][j] = sensorValue;
          }
     
        }
        digitalWrite(j-1, LOW);
      }
    }
}


void loop() {
  
for (int j = 0; j < 6; j++)
  {
    digitalWrite(j, HIGH);
    for (int k = 14; k < 22; k++)
    {
      
      // measure of how much the reading changed from the baseline reading we took in the setup function
      pressureBaselineDeviation[k][j] = analogRead(k+1) - sensorMax[k][j];
      
      if ((k == 7) && (j == 5))
      {
        int pressureMaxRow = 0;
        int pressureMaxCol = 0;
        
        for( int kmax = 0; kmax < 8; kmax++)
        {
          for (int jmax = 0; jmax < 6; jmax++)
          {
              if (kmax > 0 || jmax > 0)
              {
                if (pressureBaselineDeviation[kmax][jmax] > pressureBaselineDeviation[pressureMaxRow][pressureMaxCol]  )
                {
                  pressureMaxRow = kmax;
                  pressureMaxCol = jmax;
                }
              }
          }
        }
        
        if ((pressureMaxRow == 0) && (pressureMaxCol == 0))
        {
          if (pressureBaselineDeviation[0][0] > 20)
          {
            Serial.print(pressureMaxRow);
            Serial.print(",");
            Serial.print(pressureMaxCol);
            Serial.print(",");
            Serial.println(pressureBaselineDeviation[pressureMaxRow][pressureMaxCol]);
          }
        }
        else
        {
            if (pressureBaselineDeviation[pressureMaxRow][pressureMaxCol] > 7)
            {
              Serial.print(pressureMaxRow);
              Serial.print(",");
              Serial.print(pressureMaxCol);
              Serial.print(",");
              Serial.println(pressureBaselineDeviation[pressureMaxRow][pressureMaxCol]);
            }
        }
        
      }
      
    }
    digitalWrite(j, LOW);
  }

delay(20);

}

void OnLed()
{
  float a = 0;
  float b = 0;
  
  if (Serial.available() > 0)
  {
    
  }
  
  
  
  if (a == 1)
  {
    if (b == 1)
    {
      digitalWrite(ledPin1, HIGH);
    }

    if (b == 0)
    {
      digitalWrite(ledPin1, LOW);
    }
  }

  if (a == 2)
  {
    if (b == 1)
    {
      digitalWrite(ledPin2, HIGH);
    }

    if (b == 0)
    {
      digitalWrite(ledPin2, LOW);
    }
  }

  if (a == 3)
  {
    if (b == 1)
    {
      digitalWrite(ledPin3, HIGH);
    }

    if (b == 0)
    {
      digitalWrite(ledPin3, LOW);
    }
  }

  if (a == 4)
  {
    if (b == 1)
    {
      digitalWrite(ledPin4, HIGH);
    }

    if (b == 0)
    {
      digitalWrite(ledPin4, LOW);
    }
  }
  
  if (a == 5)
  {
    if (b == 1)
    {
      //Serial.println("LED 5 ON");  
      digitalWrite(ledPin5, HIGH);
    }

    if (b == 0)
    {
      digitalWrite(ledPin5, LOW);
    }
  }

  if (a == 6)
  {
    if (b == 1)
    {
      digitalWrite(ledPin6, HIGH);
    }

    if (b == 0)
    {
      digitalWrite(ledPin6, LOW);
    }
  }

  if (a == 7)
  {
    if (b == 1)
    {
      digitalWrite(ledPin7, HIGH);
    }

    if (b == 0)
    {
      digitalWrite(ledPin7, LOW);
    }
  }

  if (a == 8)
  {
    if (b == 1)
    {
      digitalWrite(ledPin8, HIGH);
    }

    if (b == 0)
    {
      digitalWrite(ledPin8, LOW);
    }
  }

  
}
 
This code works fine on one Teensy 3.1 but not the other.

The code looks fine.

Any chance something's happened with the hardware, perhaps one or more signals touching or somehow coupling to the Program signal? If the Mini54 sees the Program signal go low, it resets the MK20 and runs the bootloader.
 
I ran this and see nothing in serial monitor (I wouldn't expect to though but it didn't grey out). I didn't see the LED blink.

Code:
#define qBlink() (digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN) ))

void setup() {
  Serial.begin(38400);
  digitalWrite(LED_BUILTIN, HIGH);
  pinMode(LED_BUILTIN, OUTPUT);
  while (!Serial && (millis () <= 4000))
    qBlink();
}

void loop() {

}
 
The shield I built for the Teensy uses pin 13. It must have interfered with blinking. I re-ran it disconnected from my shield. The LED comes on and stays on (2 minutes and counting).
 
CROSSPOSTED - I'll leave it though ...

RE Post #9:: The loop code should have a qBlink(); as well to show it never shows up, and have a Serial.println("OK"); of some sort in there to know the USB wasn't connecting.

Does DevMgr show anything useful? Have you rebooted with this single Teensy?

This may not point to your problem if the Teensy isn't taking code, or has a wiring issue per Post #8 - but it is a simple blink sketch that should show when you get life on the Teensy. Forum search on qBlink will provide other variations I've made.

Code:
void setup() {
  Serial.begin(38400);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);   // On Teensy must set to OUTPUT before setting value
  qBlink();
  delay(200);
  qBlink();
  delay(500);
  while (!Serial && (millis () <= 4000))
    qBlink();
  Serial.println("OK");
}

void loop() {
   qBlink();
   Serial.print(".");
}
 
Last edited:
When I run this script I get continuous .... and the LED stays lit. I try to re-load my original program again and during programming I see the Teensy disappear from Device Manager and if I run the serial monitor it runs in the programming loop again. There are devices connected to any pins on it now.

I think I'm just going to go to Microcenter and buy another one for $20. I appreciate all the help but I've already spent too long on this.
 
Last edited:
That loop is running way faster than your eye can see the blink by a factor of a couple thousand. Put a delay in the loop and try again.

Are you seeing any USB output? Leaving DevMgr is a sign of something odd for sure. I assume you meant :: There are NO devices connected to any pins on it now.

Code:
void loop() {
   qBlink();
   Serial.print(".");
  delay(500);
}
 
Last edited:
I went through and fixed the pin numbers in the script and no more looping during programming. Very odd. It seems to be working now. Thanks everyone for your help.
 
Don't let that stop you from buying more Teensy's :)

I should edit my qBlink with a comment on the PIN# for BUILTIN.

I left the delay() out of loop as seeing the light means it is active and not in program mode (it should be dimmer by about half). I should update with a version using the library elapsedMicros show multitasking usage to control blink and USB output rate.
 
Got this working as a test under Windows 10:

Code:
#define qBlink() (digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN) ))  // Pin13 on T3.x & LC
void setup() {
  Serial.begin(38400);
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);   // On Teensy must set to OUTPUT before setting value
  qBlink();
  delay(200);
  qBlink();
  delay(500);
  while (!Serial && (millis() <= 4000))
    qBlink();
  Serial.println(millis());
  Serial.println("OK");
}

elapsedMillis emilUSB;
elapsedMicros emicBlink;
int iLF = 66;

void loop() {
  if ( emicBlink > 25000 ) {
    qBlink();
    emicBlink = 0;
  }
  if ( emilUSB > 250 ) {
    emilUSB = 0;
    if ( --iLF < 1 ) {
      iLF = 66;
      Serial.println(":");
    }
    else
        Serial.print(".");
  }
}
 
Last edited:
Status
Not open for further replies.
Back
Top