Serial port function on Teensy 3.1

Status
Not open for further replies.

onehorse

Well-known member
I just got my first Teensy (3.1) and hooked it up to my current project--- a Nokia 5110 display showing sensor fused AHRS from a MPU-9150. The Teensy 3.1 replaces a 3.3 V 8 MHz Pro Mini. When I start the sketch, the display shows the header info but when I get to the first serial write it seems like the sketch stops, no further output of any kind. I verified I can get serial out using the blink program, and that I set the proper serial port (COM9) showing as the Teensy USB Serial port in the device manager, but I get nothing from the Arduini IDE serial monitor. Here is the first (functional) part of the sketch:

#include <Wire.h>
#include "I2Cdev.h"
#include "MPU6050_9Axis_MotionApps41.h"
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>

// Using NOKIA 5110 monochrome 84 x 48 pixel display
// pin 9 - Serial clock out (SCLK)
// pin 8 - Serial data out (DIN)
// pin 7 - Data/Command select (D/C)
// pin 5 - LCD chip select (CS)
// pin 6 - LCD reset (RST)
Adafruit_PCD8544 display = Adafruit_PCD8544(9, 8, 7, 5, 6);

// Declare device MPU6050 class
MPU6050 mpu;

// global constants for 9 DoF fusion and AHRS (Attitude and Heading Reference System)
#define GyroMeasError PI * (60.0f / 180.0f) // gyroscope measurement error in rads/s (shown as 60 deg/s)
#define GyroMeasDrift PI * (0.0f / 180.0f) // gyroscope measurement drift in rad/s/s (shown as 0.0 deg/s/s)
// There is a tradeoff in the beta parameter between accuracy and response speed.
// In the original Madgwick study, beta of 0.041 (corresponding to GyroMeasError of 2.7 degrees/s) was found to give optimal accuracy.
// However, with this value, the LSM9SD0 response time is about 10 seconds to a stable initial quaternion.
// Subsequent changes also require a longish lag time to a stable output, not fast enough for a quadcopter or robot car!
// By increasing beta (GyroMeasError) by about a factor of fifteen, the response time constant is reduced to ~2 sec
// I haven't noticed any reduction in solution accuracy. This is essentially the I coefficient in a PID control sense;
// the bigger the feedback coefficient, the faster the solution converges, usually at the expense of accuracy.
// In any case, this is the free parameter in the Madgwick filtering and fusion scheme.
#define beta sqrt(3.0f / 4.0f) * GyroMeasError // compute beta
#define zeta sqrt(3.0f / 4.0f) * GyroMeasDrift // compute zeta, the other free parameter in the Madgwick scheme usually set to a small or zero value
#define Kp 2.0f * 5.0f // these are the free parameters in the Mahony filter and fusion scheme, Kp for proportional feedback, Ki for integral
#define Ki 0.0f

int16_t a1, a2, a3, g1, g2, g3, m1, m2, m3; // raw data arrays reading
uint16_t count = 0; // used to control display output rate
uint16_t delt_t = 0; // used to control display output rate
uint16_t mcount = 0; // used to control display output rate
uint8_t MagRate; // read rate for magnetometer data

float pitch, yaw, roll;
float deltat = 0.0f; // integration interval for both filter schemes
uint16_t lastUpdate = 0; // used to calculate integration interval
uint16_t now = 0; // used to calculate integration interval

float ax, ay, az, gx, gy, gz, mx, my, mz; // variables to hold latest sensor data values
float q[4] = {1.0f, 0.0f, 0.0f, 0.0f}; // vector to hold quaternion
float eInt[3] = {0.0f, 0.0f, 0.0f}; // vector to hold integral error for Mahony method


void setup()
{
Serial.begin(9600); // Start serial at 38400 bps

display.begin(); // Initialize the display
display.setContrast(58); // Set the contrast
display.setRotation(0); // 0 or 2) width = width, 1 or 3) width = height, swapped etc.

// Start device display with ID of sensor
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0,0); display.print("MPU9150");
display.setTextSize(1);
display.setCursor(0, 20); display.print("9 DOF sensor");
display.setCursor(0, 30); display.print("data fusion");
display.setCursor(20, 40); display.print("AHRS");
display.display();
delay(2000);

// Set up for data display
display.setTextSize(1); // Set text size to normal, 2 is twice normal etc.
display.setTextColor(BLACK); // Set pixel color; 1 on the monochrome screen
display.clearDisplay(); // clears the screen and buffer
display.display();


// initialize MPU6050 device
Serial.println(F("Initializing I2C devices..."));
delay(1000);

I see the Nokia display but do not see the first Serial print. I'm not sure it should matter but do I have to define SDA and SCL as A4 and A5 for I2C? I hope there is something obvious I am overlooking. Thanks for any help!
mpu.initialize();
 
Note when posting code, it should be inside '[' code ']' and '[' /code ']' blocks. Or go to the advanced menu and hit the '#' option in the 2nd button row.

To use i2c on the Teensy 3.1, you need to supply 4.7K ohm pull-up resistors for both SDA/SCL (A4/A5).

Are you hooking up pins 0/1 to the FTDI chip like you would for the Pro Mini? If that is the case, you need to use Serial1 instead of Serial. On the Teensy 3.x, Serial only refers to the USB port, and Serial1 refers to the first hardware serial port (there are 3 hardware serial ports in the Teensy). If you want to use the USB serial port and not bother with hooking up the FTDI chip, after the program starts hit the magnifying glass in the IDE to bring up the serial monitor. Note, the serial port is recreated each time the Teensy reboots. Typically I put a delay of a second or 2 in the begin() function, so I don't miss messages. I don't think a standard serial connection is created, but a private connection is used.
 
Thanks for the reply Michael!

I'm pretty sure the MPU-9150 breakout board I am using has pull up resistors on SDA and SCL. I am not using an FTDI chip or hardware serial. I am using the USB serial port that seems to work fine in the simplle blink and RTC sketches. I'll try a longer delay before the first write. I suspect the real problem is I am not talking to the MPU-9150.
 
Also, on the serial monitor, check your Tools > Serial Port setting.

On Teensy, the serial port setting is NOT used for upload. It only applies to the serial monitor. With a regular Arduino, you can't upload if the serial port setting is wrong. With Arduino, after a successful upload, you don't need to worry about the serial port setting. But with Teensy, the upload works regardless of the serial port setting, and the serial port doesn't even appear in the menu until Teensy is running your code (and becomes whatever USB device you set in Tools > USB Type).

If the serial port is set to your Arduino Mini or some other thing, it's possible Teensy is trying to send you info, simply on a different port.
 
Thanks everyone for your kind help. The serial wait allowed me to verify that I successfully got serial commo from the Teensy 3.1. As I suspected, it balked at the MPU-9150 initialize step. I inserted this :

/CODE


// initialize MPU6050 device
Serial.println("Initializing I2C devices...");
delay(1000);

for (int address=1; address <= 126; address++) {
Wire.beginTransmission(address); // Select address
if (!Wire.endTransmission(true)) {
Serial.printf("Found device at: %02Xh\n", address); }
else {
Serial.printf("Found no devices!"); }
}

mpu.initialize();


/CODE

and got nothing else but the "Initializing I2C devices..." message.

This library (Jeff Rowberg's) relies on a combination of Wire.h and I2Cdev.h. I suspect the problem is with the latter.

I was able to successfully run a sketch to get output from a LSM9DS0 sensor using the Teensy 3.1 using a very similar sketch that solely uses Wire.h for the I2C commo. Yet another sketch uses a bare bones I2C.h library that won't even compile on the Teensy but runs happily on the Pro Mini.

I need to chnage these libraries. Would you recommend converting all to Wire.h or to the modified I2C.h library I ran across at the forum main page? Thank again for your help!
 
Status
Not open for further replies.
Back
Top