สร้างเว็บEngine by iGetWeb.com
Cart รายการสินค้า (0)

arduino ติดต่อ compass

arduino ติดต่อ compass



arduino ติดต่อ compass

HMC5883L  เบอร์ที่เลือกใช้ในบทความนี้


เข็มทิศ คือเครื่องมือสำหรับใช้หาทิศทาง มีเข็มแม่เหล็กที่แกว่งไกวได้อิสระในแนวนอนทอดตัวในแนวเหนือ-ใต้ ตามแรงดึงดูดของแม่เหล็กโลก และที่หน้าปัดมีส่วนแบ่งสำหรับหาทิศทางโดยรอบ เข็มทิศจึงมีปลายชี้ไปทางทิศเหนือเสมอ (อักษร N หรือ น) เมื่อทราบทิศเหนือแล้วก็ย่อมหาทิศอื่นได้โดยหันหน้าไปทางทิศเหนือ ด้านขวามือเป็นทิศตะวันออก ด้านซ้ายมือเป็นทิศตะวันตก ด้านหลังเป็นทิศใต้ การบอกทิศทางในแผนที่โดยทั่วไป คือการบอกเป็นทิศที่สำคัญ 4 ทิศ คือทิศเหนือ ทิศใต้ ทิศตะวันออก และทิศตะวันตก หรืออาจจะบอกละเอียดเป็น 8,16 หรือ 32 ทิศก็ได้



module compass นั้นสามารถใช้ arduino อ่านค่าจากตัวโมดูลแล้วนำค่าที่ได้นั้นมาใช้หรือประยุกต์ให้เข้ากับงานของเรา

ขาที่ใช้ต่อระหว่าง arduino กับ compass คือ

หรือต่อตามภาพก้ได้ครับ

Arduino GND ---------> HMC5883L GND

Arduino 3.3V ---------> HMC5883L VCC

Arduino A4 (SDA) ---> HMC5883L SDA

Arduino A5 (SCL) ---> HMC5883L SCL

ต้องไปหา load #include <HMC5883L.h>  มาใส่ใน libraryด้วยนะครับ

// code program

// 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;

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

Serial.println("Starting the I2C interface.");
Wire.begin(); // Start the I2C interface.

Serial.println("Constructing new HMC5883L");
compass = HMC5883L(); // Construct a new HMC5883 compass.

Serial.println("Setting scale to +/- 1.3 Ga");
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));

Serial.println("Setting measurement mode to continous.");
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));
}

// 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);


}

// 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(" \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");
}





ดูค่า output ทาง serial monitor ครับแล้วลองหมุนตัว compass ดูนะครับก็จะเห็นค่ามุมที่มีการเปลี่ยนแปลง




view