Here’s some modified code from Sensor Workshop to get heading values in Arduino from the Micromag 3 axis magneto sensor. Next up, accelerometer calculations on the magneto sensor to get tilt compensation on the compass readings.
//heading calcuations added from Daniel's processing code to get a heading value of 0-3600 in Arduino
//edited by Che-Wei Wang
//tested on MICROMAG 3-AXIS V1.00
//clockwise rotation is positive
//Original Arduino code for controlling a MicroMag3 magnetometer through SPI
//by Daniel Soltis
#define SCLK 7 //the clock to 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, the device is enabled (#4 on breakout)
#define DRDY 3 //this is low after a reset, high when data is ready (#5 on breakout)
#define RESET 2 //this needs to be toggled low-high-low before each measurement (#6 on breakout)
#include
int x = 0;
int y = 0;
int z = 0;
int heading = 0;
void setup(){
Serial.begin(9600);
pinMode(SSNOT, OUTPUT);
pinMode(RESET, OUTPUT);
pinMode(MOSI, OUTPUT);
pinMode(MISO, INPUT);
pinMode(DRDY, INPUT);
pinMode(SCLK, OUTPUT);
//i could just ground this line, but this at least marks it as
//something that one could 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 the bit on the rising edge of the clock
digitalWrite(MOSI, _high);
delay(2);
digitalWrite(SCLK, HIGH);
delay(2);
digitalWrite(SCLK, LOW);
delay(2);
}
int receive_bit(){
//this receives the data on the falling edge of the 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 eight bits, waits until the data is ready
//and receives 16 bits
//pulse the reset
digitalWrite(RESET, LOW);
delay(2);
digitalWrite(RESET, HIGH);
delay(2);
digitalWrite(RESET, LOW);
delay(2);
//send the command byte
//this sends data that we are not in debug mode
//and sets the amount of time to read the 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 two bits select the 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 the drdy line is high
while (digitalRead(DRDY) == LOW){
//wait
//Serial.println("waiting");
}
//Serial.println("data!");
long runningtotal = 0;
//receive the results and tally them up as they come in
//the leftmost bit signs the number as positive or negative
long sign = receive_bit();
//the remaining bits need to be translated from individual bits into an integer
for (int i = 14; i >= 0; i = i - 1){
long thisbit = receive_bit();
thisbit = thisbit 0) heading=2700.0;
if (x 0 && y 0 && y > 0) heading = (2.0*PI - atan(y/x))/TWO_PI*3600.0;
Serial.print("heading: ");
Serial.println(int(heading));
return heading;
}