GY-271 compass module with an ESP8266-01S

I am a newbie and wonder if anyone can advise me please?

I am interfacing a GY-271 compass module compass to an ESP8266-01S and having problems getting any usable output.

I have several compass modules which work brilliantly with an ESP32. However when used with an ESP8266-01S the data output is either fixed values or small variation. i.e Azimuth varies only 20° or so in a whole circle - almost as if it cannot really see the compass module on the I2C bus...

By interchanging the compass modules with the working ESP32 I can rule out their failure.

I am using a very simple sketch with the QMC5883L Compass library included which works nicely for the ESP32. For the ESP8266-01S I have also included the Wire library and added;

“Wire.begin(0, 2);” to assign SDA to GPIO0 SCL to GPIO2
before
“compass.init();”

Thank you for taking the time to read this, any guidance would be greatly appreciated!

These are the limitations of the IO-pins of an ESP8266-01

you can read more about it here

you are trying to use I2C

I2C is I2C regardless of the I2C-device. So why did you not simply quogle with terms like
I2C

which finds links like these?

best regards Stefan

Thanks for that, Stefan.

I must admit that I have "Googled to death" over the past 4 now 5 days for, as a courtesy, I always prefer to make as much effort as possible to find the answers myself and not just rely on others to do it for me! In fact I had already discovered these sites and can confirm that I am using the correct pins on the ESP8266-01S

The GY-271 compass module [ also known colloquially as HMC5883L, but is in fact a QMC5883L - made under licence ] works 'out of the box' with the ESP32's I2C interface.

The library is "QMC5883L Compass Arduino Library by MPrograms" as recommended by the module supplier.

There is output from the ESP8266-01S but it is usually either zeros or fixed x,y,z values.

Running an I2C scan reports "No I2C devices found" - which has been reported by several others, but with no useful suggestions. The only one so far was to set "Wire.begin(0,2);"

Has anyone had success with this module and an ESP8266-01S - especially making it 'appear' on the I2C bus?

Then something fundamentally is wrong.
Post the I2C-scanner-code that you used.
Ifthis code uses just wrire.begin() without specifying the SCL/SDA-pin in the right way even the I2C-scanner can't find anything.
wire.begin() takes the default io-pins which are surely not IO-pin 0 and IO-pin 2

Thanks for that. I must admit to a schoolboy error by not clarifying Wire.begin.

I can now confirm that setting Wire.begin(0,2); enabled the I2C scanner to detect the module at 0x0D There is some limited output, as before.

Should I use 4K7 pull-ups on the Clock and Data pins or do I have to tell the ESP8266-01S to specifically 'see' 0x0D?

what does this say?

What does this project show about connecting a I2C-device to the ESP8266?

How do you make a microcontroller "see" a certain adress??

You can imagine the I2C-adress just the same way as a postal adress.
How does postal adressing work?
Does the postman "watch out" to "see" a certain adress??

Yes I can see it says 'pulled up' but there are many suggestions to add pull up resistors, as seen in your

However, you will note this example pulls the data and clock pins on the ESP2866 up to 5 volts which is not good practice when interfacing 3v3 and 5 volt devices.

I am trying to seek some simple advice not clever riddles. Instead of

"You can imagine the I2C-adress just the same way as a postal adress.
How does postal adressing work?
Does the postman "watch out" to "see" a certain adress??"

What does this mean? Is it "Yes you must tell the ESP what the address code is" or "The ESP doesn't need to know, it will just find it"

Thank you for your time Stefan. We appear to be getting some results over I2C but not quite there yet.

...............................................

Perhaps someone with experience of interfacing the GY-271 compass module can assist? I am totally stuck to understand why the devices work so easily with the ESP32 but not the ESP8266-01. Why am I getting a variation of just 40-50° when rotating the sensor through 360°, yet on the ESP with the same modules, I am getting the full 360° resolution - with the same software?

Magnetometers don't work "out of the box" as compasses, and must be calibrated before use.

Quick and dirty approach: How to Calibrate a Magnetometer?

Advanced tutorial: Tutorial: How to calibrate a compass (and accelerometer) with Arduino | Underwater Arduino Data Loggers

Practical example with Arduino: https://forum.pololu.com/t/correcting-the-balboa-magnetometer/14315

Sincere thanks for that. My comment 'out-of-the-box' was a casual reference to getting the device up and running, just to see some recognisable output before the serious work began to render it usable.

I was aware there is a serious need to calibrate and also add some compensation processing for tilting out of the horizontal plane, which I have been doing with the same magnetometer modules connected to an ESP32. Your suggested links have helped greatly - for the ESP32 and hopefully soon the ESP8266.....

The problem I have with the ESP8266-01S is that the output so far is unusable, inasmuch that it is a very narrow band of values when rotated around 360° - almost as if the 8266 isn't really getting any valid data from the module. At present the prospect of calibration and correction would be a total luxury!

The code below is used for both ESP32 and the ESP8266-01S, the only difference being the inclusion of the Wire library and the single line in the Setup section [ Wire.begin etc] when used with the 8266.

/*
===============================================================================
QMC5883LCompass.h Library Azimuth Example Sketch
Learn more at [https://github.com/mprograms/QMC5883Compas]
===============================================================================
v0.3 - June 12, 2019
Written by MRPrograms 
Github: [https://github.com/mprograms/]

............

*/
#include <QMC5883LCompass.h>
#include <Wire.h>

QMC5883LCompass compass;

void setup() {
  Serial.begin(9600);

// Added for ESP8266-01S   - not required for ESP32
  Wire.begin(0, 2);                  // sda=gpio0 scl=gpio2;  

  compass.init();
  
}

void loop() {
  int a;
  
  // Read compass values
  compass.read();

  // Return Azimuth reading
  a = compass.getAzimuth();
  
  Serial.print("A: ");
  Serial.print(a);
  Serial.println();
  
  delay(250);
}

Is there something missing in the code which should tell the 8266 to look specifically at 0x0D to read the incoming data?

My project is really looking for a very physically small device, hence the hope that the ESP8266 will work.

Many thanks again!

There is a big difference between the ESP32 and the ESP8266-01S

The 01S has a external pull-up on GPIO0 and an LED connected to GPIO2, the ESP32 has nothing connected.

GPIO2 will not work well as a clock.

Set GPIO0 as SCL and GPIO2 as SDA.
Do not put a pull-p on SCL but put a 10K pull-up on SDA.

1 Like

If you have code for the EPS32 that is working. The exact same lines of code will work for the ESP8266 if the electronically conditions follow the generally valid specifications for I2C-bus-devices.

What so riddley about imagine how to send a letter?
You write the adress onto the letter so the postman has an information into which postbox he must put the letter.

Same thing with I2C-devices: You specify an I2C-adress and send the data on the I2C-bus with this adress. The adress will make that I2C-device react that has this adress that you specified.

If your I2C-code for the ESP32 did specifiy an adress you use the same code
It might be that the library does this internally with an default-adress.
If the documenation of the library is good the documentation explains this
if the documenation is small you will have to look up this inside the library

best regards Stefan

1 Like

Sorry, I thought you had solved the I2C problem. If you don't see the expected address using the I2C address scanner, then communications aren't working and you will get nowhere. That has to be fixed before you can proceed.

Three possible problems come to mind:

  1. wrong or missing pullups on the I2C lines
  2. connecting a 5V I2C bus to a 3.3V I2C bus without using logic level shifters.
  3. wrong I2C library or hardware: with some processors you need to use the Wire2 library and a different set of I2C pinouts.
1 Like

Thank you all for your contributions, they have helped enormously.

For some inexplicable reason, when used with the ESP32, the original output was pretty close to Reality and calibration made it even better, whereas with the ESP8266-01S, calibration was absolutely vital to enable any sense to be made out of the limited range of values.


For the benefit of anyone straying upon this dialogue, the solution was;

ESP8266-01S Vcc connected to GY-271 compass module Vcc [ both 3V3 units ]
ESP8266-01S Gnd connected to GY-271 compass module Gnd

ESP8266-01S GPIO0 linked to GY-271 SCL connection
ESP8266-01S GPIO2 linked to GY-271 SDA connection and pulled high with 10k pull-up to Vcc

Library used for GY-271 QMC5883LCompass

All usage required

#include <QMC5883LCompass.h>

 // required
#include <Wire.h>                             

QMC5883LCompass compass;

void setup() {
  Serial.begin(9600);

 // required to assign SDA and SCL to ESP8266 pins GPIO2 and GPIO0 respectively
  Wire.begin(2,0);
 // required set to I2C address 
  compass.setADDR(0x0D);
  
compass.init();

The output is very strange until running the calibration sketch included in the library examples. This will provide a rudimentary correction which at leasts normalises the Azimuth output and makes the 360° compass rose appear a little more credible.

The correction is added after initialising the compass, ie

#include <QMC5883LCompass.h>
#include <Wire.h>

QMC5883LCompass compass;

void setup() {
  Serial.begin(9600);
  Wire.begin(2,0);
  compass.setADDR(0x0D);
  compass.init();
  compass.setCalibration(-1491, 1242, -2107, 556, -2270, 107);      
}

The values above are included as an example. The User must create their own set.

There is more work to be done, calibrating and correcting for roll and pitch effects.

I hope this helps.

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