T3.1 w/ TD 1.2: I2C failure after power loss, requiring re-upload to fix

Status
Not open for further replies.

zelfor

Member
Hi!

I'm using a Teensy 3.1 with Teensyduino 1.20, along with a GY-521 ( MPU-6050 breakout, 6DOF I2C sensor ).

I'm using the GY-521 as a source for X/Y rotation translated into Joystick output by the Teensy 3.1 using the Serial/Keyboard/Mouse/Joystick configuration.

I'm finding that on power loss to the Teensy 3.1 ( unplugged, PC off and power off, etc ), upon restoration of power the I2C communication seems to fail. I wind up having to reupload my code via the Teensy Loader. After the Teensy 3.1 reboots upon upload, my code works correctly until the next time there is a complete power loss to the Teensy.

I surmise that it's the I2C failing after power loss because A: A pure joystick sketch ( random movement ) works fine after power loss, B: the I2C communication after reupload works fine, and C: after power loss, hitting the reset button does not get the application working again.

Any ideas on the problem or further troubleshooting I can do?

Code:
// I2C device class (I2Cdev) demonstration Arduino sketch for MPU6050 class
// 10/7/2011 by Jeff Rowberg <jeff@rowberg.net>
// Updates should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib
//
// Changelog:
//      2013-05-08 - added multiple output formats
//                 - added seamless Fastwire support
//      2011-10-07 - initial release

/* ============================================
I2Cdev device library code is placed under the MIT license
Copyright (c) 2011 Jeff Rowberg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/


#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"
MPU6050 accelgyro;

int16_t ax, ay, az;
int16_t gx, gy, gz;
#define OUTPUT_READABLE_ACCELGYRO
#define LED_PIN 13
bool blinkState = false;

double pitch = 512, roll = 512;

IntervalTimer joyupdate;

void updateJoy(){
  Joystick.X(test(pitch));
  Joystick.Y(test(roll));
  Serial.print(test(pitch));
  Serial.print("\t");
  Serial.println(test(roll));
}

void setup() {
    
    // join I2C bus (I2Cdev library doesn't do this automatically)
        Wire.begin();
		TWBR = 24;
	Serial.begin(38400);
    

    // initialize device
    Serial.println("Initializing I2C devices...");
    accelgyro.initialize();

    // verify connection
    Serial.println("Testing device connections...");
    Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
    joyupdate.begin(updateJoy, 50000);
}

const float alpha = 0.1;

double fXg = 0;
double fYg = 0;
double fZg = 0;


void loop() {
	noInterrupts();
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
	interrupts();
    
    
    	//Low Pass Filter
    fXg = ax * alpha + (fXg * (1.0 - alpha));
    fYg = ay * alpha + (fYg * (1.0 - alpha));
    fZg = az * alpha + (fZg * (1.0 - alpha));
    roll  = -1.0*(atan2(fYg, sqrt(pow(fXg,2) +pow(fZg,2)))*180.0)/M_PI;
    pitch = -1.0*(atan2(fXg, sqrt(pow(fYg,2) + pow(fZg,2)))*180.0)/M_PI;
    delay(5);
    
}

int test( double ang){
  if(abs(ang) > 70){
    if(ang > 0){
        return 1023;
    }else{
      return 0;
    }
  }else{
    if(ang > 0){
      return constrain(((abs(ang))*25.6) + 512, 0, 1023);
    }else{
        return constrain(512-((abs(ang))*25.6),0, 1023);
    }
  }
}
 
Last edited:
Try adding a delay before you initialize the MPU6050.

With FreeIMU, a problem was discovered where the MPU6050 is not actually ready immediately at powerup. The problem was never seen with normal Arduino, where their bootloader imposes a slow startup time.

Teensy 3.1 starts up very quickly. If you're seeing the MPU6050 work after reprogramming, when it's already been powered up, but not at cold boot, the problem may be as simple as the Teensy 3.1 booting up too fast and the MPU6050 simply isn't ready (and the library isn't properly designed to detect this and wait).
 
That did the trick!

I added a 50ms delay and it's working great after power loss.

You sir are epic! I'll gladly continue to support your great work by purchasing more Teensy boards!
 
Last edited:
I had the same problem with an I2C LCD and this fixed it for me too (20ms was already enough, so also using 50ms now).
You indeed are the man Paul, thanks!
 
Status
Not open for further replies.
Back
Top