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 零件圖:

2015年9月1日 星期二

Arduino + GY-273 HMC5883L 電子指南針

GY-273 HMC5883L code:

GY-273 HMC5883L /  arduino

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

/* Include the standard Wire library */
#include <Wire.h>

/* The I2C address of the module */
#define HMC5803L_Address 0x1E

/* Register address for the X Y and Z data */
#define X 3
#define Y 7
#define Z 5

void setup()
{
  Serial.begin(9600);
  /* Initialise the Wire library */
  Wire.begin();

  /* Initialise the module */
  Init_HMC5803L();
}

void loop()
{
  /* Read each sensor axis data and output to the serial port */
  Serial.print(HMC5803L_Read(X));
  Serial.print(" ");
  Serial.print(HMC5803L_Read(Y));
  Serial.print(" ");
  Serial.println(HMC5803L_Read(Z));

  /* Wait a little before reading again */
  delay(200);
}


/* This function will initialise the module and only needs to be run once
   after the module is first powered up or reset */
void Init_HMC5803L(void)
{
  /* Set the module to 8x averaging and 15Hz measurement rate */
  Wire.beginTransmission(HMC5803L_Address);
  Wire.write(0x00);
  Wire.write(0x70);
         
  /* Set a gain of 5 */
  Wire.write(0x01);
  Wire.write(0xA0);
  Wire.endTransmission();
}


/* This function will read once from one of the 3 axis data registers
and return the 16 bit signed result. */
int HMC5803L_Read(byte Axis)
{
  int Result;

  /* Initiate a single measurement */
  Wire.beginTransmission(HMC5803L_Address);
  Wire.write(0x02);
  Wire.write(0x01);
  Wire.endTransmission();
  delay(6);

  /* Move modules the resiger pointer to one of the axis data registers */
  Wire.beginTransmission(HMC5803L_Address);
  Wire.write(Axis);
  Wire.endTransmission();
 
  /* Read the data from registers (there are two 8 bit registers for each axis) */
  Wire.requestFrom(HMC5803L_Address, 2);
  Result = Wire.read() << 8;
  Result |= Wire.read();

  return Result;
}

 GY-273 HMC5883L 零件圖:

成功圖: