برای راه اندازی این ماژول و هر چهار سنسور اون فقط کافیه 5 سیم اول رو طبق عکس به آردوینو متصل کنید و هیچ نیازی به وصل کردن سیم های دیگه نیست
برای راه اندازی سنسور HMC5883L یا همون قطب نمای خودمون باید فایل HMC5883L.zip رو دانلود کنید و بعد از اکسترکت کردن اون رو تو پوشه 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() { 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) error = compass.SetMeasurementMode(Measurement_Continuous); // Set the measurement mode to Continuous if(error != 0) Serial.println(compass.GetErrorText(error)); } void loop() { MagnetometerRaw raw = compass.ReadRawAxis(); 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; 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; Serial.print("Heading: "); Serial.print(heading); Serial.print(" Radians "); Serial.print(headingDegrees); Serial.println(" Degrees"); }


23 لایک
LinkBack URL
About LinkBacks



پاسخ با نقل قول