2015年9月4日 星期五

Arduino + HMC5883 三軸磁場感測器

Code :
HMC5883 / Arduino :

Vcc : 5v
GND : GND
SCL : 5A
SDA : 4A
DRDY : N/A 

LED / Arduino:
LED_1:  10
LED_2 : 11
LED_3 : 12
LED_4 : 13

HMC5883  類別下載:

// Reference the I2C Library
#include <Wire.h>
// Reference the HMC5883L Compass Library
#include <HMC5883L.h>

// Store our compass as a variable.
HMC5883L compass;
// Record any errors that may occur in the compass.
int error = 0;
int LedCode = 12, LedGreen = 13, LedCode3 = 11, LedCode4 = 10;

// Out setup routine, here we will configure the microcontroller and compass.
void setup()
{
  // Initialize the serial port.
  Serial.begin(9600);
  
  Wire.begin(); // Start the I2C interface.

  compass = HMC5883L(); // Construct a new HMC5883 compass.
    
  error = compass.SetScale(1.3); // Set the scale of the compass.
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.GetErrorText(error));
    
  error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous
  if(error != 0) // If there is an error, print it out.
    Serial.println(compass.GetErrorText(error));
    
    pinMode(LedCode, OUTPUT); 
    pinMode(LedGreen, OUTPUT);
    pinMode(LedCode3, OUTPUT);
    pinMode(LedCode4, OUTPUT);
}

// Our main program loop.
void loop()
{
  // Retrive the raw values from the compass (not scaled).
  MagnetometerRaw raw = compass.ReadRawAxis();
  // Retrived the scaled values from the compass (scaled to the configured scale).
  MagnetometerScaled scaled = compass.ReadScaledAxis();
  
  // Values are accessed like so:
  int MilliGauss_OnThe_XAxis = scaled.XAxis;// (or YAxis, or ZAxis)

  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading = atan2(scaled.YAxis, scaled.XAxis);
  
  float declinationAngle = 0.0457;
  heading += declinationAngle;
  
  // Correct for when signs are reversed.
  if(heading < 0)
    heading += 2*PI;
    
  // Check for wrap due to addition of declination.
  if(heading > 2*PI)
    heading -= 2*PI;
   
  // Convert radians to degrees for readability.
  float headingDegrees = heading * 180/M_PI; 

  // Output the data via the serial port.
  Output(raw, scaled, heading, headingDegrees);
  LedDate(raw);
  LedDatefalse(raw);
}

void LedDate(MagnetometerRaw raw){
    if(raw.XAxis >= 25 || raw.YAxis >= 25 || raw.ZAxis >= 25){
     digitalWrite(LedGreen,HIGH);
   }else{
       digitalWrite(LedGreen,LOW);
   }

    if(raw.XAxis >= 100 || raw.YAxis >= 100 || raw.ZAxis >= 100){
        digitalWrite(LedCode,HIGH);
    }else{
      digitalWrite(LedCode,LOW);
    }
    
    if(raw.XAxis >= 200 || raw.YAxis >= 200 || raw.ZAxis >= 200){
      digitalWrite(LedCode3,HIGH);
    }else{
      digitalWrite(LedCode3,LOW);
    }
    
    if(raw.XAxis >= 400 || raw.YAxis >= 400 || raw.ZAxis >= 400){
      digitalWrite(LedCode4,HIGH);
    }else{
      digitalWrite(LedCode4,LOW);
    }
    
}

void LedDatefalse(MagnetometerRaw raw){
    if(raw.XAxis <= -25 || raw.YAxis <= -25 || raw.ZAxis <= -25){
      digitalWrite(LedGreen,HIGH);
   }else{
       digitalWrite(LedGreen,LOW);
   }
   if(raw.XAxis <= -100 || raw.YAxis <= -100 || raw.ZAxis <= -100){
        digitalWrite(LedCode,HIGH);
     }else{
       digitalWrite(LedCode,LOW);
     }
     if(raw.XAxis <= -200 || raw.YAxis <= -200 || raw.ZAxis <= -200){
       digitalWrite(LedCode3, HIGH);
     }else{
       digitalWrite(LedCode3,LOW);
     }
     
     if(raw.XAxis <= -400 || raw.YAxis <= -400 || raw.ZAxis <= -400){
       digitalWrite(LedCode4, HIGH);
     }else{
       digitalWrite(LedCode4,LOW);
     }
}

// Output the data down the serial port.
void Output(MagnetometerRaw raw, MagnetometerScaled scaled, float heading, float headingDegrees)
{
   Serial.print("Raw: \t");
   Serial.print(raw.XAxis);
   Serial.print("   ");   
   Serial.print(raw.YAxis);
   Serial.print("   ");   
   Serial.print(raw.ZAxis);
   Serial.print("\t\n");
   Serial.print("   \tScaled:\t");
   
   Serial.print(scaled.XAxis);
   Serial.print("   ");   
   Serial.print(scaled.YAxis);
   Serial.print("   ");   
   Serial.print(scaled.ZAxis);

   Serial.print("   \tHeading:\t");
   Serial.print(heading);
   Serial.print(" Radians   \t");
   Serial.print(headingDegrees);
   Serial.println(" Degrees   \t");
}

HMC5883 零件圖:

沒有留言:

張貼留言