Magnetometer MicroMag 3 on Arduino Due


hi

i'm trying magnetometer working on due, i'm getting output don't think correct. x y , z axis numbers fluctuate when compass isn't moving. i'm beginning believe magnetometer garbage though expensive!

here have far:

code: [select]
//initialise analog pins 0
int an1, an2, an3 = 0;

void setup() {
//open serial @ 19200 baud
  serial.begin(19200);
}

void loop() {
read_analog_pins();
}

void read_analog_pins() {

  an1 = analogread(0);
  delay(5);

  an2 = analogread(1);
  delay(5);

  an3 = analogread(2);
  delay(5);

  serial.print("x = ");
  serial.print(an1,dec);
  delay(500);

  serial.print(" ");
  serial.print("y = ");
  serial.print(an2,dec);
  delay(500);

  serial.print(" ");
  serial.print("z = ");
  serial.println(an3,dec);
  delay(500);
}


so started google searching , found this:

http://wiring.org.co/learning/basics/magneticfield.html

but doesn't work. when run code uploads fine (you have declare int wled = 1; , add #include <arduino.h> in order run in arduino ide) stuck here guys, appreciated

so found other sketch looks work i'm getting incorrect output of -1,-1,-1 idk i'm doing wrong? maybe connections??

http://cwwang.com/2008/02/20/micromag-3-axis/

code: [select]
//heading calcuations added daniel's processing code heading value of 0-3600 in arduino
//edited che-wei wang
//tested on micromag 3-axis v1.00
//clockwise rotation positive

//original arduino code controlling micromag3 magnetometer through spi
//by daniel soltis

#define sclk 7 //the clock pulse (#1 on breakout)
#define miso 6 //master in, slave out (#2 on breakout)
#define mosi 5 //master out, slave in (#3 on breakout)
#define ssnot 4 //when low, device enabled (#4 on breakout)
#define drdy 3 //this low after reset, high when data ready (#5 on breakout)
#define reset 2 //this needs toggled low-high-low before each measurement (#6 on breakout)

#include <math.h>

int x = 0;
int y = 0;
int z = 0;
int heading = 0;

float readaxis(int _axis);
int getheading(float x, float y, float z);


void setup(){
  serial.begin(9600);

  pinmode(ssnot, output);
  pinmode(reset, output);
  pinmode(mosi, output);
  pinmode(miso, input);
  pinmode(drdy, input);
  pinmode(sclk, output);

  //i ground line, @ least marks as
  //something 1 change if needed
  digitalwrite(ssnot, low);
}

void loop(){

  x=readaxis(0);
  y=readaxis(1);
  z=readaxis(2);
  getheading(x,y,z);

}

void send_bit(int _high){

  //this sends bit on rising edge of clock

  digitalwrite(mosi, _high);
  delay(2);
  digitalwrite(sclk, high);
  delay(2);
  digitalwrite(sclk, low);
  delay(2);
}

int receive_bit(){

  //this receives data on falling edge of clock

  digitalwrite(sclk, high);
  delay(2);
  int bit = digitalread(miso);
  delay(2);
  digitalwrite(sclk, low);
  delay(2);
  return bit;
}

float readaxis(int _axis){
  //this function sends 8 bits, waits until data ready
  //and receives 16 bits

    //pulse reset
  digitalwrite(reset, low);
  delay(2);
  digitalwrite(reset, high);
  delay(2);
  digitalwrite(reset, low);
  delay(2);

  //send command byte

  //this sends data not in debug mode
  //and sets amount of time read magnetic sensors (the asic period)
  //as /2048
  send_bit(low);
  send_bit(high);
  send_bit(high);
  send_bit(low);
  send_bit(low);
  send_bit(low);

  //the last 2 bits select axis
  if (_axis == 0){ //x axis
    send_bit(low);
    send_bit(high);
  }
  else if (_axis == 1){ //y axis
    send_bit(high);
    send_bit(low);
  }
  else{ //z axis
    send_bit(high);
    send_bit(high);
  }

  //wait until drdy line high
  while (digitalread(drdy) == low){
    //wait
    //serial.println("waiting");
  }
  //serial.println("data!");

  long runningtotal = 0;

  //receive results , tally them come in

  //the leftmost bit signs number positive or negative
  long sign = receive_bit();

  //the remaining bits need translated individual bits integer
  (int = 14; >= 0; = - 1){
    long thisbit = receive_bit();
    thisbit = thisbit << i;
    runningtotal = runningtotal | thisbit;
  }

  if (sign == 1){
    runningtotal = runningtotal - 32768;
  }

  //this sends data on serial line, string separated commas
  if (_axis == 0){
    x = runningtotal;
    serial.print(x,dec);
    serial.write(44);
  }
  else if (_axis == 1){
    y = runningtotal;
    serial.print(y,dec);
    serial.write(44);
  }
  else{
    z = runningtotal;
    serial.print(z,dec);
    serial.write(44);
  }
  return runningtotal;
}

int getheading(float x, float y, float z){
  int heading=0;
  if (x==0 && y<0) heading=900.0;
  if(x==0 && y>0) heading=2700.0;
  if (x < 0) heading = (pi - atan(y/x))/two_pi*3600.0;
  if (x > 0 && y < 0) heading = (-1*atan(y/x))/two_pi*3600.0;
  if (x > 0 && y > 0) heading = (2.0*pi - atan(y/x))/two_pi*3600.0;
  serial.print("heading: ");
  serial.println(int(heading));
  return heading;
}



but output i'm getting is:

code: [select]
connected /dev/tty.usbmodem1421 @ 9600
0,-1,-1,heading: 900
-1,-1,-1,heading: 1350
-1,-1,-1,heading: 1350
-1,-1,-1,heading: 1350



i have wired compass due stated here except vdd connected 3.3 not 5v on due.

http://ardadv.blogspot.com/2012/03/using-micromag3-with-arduino-and-jump.html


i appreciate feedback or suggestions



Arduino Forum > Products > Arduino Due (Moderator: fabioc84) > Magnetometer MicroMag 3 on Arduino Due


arduino

Comments

Popular posts from this blog

VIDIOC_S_FMT error 16, Device or resource busy - Raspberry Pi Forums

using a laptop skeleton to build a pi laptop - Raspberry Pi Forums

Forum for Joomla? - Joomla! Forum - community, help and support