Adafruit_HMC5883_Unified requestFrom Example code Error

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.

Umm, anyone?

I have exactly the same problem ! I managed to make the error messages disappear by choosing Adafruit HMC5883 Unified from the list of libraries (Sketch/Include library menu), but that doesn't work either. The serial monitor only displays "HMC5883 Magnetometer Test" and that's it.
I have double check the wiring (very simple), no error possible.
I have a question : where is it written in the code that we use the A4 and A5 ports ? Does that mean that we can't use other analogic ports ?

I have tried with a new Arduino R4 Wifi and it seems to work,I get the following result: Magnetometer OK
22:56:55.075 -> ------------------------------------
22:56:55.075 -> Sensor: HMC5883
22:56:55.075 -> Driver Ver: 1
22:56:55.200 -> Unique ID: 12345
22:56:55.200 -> Max Value: 800.00 uT
22:56:55.200 -> Min Value: -800.00 uT
22:56:55.200 -> Resolution: 0.20 uT
22:56:55.200 -> ------------------------------------
22:56:55.200 ->
22:56:55.807 -> X: -0.09 Y: -0.09 Z: -0.10 uT
22:56:55.807 -> Heading (degrees): 237.61
22:56:56.398 -> X: -0.09 Y: -0.09 Z: -0.10 uT
22:56:56.398 -> Heading (degrees): 237.61
22:56:56.986 -> X: -0.09 Y: -0.09 Z: -0.10 uT
22:56:56.986 -> Heading (degrees): 237.61

But the heading does not change at all when I rotate the chip.
Even stranger : if I remove the component from the breadboard, I get the same result ! Exactly as if the R4 Wifi had already a magnetometer included, but a blocked one to 237.61 !
I do not understand anything anymore !
Please help me!

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.