Arduino board: Leonardo
IDE: v1.0.1
I2C hardware: MAG3110 magnetometer (Triple Axis Magnetometer Breakout - MAG3110 - SEN-10619 - SparkFun Electronics)
Trying to set up the I2C sensor to take magnetic field data, but debugging is showing that Wire.endTransmission(); hangs the sketch (debug code below). Looking around online, I found others are having this problem as well:
- Similar but his at least runs for a while: http://arduino.cc/forum/index.php/topic,117452.0.html
- Basically the same issue: http://letsmakerobots.com/node/28941
Output always makes it to the line right before the first call to Wire.endTransmission(); in the config() function - In particular the output looks like:
Serial started.
Made it to the config fn.
Made it to the first CtrlReg.
Addressed CtrlReg1.
Enabled ACTIVE mode.
Based on internet research, it sounds like there's either an obscure problem with the Wire library or I'm just not communicating with my I2C device (which sounds more likely). I've quadruple-checked my wiring and everything looks correct. [I've tried using both the Leonardo's dedicated SCL/SDA pins and D2/D3, but there's no difference, not surprisingly.]
Any thoughts on how to debug further? I'm at a bit of a loss, I'll admit. Is it possible that my magnetometer is just fried? (Unfortunately, I don't have another arduino to test it on. )
Any suggestions welcomed.
/*
MAG3110 Breakout Example Code
by: Aaron Weiss, aaron at sparkfun dot com
SparkFun Electronics 2011
date: 9/6/11
license: beerware, if you use this code and happen to meet me, you
can by me a beer
The code reads the raw 16-bit x, y, and z values and prints them
out. This sketch does not use the INT1 pin, nor does it poll for
new data.
*/
#include <Wire.h>
#define MAG_ADDR 0x0E //7-bit address for the MAG3110, doesn't change
void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial for output
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Serial started.");
config(); // turn the MAG3110 on
Serial.println("Magnetometer config done.");
}
void loop()
{
print_values();
delay(100);
}
void config(void)
{
Serial.println("Made it to the config fn.");
int errorcode;
Serial.println("Made it to the first CtrlReg.");
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x10); // cntrl register1
Serial.println("Addressed CtrlReg1.");
Wire.write(0x1); // send 0x01, active mode
Serial.println("Enabled ACTIVE mode.");
errorcode = Wire.endTransmission(); // stop transmitting
delay(1000);
Serial.print("Error code:");
Serial.println(errorcode);
Serial.println("Ended transmission to CtrlReg1.");
delay(15);
Serial.println("Made it to the second CtrlReg.");
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x11); // cntrl register2
Serial.println("Addressed CtrlReg2.");
Wire.write(0x10); // send 0x10, enable auto resets
Serial.println("Enabled auto-reset.");
errorcode = Wire.endTransmission(); // stop transmitting
delay(1000);
Serial.print("Error code: ");
Serial.println(errorcode);
Serial.println("Ended transmission to CtrlReg2.");
}
void print_values(void)
{
Serial.print("x=");
Serial.print(readx());
Serial.print(", y=");
Serial.print(ready());
Serial.print(", z=");
Serial.println(readz());
}
int readx(void)
{
int xl, xh; //define the MSB and LSB
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x01); // x MSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
xh = Wire.read(); // receive the byte
}
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x02); // x LSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
xl = Wire.read(); // receive the byte
}
int xout = (xl|(xh << 8)); //concatenate the MSB and LSB
return xout;
}
int ready(void)
{
int yl, yh; //define the MSB and LSB
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x03); // y MSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
yh = Wire.read(); // receive the byte
}
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x04); // y LSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
yl = Wire.read(); // receive the byte
}
int yout = (yl|(yh << 8)); //concatenate the MSB and LSB
return yout;
}
int readz(void)
{
int zl, zh; //define the MSB and LSB
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x05); // z MSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
zh = Wire.read(); // receive the byte
}
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
Wire.write(0x06); // z LSB reg
Wire.endTransmission(); // stop transmitting
delayMicroseconds(2); //needs at least 1.3us free time between start and stop
Wire.requestFrom(MAG_ADDR, 1); // request 1 byte
while(Wire.available()) // slave may send less than requested
{
zl = Wire.read(); // receive the byte
}
int zout = (zl|(zh << 8)); //concatenate the MSB and LSB
return zout;
}
[\code]