I am trying to use the GY-271 with a HiLetgo ESP-WROOM-32 ESP32 and it is not showing heading correctly. Always show something like 96.2 or 54.6.
Code used for the ESP32(Same code used for arduino mega but SCL/SDA pins changed)
/*
*
* Compass.ino - Example sketch for reading a heading from an HMC5883L triple axis magnetometer.
*
* GY-273 Compass Module -> Arduino
* VCC -> VCC (See Note Below)
* GND -> GND
* SCL -> A5/SCL, (Use Pin 21 on the Arduino Mega)
* SDA -> A4/SDA, (Use Pin 20 on the Arduino Mega)
* DRDY -> Not Connected (in this example)
*
* Voltage Note
* ~~~~~~~~~~~~
* The GY-273 Board has a 3v3 Regulator on it, and the SDA/SCL are pulled up to that so it is OK to
* use with 5v Arduino's.
*
* If you are using any other breakout, or the raw IC, you need to be using 3v3 to supply and signal!
*
* Datasheet: http://goo.gl/w1criV
*
* Copyright (C) 2014 James Sleeman
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author James Sleeman, http://sparks.gogo.co.nz/
* @license MIT License
*
*/
#include <Arduino.h>
#include <Wire.h>
#include <HMC5883L_Simple.h>
#define I2C_SDA 21
#define I2C_SCL 22
// Create a compass
HMC5883L_Simple Compass;
void setup()
{
Serial.begin(9600);
Wire.begin(I2C_SDA, I2C_SCL);
//setup compass
Compass.SetDeclination(-7, 35, 'W');
Compass.SetSamplingMode(COMPASS_SINGLE);
Compass.SetScale(COMPASS_SCALE_130);
Compass.SetOrientation(COMPASS_HORIZONTAL_X_NORTH);
}
// Our main program loop.
void loop()
{
float heading = Compass.GetHeadingDegrees();
Serial.print("Heading: \t");
Serial.println( heading );
delay(1000);
}
It does work with an Arduino Mega but not on the ESP32. It is also strange it says it is a QMC5883L but has the HMC5883L address of 0x1E and works on Mega with HMC5883L_Simple.h Library.
Link to GY-271 sensor
Link to ESP32