Go Down

Topic: MAG3110 magnetometer I2C problem (Read 1 time) previous topic - next topic

alkopop79

I'm having trouble setting up this magnetometer:

https://www.sparkfun.com/products/10619

I checked the wiring thoroughly and even made sure the ID matches the one in the datasheet. Yet I can't see anything happening on the serial monitor. Interestingly, when I remove the clock wire, occasionally the Arduino starts spitting out data (the three axes). Is it possible that the clock signal is skewed? This is the sketch provided by Sparkfun:

Code: [Select]
/*
  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
  config();            // turn the MAG3110 on
}

void loop()
{
  print_values();
  delay(5);
}

void config(void)
{
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.send(0x11);              // cntrl register2
  Wire.send(0x80);              // send 0x80, enable auto resets
  Wire.endTransmission();       // stop transmitting
 
  delay(15);
 
  Wire.beginTransmission(MAG_ADDR); // transmit to device 0x0E
  Wire.send(0x10);              // cntrl register1
  Wire.send(1);                 // send 0x01, active mode
  Wire.endTransmission();       // stop transmitting
}

void print_values(void)
{
  Serial.print("x=");
  Serial.print(readx());
  Serial.print(","); 
  Serial.print("y=");   
  Serial.print(ready());
  Serial.print(",");       
  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.send(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.receive(); // 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.send(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.receive(); // 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.send(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.receive(); // 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.send(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.receive(); // 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.send(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.receive(); // 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.send(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.receive(); // receive the byte
  }
 
  int zout = (zl|(zh << 8)); //concatenate the MSB and LSB
  return zout;
}

HaloCamper

#1
Jan 31, 2013, 08:10 pm Last Edit: Jan 31, 2013, 08:17 pm by HaloCamper Reason: 1
Do u have Pull-Ups on SCL and SDA (from 100k  to 1k Ohm)?

If you are not sure if the Address you use is right use http://todbot.com/blog/2009/11/29/i2cscanner-pde-arduino-as-i2c-bus-scanner/comment-page-1/ to look for is.

If you think the code isn't working as it should throw some Serial.println s in.

Go Up