Chat freely about anything...
User avatar
By batstru
#72330 Hi all,

some time ago I posted about an issue I'm having with a i2c mag sensor... unfortunately the issue is still there, and I'm a kind of lost. I'm looking for an advice on what do to in order to understand what's wrong.

So...
1) I have connected the sensor, and I can detect it with the i2c scanner.
2) I have replaced wires with new ones
3) I have replaced the sensor itself and the esp01.
4) I have also removed all the libraries from arduino ide, and installed only the minimum (Adafruit_Unified_Sensor and Adafruit_HMC5883_Unified), with the latest version.

if I deploy the example from the library (that I'm attaching below here), I get the following output.

Code: Select all<⸮⸮⸮⸮⸮⸮⸮
⸮⸮⸮jT'⸮&HSX⸮⸮׫⸮W⸮.$UY.]C⸮C⸮KKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKKC⸮⸮Yk⸮K⸮B⸮5⸮⸮šj⸮DJ⸮ٕɁ⸮⸮⸮⸮⸮j⸮T⸮++WW⸮)Q'HH⸮⸮12345
Max Value:    800.00 uT
Min Value:    -800.00 uT
Resolution:   0.20 uT
------------------------------------


I get this dirty output also after a cleanup of the com buffer, and a reset of the esp01. Basically it always comes exactly like this.

In the past I was able to make it working, and now... it doesn't work anymore. I'm lost...
I'm really looking for an advice, I don't know what to check more.

I'm looking forward for any suggestion.

Code: Select all/***************************************************************************
  This is a library example for the HMC5883 magnentometer/compass

  Designed specifically to work with the Adafruit HMC5883 Breakout
  http://www.adafruit.com/products/1746
 
  *** You will also need to install the Adafruit_Sensor library! ***

  These displays use I2C to communicate, 2 pins are required to interface.

  Adafruit invests time and resources providing this open source code,
  please support Adafruit andopen-source hardware by purchasing products
  from Adafruit!

  Written by Kevin Townsend for Adafruit Industries with some heading example from
  Love Electronics (loveelectronics.co.uk)
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the version 3 GNU General Public License as
 published by the Free Software Foundation.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

 ***************************************************************************/

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>

/* Assign a unique ID to this sensor at the same time */
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("");
 
  /* Initialise the sensor */
  if(!mag.begin())
  {
    /* There was a problem detecting the HMC5883 ... check your connections */
    Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
    while(1);
  }
 
  /* Display some basic information on this sensor */
  displaySensorDetails();
}

void loop(void)
{
  /* Get a new sensor event */
  sensors_event_t event;
  mag.getEvent(&event);
 
  /* Display the results (magnetic vector values are in micro-Tesla (uT)) */
  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");

  // Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
  // Calculate heading when the magnetometer is level, then correct for signs of axis.
  float heading = atan2(event.magnetic.y, event.magnetic.x);
 
  // Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
  // Find yours here: http://www.magnetic-declination.com/
  // Mine is: -13* 2' W, which is ~13 Degrees, or (which we need) 0.22 radians
  // If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
  float declinationAngle = 0.22;
  heading += declinationAngle;
 
  // Correct for when signs are reversed.
  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 (degrees): "); Serial.println(headingDegrees);
 
  delay(500);
}
User avatar
By philbowles
#72343 OK. I'm not sure if this is anything to do with the sensor at all, but since you havent described or shown your wiring, it's just a guess.

It's a timing problem - something is interfering with or corrupting the serial output.
Look at the "gibberish" - the line of KKKK etc - there are 36 of them - the code shows you'd be expecting 36 "-" characters. Now look at the ascii values of each: "-" is 00101101 so a string of them would look like: 00101101001011010010110100101101 etc. K is 01001011 - 36 of them would start: 010010110100101101001011. Now look at the patterns:

"-" =00101101001011010010110100101101
"K"=xxxxxx010010110100101101001011 shifted by six characters....its the same

also the last part of the "junk" is the serial number etc...

so somehow you are losing 6 characters (48bits) of serial output somewhere...somehow. We need to see your wiring and/or power supply etc

First of all, change the Serial speed to something a little more "modern" - make it 74880 as then you will see the ESP8266 boot messages too. It also changes the timing, so we can see if the dodgy output changes too...

THEN

Put a tiny delay() here - to give some "settling time"

Code: Select allmag.getSensor(&sensor);
delay(???);
Serial.println("------------------------------------");


I'm rubbish at maths and lazy, but the ??? needs to be AT LEAST what 48bits @ 9600 baud takes...someone do the maths for me would you?

Either way keep the value as small as possible or you may cause other problems...
try several different values, e.g. start at 500 and then try 250 or 125 etc and go down as low as you can

AND show us your wiring.

The code says 2 pins are required for the I2C - which are you using? What does Adafruit_HMC5883_Unified default to?
.

The more I think about this, the more I think its a wiring problem! Where does your sketch identify the pins that are used by the device? There aint many on an ESP01..your SDA / SCL are going to have to be on GPIO / 2 somehow - How does the Adafruit library know that?
User avatar
By batstru
#72356
philbowles wrote:Don't you need Wire.begin(0, 2); somewhere? ARe you sure you posted the actual code yopu are using?


Yes, the code comes from the example in the library: you can take a look by yourself... just add the library Adafruit_HMC5883_Unified.
The Wire.begin(0, 2); doesn't change the behaviour: I have posted the simplest code I could post, coming from an example, not my actual code. With the example I can reproduce the issue, so that's it.

Regards