I just got the GY271 sensor for bot heading and trying out the Adafruit_HMC5883_Unified example code. Without making any changes. But I am getting a Error for uint8_t requestFrom(uint8_t, uint8_t, uint8_t); The sensor stats working but its not giving any correct output.
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
void displaySensorDetails(void)
{
sensor_t sensor;
mag.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" uT");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" uT");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" uT");
Serial.println("------------------------------------");
Serial.println("");
delay(500);
}
void setup(void)
{
Serial.begin(9600);
Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
if(!mag.begin())
{
Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
while(1);
}
displaySensorDetails();
}
void loop(void)
{
sensors_event_t event;
mag.getEvent(&event);
Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" ");
Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print(" ");
Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print(" ");Serial.println("uT");
float heading = atan2(event.magnetic.y, event.magnetic.x);
float declinationAngle = 0.22;
heading += declinationAngle;
if(heading < 0)
heading += 2*PI;
if(heading > 2*PI)
heading -= 2*PI;
float headingDegrees = heading * 180/M_PI;
Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
delay(500);
}
Output:
In file included from f:\Codes\Arduino\libraries\Adafruit_HMC5883_Unified\Adafruit_HMC5883_U.cpp:38:0:
C:\Users\medha\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src/Wire.h: In member function 'void Adafruit_HMC5883_Unified::read()':
C:\Users\medha\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src/Wire.h:69:13: note: candidate 1: uint8_t TwoWire::requestFrom(int, int, int)
uint8_t requestFrom(int, int, int);
^~~~~~~~~~~
C:\Users\medha\AppData\Local\Arduino15\packages\arduino\hardware\avr\1.8.6\libraries\Wire\src/Wire.h:66:13: note: candidate 2: uint8_t TwoWire::requestFrom(uint8_t, uint8_t, uint8_t)
uint8_t requestFrom(uint8_t, uint8_t, uint8_t);
^~~~~~~~~~~
Sketch uses 8398 bytes (26%) of program storage space. Maximum is 32256 bytes.
Global variables use 668 bytes (32%) of dynamic memory, leaving 1380 bytes for local variables. Maximum is 2048 bytes.