Teensy 3.0 (TeensyDuino) + L3G4200D === “574” reading? Why? How to fix

arduinogyroteensy

I have an Arduino Uno, an Arduino Nano, a Teensy 3.0 and a 3-axis gyroscope (L3G4200D module from Parallax http://www.parallax.com/StoreSearchResults/tabid/768/txtSearch/gyroscope/List/0/SortField/4/ProductID/778/Default.aspx)

When I hook up the Arduino Nano or Arduino Uno to the gyroscope and use the sample code provided at the bottom of this page, it works great: http://learn.parallax.com/kickstart/27911

I get the readings I expect from the gyroscope.

When I hook up the same gyroscope to my Teensy 3.0, I get readings… but they aren't quite right. Instead of reading (x, y, z) 0, 0, 0 at rest, it will read 574, 0, 574. Sometimes 574, 0, 0, and sometimes 574, 574, 574.

When I move the module around, though, the numbers read closer to what I would expect from the Arduino boards.

I'm still very new to hardware. I understand some of the basics, and I know enough to make sure I have all the right wires plugged in to the right pins, etc. I'm sure there is something in the sample code that I'm using causing the problem – after all, this is arduino sample code that I'm using with a teensy…

I'm coding in the Arduino IDE w/ the TeensyDuino add-on. I have the board configured correctly in the Arduino options, of course.

Does anyone have any idea why my X and Z axis are reading 574 most of the time? What do I need to do to fix this?

[EDIT]

Per comment requests, here are images of my setup.

The Teensy setup: http://cl.ly/image/0E3R0x380d3R

The Arduino Nano setup: http://cl.ly/image/2S1J03123L16

As you can see, the setup is the same – just putting the pins in the right place for the controller I'm using. The Nano produces the correct results, while the Teensy does not.

[EDIT]

Would the Arduino being 16 bit while the Teensy being 32 bit cause problems? Do I need to adjust the math to calculate the two's complement differently? Use different variable types to store the resulting information? Or am I thinking wrong on this path?

Best Answer

I figured it out.

The problem was in the 32bit vs 16bit architecture of the Teensy vs Arduino boards. In a 32 bit environment, an int is 32 bits. In a 16bit environment, an int is 16 bits. At least, that's what I'm seeing in Teensy (32bit) vs Arduino (16bit). This makes a difference in the bit shifting / 2's complement calculation done for the X, Y, and Z values.

When I change the data types from int x, y, z; to short x, y, z; in my Teensy sketch, it gives me the correct reading for the values.