Teensy 3.1 not working with hmc6343 gyro/accelerometer

Status
Not open for further replies.

gbernal

Well-known member
Hi everyone,

I have an issue with computability hmc6343 library, I know the sensor is old but it's all I have and it works really well for what I need.
The example code from the library https://github.com/n-ovesen/hmc6343 works fine with an arduino uno, however when I plug it to teensy 3.1 I don't get anything in the serial window. I have tried with two different teensys so I don't think is a wiring issue.

Any ideas?

example code and library are from the link above.

Thank you in advanced
 
Last edited:
I just tried a different code and this is all i get from Teensy
Heading: 0 Tilt: 0 Roll: 0
Heading: 0 Tilt: 0 Roll: 0
Heading: 0 Tilt: 0 Roll: 0
Heading: 0 Tilt: 0 Roll: 0
Heading: 0 Tilt: 0 Roll: 0
Heading: 0 Tilt: 0 Roll: 0
Heading: 0 Tilt: 0 Roll: 0
Heading: 0 Tilt: 0 Roll: 0
Heading: 0 Tilt: 0 Roll: 0
Heading: 0 Tilt: 0 Roll: 0
Heading: 0 Tilt: 0 Roll: 0
Heading: 0 Tilt: 0 Roll: 0

Code:
#include <Wire.h> 

int compassAddress = 0x32 >> 1; // From datasheet compass address is 0x32 for write operations, 
                                // or 0x33 for read operations.
                                // shift the address 1 bit right, the Wire library only needs the 7
                                // most significant bits for the address

int heading = 0; // variable to hold the heading angle
int tilt = 0;    // variable to hold the tilt angle
int roll = 0;    // variable to hold the roll angle
int WLED = 13;    // variable to hold the roll angle


byte responseBytes[6];  // for holding the sensor response bytes

void setup() 
{ 
  delay(500);  //Wait at least 500 milli-seconds for device initialization
  Wire.begin();       // join i2c bus (address optional for master) 
  Serial.begin(9600); // start serial communication at 9600bps 
  pinMode(WLED, OUTPUT);  
  digitalWrite(WLED, HIGH); // just turn ON the onboard LED 
} 

void loop() 
{
  readSensor();  // read data from the HMC6343 sensor
  // Note that heading, tilt and roll values are in tenths of a degree, for example
  // if the value of heading is 1234 would mean 123.4 degrees, that's why the result
  // is divided by 10 when printing.
  Serial.print("Heading: ");
  Serial.print(heading / 10, DEC); 
  Serial.print(" Tilt: ");
  Serial.print(tilt / 10, DEC);
  Serial.print(" Roll: ");
  Serial.println(roll / 10, DEC);
  delay(200);                   // wait for half a second 
}
  
void readSensor() 
{ 
  // step 1: instruct sensor to read echoes 
  Wire.beginTransmission(compassAddress); // transmit to device
                          // the address specified in the datasheet is 66 (0x42) 
                          // but i2c adressing uses the high 7 bits so it's 33 
  Wire.write(byte(0x50)); // Send a "Post Heading Data" (0x50) command to the HMC6343  
  Wire.endTransmission(); // stop transmitting 
 
  // step 2: wait for readings to happen 
  delay(2); // datasheet suggests at least 1 ms 
  
  // step 3: request reading from sensor 
  Wire.requestFrom(compassAddress, 6); // request 6 bytes from slave device #33 
 
  // step 4: receive reading from sensor 
  if (6 <= Wire.available()) // if six bytes were received 
  {
    for (int i = 0; i<6; i++) {
      responseBytes[i] = Wire.read();
    }
  }
  
  heading = ((int)responseBytes[0]<<8) | ((int)responseBytes[1]); // heading MSB and LSB
  tilt = (((int)responseBytes[2]<<8) | ((int)responseBytes[3]));  // tilt MSB and LSB
  roll = (((int)responseBytes[4]<<8) | ((int)responseBytes[5]));  // roll MSB and LSB
}

Your help will be greatly appreciated
 
I use this to search the I2C bus for a device. Perhaps it will help you see if your address is correct.

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

Thant's a nifty piece of code, unfortunately it doesn't find the hmc6343 addresses. Tested it with my other accelerometer a LSM9DS0 and it does see that the HMC6343.

Argh!
 
Do you have pullup resistors on SDA and SCL? With Teensy3, real pullup resistors are required.

You're reading all zeros, so maybe the pins are stuck at zero? A voltmeter might also help. Since the sensor is only read once every 200 ms, both pins should be at 3.3V most of the time.

Also, you will need an int16_t cast, like this:

Code:
heading = (int16_t)((responseBytes[0]<<8) | responseBytes[1]);

This is a common 16 vs 32 bit issue that happens with poorly written C code.

However, since you're seeing all zeros and Wire.endTransmission() is returning an error status, my guess is you're missing the 2 resistors, or the wiring is incorrect so the 2 signals are stuck at zero.
 
OH!! that is right! I have been using breakout boards that already have the pullup resistor on them The HMC6343 doesn't have them after inspecting the board.
Thank you so much I will report back after I try your suggestions.
 
Yep, That was the problem. Screen Shot 2014-04-15 at 12.37.50 AM.jpg
You can see it in sparkfun's schematic that the resistor are not populated.
Thank you Paul, you seem to have all the answers. Maybe the hitchhiker's guide to the galaxy should have looked for you.
 
Sorry to bring this thread back, but After adding the resistors to the board. I'm now seeing very estrange outputs by Teensy compared to the ones output by the arduino.
Below is the output from arduino

Accelerometer values:
x: 1.50 y: 168.20 z: -845.10

Magnetometer values:
x: 153.70 y: 418.00 z: -406.30

Heading values:
Heading: 65.10 Roll: 0.00 Pitch: -11.20

and here it is the output from Teensy.

Accelerometer values:
x: 6430.70 y: 140.70 z: 5708.70

Magnetometer values:
x: 316.40 y: 6509.30 z: 5741.60

Heading values:
Heading: 337.40 Roll: 8.10 Pitch: 6544.20


Any ideas?
 
I think it has to do with the poorly executed C line as Paul mentioned

Edit2

Yes, that was the issue.
 
Status
Not open for further replies.
Back
Top