Compass project failure once disconnected from PC

My project idea was to make a simple compass for my car.
I had just gotten an OLED display and a couple of cheap HMC5883L and this seemed like something I might be able to put together.
Eventually the plan is using a nano and packaging it for permanent car installation.
But currently it is still at the breadboard stage.

My current sketch compassBeta.iso basically reads the data from the HMC5883L every 500ms and updates the OLED display. The display shows 1 of 8 possible headings such as north, northeast, east etc plus a numeric value in degrees of the heading. The code is ugly and probably inefficient but when uploaded to the Uno from the PC and left plugged into the USB it works.

#include "HCuOLED.h"
#include "SPI.h"
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>

const byte CS_DI =  10;
const byte DC_DI = 9;
const byte RST_DI = 8;

Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
HCuOLED HCuOLED(SH1106, CS_DI, DC_DI, RST_DI);

void setup()
{
  //Serial.begin(9600);
  HCuOLED.Reset();
}

void loop()
{
  sensors_event_t event;
  mag.getEvent(&event);
  float heading = atan2(event.magnetic.y, event.magnetic.x);
  float declinationAngle = 0.056;
  heading += declinationAngle;
  if (heading < 0)
    heading += 2 * PI;
  if (heading > 2 * PI)
    heading -= 2 * PI;
  float headingDegrees = heading * 180 / M_PI;
  //Serial.println(headingDegrees);
  eightPoint(headingDegrees);
}
void eightPoint(float hD) {
  HCuOLED.SetFont(LCDLarge_24pt);
  if (hD > 337 || hD  <= 22) {
    HCuOLED.Cursor(35, 10);
    HCuOLED.Print("North");
  }

  if (hD > 22 && hD <= 67) {
    HCuOLED.Cursor(2, 10);
    HCuOLED.Print("NorthEast");
  }
  if (hD > 67 && hD <= 112) {
    HCuOLED.Cursor(45, 10);
    HCuOLED.Print("East");
  }
  if (hD > 112 && hD <= 157) {
    HCuOLED.Cursor(2, 10);
    HCuOLED.Print("SouthEast");
  }
  if (hD > 157 && hD <= 202) {
    HCuOLED.Cursor(35, 10);
    HCuOLED.Print("South");
  }
  if (hD > 202 && hD <= 247) {
    HCuOLED.Cursor(2, 10);
    HCuOLED.Print("SouthWest");
  }
  if (hD > 247 && hD <= 292) {
    HCuOLED.Cursor(45, 10);
    HCuOLED.Print("West");
  }
  if (hD > 292 && hD <= 337) {
    HCuOLED.Cursor(2, 10);
    HCuOLED.Print("NorthWest");
  }
  HCuOLED.Cursor(50, 50);
  HCuOLED.SetFont(MedProp_11pt);
  HCuOLED.Print(hD);
  HCuOLED.Print("*");
  //Serial.println(hD);
  HCuOLED.Refresh();
  delay(500);
  HCuOLED.Erase(0, 0, 127, 63);
}

At this point I discovered I was not happy with the performance of the HMC5883L. East and west are not too bad. Usually only off by a couple of degrees. But north and south can vary from actual by up to 45 degrees.

This led me to want to take the prototype outside to test if all the electronics and speakers in my house could be partially responsible for the poor performance. Plus I want to see how visible the display is in direct sunlight while I’m wearing sunglasses.

So I use a Duracell 9v alkaline battery with a connector for the barrel jack on the Uno so the prototype will be portable.

Prototype displays an initial heading and then promptly stops updating.

I check connections and everything still seems to be attached correctly to the breadboard and the Uno.

I plug the Uno back into the PC. Still does not update.

I re-upload the compassBeta sketch. The display still does not update past the initial directional value.

I upload the magsensor.ino sketch which is the example provided for my HMC5883L. Serial monitor shows the heading values updating.

I upload one of the example sketches for the OLED that does an animation. OLED is working.

I upload my compassBeta sketch. It now works again.

So I disconnect my UNO from the PC and plug it into a USB powerbank.

Prototype displays an initial heading and then promptly stops updating.

I reattach Uno to PC USB. Still doesn’t update.

I re-upload my sketch. Still doesn’t update.

I upload magsensor sketch and then immediately upload compassBeta sketch and it begins to work again.

I unplug Uno from PC then plug it back in to PC.
Prototype displays an initial heading and then promptly stops updating.

I upload a blank sketch and then compassBeta sketch. OLED display does not update.

I upload magsensor sketch then upload compassBeta sketch. Prototype starts working fine again.

TLDR; My sketch only works if I first upload the magsensor sketch. I can upload any other sketch after magsensor sketch and then my sketch and my sketch runs fine until I disconnect USB. Then it will not work until magsensor sketch is uploaded again.

magsensor.ino (4.05 KB)

Frostline: So I use a Duracell 9v alkaline battery with a connector for the barrel jack on the Uno so the prototype will be portable.

This is almost certainly the problem. Those small 9v batteries are not designed to produce enough current.

Try a pack of 6 AA cells.

...R

Robin2: This is almost certainly the problem. Those small 9v batteries are not designed to produce enough current.

Try a pack of 6 AA cells.

...R

Thank you for responding.

I suspected the 9v battery might be an issue. Which is why I also tried a usb powerbank. To be more specific a RavPower RP-WD01 which outputs 5v/1A with a 3000mAh capacity. I also used the PC USB to power my prototype. The exact same USB port it had previously been working with. I thought I made that clear in my first post.

But to restate for clarity:

Condition A: Prototype attached to PC via USB with compassBeta.ino loaded and working correctly. As prototype is rotated the heading values update every 1/2 second.

Event A: Prototype removed from PC USB and then reattached to same PC USB port.

Condition B: Prototype no longer works correctly. OLED displays an initial heading and then stops updating heading. HMC5883L stops refreshing values. Serial.println functions in my code(which are commented out) will show if the value for headingDegrees changes. In condition B the values printed to serial do not change even with constant rotation of prototype. Both the reset button on the Uno or disconnecting and reconnecting the Uno to USB fail to take prototype out of condition B.

Event B: Magsensor.ino uploaded to prototype. Then compassBeta.ino is uploaded to prototype.

Prototype returns to condition A.

I would like to not have condition B and need to do event B.

Sorry if I missed stuff - it was a long post.

I don't understand the difference between "Condition A" and the situation immediately after "Event A". It seems to me the first time you run the code it is in the same situation as it will be after "Event A"

Or are you trying to say that the code only works immediately after it is uploaded ?

I guess from your picture that you have some sort of clone Uno ?

What happens in what you call "Condition A" if you press the Arduino reset button ?

...R

Robin2:
I don’t understand the difference between “Condition A” and the situation immediately after “Event A”.
It seems to me the first time you run the code it is in the same situation as it will be after “Event A”

After event A it is in condition B.

Condition A…its working
Event B… I unplug it and plug it back in

At this point it should go back to condition A, where it is working

But it doesn’t.

It goes into condition B…where it is not working.

I don’t know how to explain it any clearer.

Maybe a video…

Attempted YouTube video of prototype working then not working

It could be that you are running out of SRAM. Do you get the same effect if you modify your code to exclude the OLED display and just have the code printing out of the serial port?

Frostline: I don't know how to explain it any clearer.

I understand what you are saying. But I don't know why it is happening. And you may not be reporting all the relevant facts. I am trying to get you to think about the problem from a different point of view.

You did not say if you tested the reset button like I suggested ? You did not say what type of Arduino you are using ? You did not say whether it only works immediately after you upload code ?

You did not explain what is different between the initial working situation and the situation after "Event A" There MUST be some difference to account for the behaviour. There is no point considering anything else until you can account for that.

Let me try to describe a test process for you to try ... (the sort of thing that might happen first thing in the morning)

You start with your Arduino with its program already loaded and the Arduino not connected to anything. You plug the Arduino into your PC USB connection What happens ?

If it does not work in that scenario What must you do to get it to work ?

...R

Hi, In your youtube you didn't try the reset button after you reconnected the USB and it locked up. Can you try that please. Your sketch may be okay and it could be a characteristic of the clone UNO board.

Tom..... :)

Robin2: You did not say if you tested the reset button like I suggested ?

No I didn't say, but I demonstrated it in the video I posted in response to your question about it. But to again answer... When the program is working the reset button resets the prototype and then the program continues to work. However when the program has stopped working(after unplugging from USB and plugged back in) the reset button does not make the code start to work.

Robin2: You did not say what type of Arduino you are using ?

I called it an Uno several times. It is obviously a clone. The distributor visible on the silk screen in the image and video is 16Hertz. I don't know what other information I can provide.

http://www.amazon.com/gp/product/B00OHQ3BFE?psc=1&redirect=true&ref_=oh_aui_detailpage_o02_s00

That is web address if where I bought it. My attempt to make it a hyperlink would only return a blank page. Tried both with and without the extra bits after B00OHQ3BFE.

Robin2: You did not say whether it only works immediately after you upload code ?

I don't understand this question. But I will try to answer anyway. It is not immediately after I upload my compassBeta sketch. There is a slight pause. A couple of seconds. And as long as I do not unplug it from USB it has continued to work going on 14 hours now from when I made the video of it not working.

Robin2: You did not explain what is different between the initial working situation and the situation after "Event A"

The difference is the display stops updating because the HMC5883L stops producing new values. It goes from event A right into condition B. Event A CAUSES condition B. If event A never occurs the prototype stays in condition A(working).

Robin2: There MUST be some difference to account for the behaviour.

Something happens. No doubt. From my perspective the device is working until I unplug it from the PC USB. Then the prototype stop working. What simply unplugging USB and plugging back in causes to make the prototype stop working I don't know. It is why I am here asking the question of why it is happening.

Robin2: You start with your Arduino with its program already loaded and the Arduino not connected to anything. You plug the Arduino into your PC USB connection What happens ?

You seem to be asking something that I have explained several times now. That seems to describe the midpoint of event A. USB is unplugged but not plugged back in yet. And when plugged back in to USB the device fails to update past the initial heading. Unless you are suggesting to unplug the OLED and HMC5883L as well as the USB? At which point I would not be able to see if anything happens so I don't see the logic in that. So I don't think that is what you are suggesting.

Robin2: If it does not work in that scenario What must you do to get it to work ?

The only thing I have found to get it to start working again is exactly what I have both written and shown in the video. I must first upload magsensor.ino. Second I upload compassBeta.ino. Then prototype works again.

If magsensor.ino is not uploaded to the prototype after it is disconnected from USB and reconnected I can't get the compassBeta code to work properly. I have uploaded other working sketches and blank sketches. Something in magsensor.ino seems to "fix" whatever the issue is that is caused by unplugging the USB when compassBeta.ino is loaded.

TomGeorge: Hi, In your youtube you didn't try the reset button after you reconnected the USB and it locked up. Can you try that please. Your sketch may be okay and it could be a characteristic of the clone UNO board.

Tom..... :)

I have tried the reset button many many times. Both when prototype is working and not working.

While prototype is working and I press reset the program continues to work when reset is complete.

While prototype is not working and I press reset the program still fails to work.

Grumpy_Mike:
It could be that you are running out of SRAM. Do you get the same effect if you modify your code to exclude the OLED display and just have the code printing out of the serial port?

I modified my code to accomplish what I think you are asking.

#include "HCuOLED.h"
#include "SPI.h"
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_HMC5883_U.h>

const byte CS_DI =  10;
const byte DC_DI = 9;
const byte RST_DI = 8;

Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
HCuOLED HCuOLED(SH1106, CS_DI, DC_DI, RST_DI);

void setup()
{
  Serial.begin(9600);
  HCuOLED.Reset();
}

void loop()
{
  sensors_event_t event;
  mag.getEvent(&event);
  float heading = atan2(event.magnetic.y, event.magnetic.x);
  float declinationAngle = 0.056;
  heading += declinationAngle;
  if (heading < 0)
    heading += 2 * PI;
  if (heading > 2 * PI)
    heading -= 2 * PI;
  float headingDegrees = heading * 180 / M_PI;
  Serial.println(headingDegrees);
  //eightPoint(headingDegrees);
}
void eightPoint(float hD) {
  HCuOLED.SetFont(LCDLarge_24pt);
  if (hD > 337 || hD  <= 22) {
    HCuOLED.Cursor(35, 10);
    HCuOLED.Print("North");
  }

  if (hD > 22 && hD <= 67) {
    HCuOLED.Cursor(2, 10);
    HCuOLED.Print("NorthEast");
  }
  if (hD > 67 && hD <= 112) {
    HCuOLED.Cursor(45, 10);
    HCuOLED.Print("East");
  }
  if (hD > 112 && hD <= 157) {
    HCuOLED.Cursor(2, 10);
    HCuOLED.Print("SouthEast");
  }
  if (hD > 157 && hD <= 202) {
    HCuOLED.Cursor(35, 10);
    HCuOLED.Print("South");
  }
  if (hD > 202 && hD <= 247) {
    HCuOLED.Cursor(2, 10);
    HCuOLED.Print("SouthWest");
  }
  if (hD > 247 && hD <= 292) {
    HCuOLED.Cursor(45, 10);
    HCuOLED.Print("West");
  }
  if (hD > 292 && hD <= 337) {
    HCuOLED.Cursor(2, 10);
    HCuOLED.Print("NorthWest");
  }
  HCuOLED.Cursor(50, 50);
  HCuOLED.SetFont(MedProp_11pt);
  HCuOLED.Print(hD);
  HCuOLED.Print("*");
  //Serial.println(hD);
  HCuOLED.Refresh();
  delay(500);
  HCuOLED.Erase(0, 0, 127, 63);
}

Basically I commentated out the call to my method for using the OLED and just used a serial.println to display the output of the HMC5883L on serial monitor.
Result was upon upload the serial monitor displayed ever changing values of heading. This is good.
Upon disconnect and reconnect of USB the serial monitor stopped updating of course.
So I closed and re-opened the serial monitor and it began to display the same value over and over. This is bad. A duplicate of the issue with the OLED also running.
If you want me to run further tests by cutting code out instead of just commenting out I will. Just let me know.

I was/am concerned about memory usage. Specifically the memory used for global variables. It was originally at 75%. Removing all my serial.prints for debugging got this down to 67%.
I thought (and I could be very wrong here) that memory, if it was going to be an issue would be more at risk the longer the program ran. Prototype ran fine for 14 hours in longest test without unplugging from USB. So length of running is not an issue(so far) just the unplugging of USB.

But I do want to eventually make the code more efficient. There seems to be a ton of overhead in the includes for the HCM5883L. Reading 2 two values and doing a bit of math is all I need for this project. So once the hardware actually works after unplugging from the PC I plan on looking into writing my own code for that.
I would also eventually like to use the U8glib for the OLED since it complies a good bit smaller. But so far no luck getting it to work with this OLED.

I do understand that I am asking the same, or similar questions. But I need to be sure of what you are saying and it is easier to get you to answer specific questions.

Frostline: The only thing I have found to get it to start working again is exactly what I have both written and shown in the video. I must first upload magsensor.ino. Second I upload compassBeta.ino. Then prototype works again.

Now, perhaps we are getting to the root of the issue.

The project only seems to work after you upload the code. It seems as if the Arduino loses the code when you disconnect it from the PC.

That is why I asked you to confirm that you are using a clone. Is it possible the clone is not working properly ?

Have you tried uploading another non-trivial program and disconnecting it from the PC then reconnecting it - does it work ?

Can you make a pencil drawing showing all the wiring connections and post a photo of that ? A photo of your project is not a suitable alternative to a pencil (or pen) drawing.

...R

So I did some further investigation while awaiting further helpful suggestions. Since some comments brought the ability of my Uno into question I switched to a nano for further testing. |375x500

I do not know what code was loaded into the nano when I started. Most likely something with NRF24L01+ sketches.

This nano had never had an OLED attached before. Nor had it had OLED code sketches uploaded to it.

This nano had never had HMC5883L attached before. Nor had it ever had any code for that uploaded to it. This includes never having magsensor.ino on it.

I connect the nano, the OLED and the HMC5883L.

I set the Arduino IDE to the proper device and com port.

I upload my compassBeta.ino to the nano.

OLED displays the initial heading and nothing else. All rotations of the nano are ignored by the program. It displays the initial value only. Basically the initial test started in condition B.

Pressing reset does not change this.

So I apply the same solution for condition B that I used with the previous board.

Upload magsensor.ino then upload compassBeta.ino.

Nano OLED display now responding to rotational changes by updating new values to the OLED. It is now in condition A.

So changing boards had no effect. When unplugging from USB the HMC5883L stops responding and will not respond until magsensor.ino is loaded no matter which board is used. If a board has not had magsensor.ino ever installed then compassBeta.ino will not work at all.

All I can determine is that something in magsensor.ino allows the HMC5883L to recover from power disconnect and sets it to function correctly. Without magsensor.ino uploaded prior to compassBeta.ino the prototype will not work after a power disconnect.

Robin2:
The project only seems to work after you upload the code. It seems as if the Arduino loses the code when you disconnect it from the PC.

This is not quite correct.

My code compassBeta.ino is not getting lost.

The HMC5883L stops working on a power disconnect. The OLED is displaying the value correctly from the HMC5883L. The trouble is the HMC5883L starts sending the same value instead of new values.

Reloading just my code does not ‘fix’ this.

I have to first load a specific different code magsensor.ino.

Then I can load my code compassBeta.ino and the device starts to function properly.

More of what you asked was probably answered in my post I was writing as you were replying.

Honestly the way I draw I don’t know how helpful that will be. But I will attempt to do so.

Also I have uploaded an OLED animation program to both boards I have used. Disconnected them from USB, reconnected them to USB and the animation program resumes running. That is probably the only non-trivial sketch I have to test.

I have used the Uno (the first board) on other projects involving 8x8 LEDs matrix and NRF24L01 transceivers without USB, remotely on battery power and it functioned fine.

Terrible drawing as requested.

|375x500

The Nano tests are very useful and your drawing is perfectly fine. It does not ring any alarm bells for me apart from the small possibility that the HMC device requires more current than the Arduino 5v can supply.

In your Original Post you attached the magsensor code but I don't see the compass code anywhere.

Also, if the magsensor code has changed please post the lastest version.

Have you tried removing the OLED and all its code and just printing the data to the Serial Monitor ?

I find this confusing

My code compassBeta.ino is not getting lost.
...SNIP...
Reloading just my code does not 'fix' this.

I have to first load a specific different code magsensor.ino.

There seem to be two issues here. First, I get the impression that the compassBeta does not run after de-powering the Arduino. If that is true, how can you say it is not getting lost ?

Also you say you first have to load another program and that certainly "loses" the compass code.

Second, the magsensor code must be doing something beneficial to "untangle" the Arduino. (Of course if something needs to be untangled it may be evidence that it was not "lost")

And I am not quite sure which is the code you actually want to work ? I am assuming it is the compass code (and, as I say, I don't recall where you posted that).

Finally, am I correct to assume that the magsensor program runs perfectly in all circumstances ?

...R

Thanks for doing that test, it looks like we can rule out memory problems for the time being.

that memory, if it was going to be an issue would be more at risk the longer the program ran.

No that is only when you have a memory leak, and that is not the case here.

Two points:- 1) You seem to have no pull up resistors on the I2C bus. You do need them. Look here at the results of not having them. http://www.dsscircuits.com/index.php/articles/47-effects-of-varying-i2c-pull-up-resistors

2) It could be that the first program is doing some initialization of your compass module that the second program is not doing. This would explain why you get it working when you load one followed by another, but on a power down this initialization is lost and therefore just having the second program will not work.

Robin2:
In your Original Post you attached the magsensor code but I don’t see the compass code anywhere.

The compassBeta code is in post #1 and post #10. I was concerned that my post was too long to have the magsensor.ino code in-line with the rest of the post so I attached it.
The only difference between the two is the uncommenting out of my serial.prints for debugging and the commenting out of my method that make the OLED display the data.

Robin2:
Also, if the magsensor code has changed please post the lastest version.

Magsensor.ino is not my code. It is the example that came with the library for the HCM5883L. I have not modified it in any way. I use it only to ‘reset’ the HCM5883L so it will start outputting more then a single run of data.

Robin2:
Have you tried removing the OLED and all its code and just printing the data to the Serial Monitor ?

Not exactly. Please see post #10. I disabled the OLED without removing it entirely.
But since asked to specifically remove it I will attempt to with the code below.

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



Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);

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

void loop()
{
  sensors_event_t event;
  mag.getEvent(&event);
  float heading = atan2(event.magnetic.y, event.magnetic.x);
  float declinationAngle = 0.056;
  heading += declinationAngle;
  if (heading < 0)
    heading += 2 * PI;
  if (heading > 2 * PI)
    heading -= 2 * PI;
  float headingDegrees = heading * 180 / M_PI;
  Serial.println(headingDegrees);
  
}

This performs exactly as the code in post #10.
Without magsensor loaded first, the above code just displays the same value over and over.
With magsensor loaded first the display value changes with the rotation of the HCM5883L.

It also stops returning new values to serial monitor once it is unplugged from USB. Just like my code from post #1.

Robin2:
There seem to be two issues here. First, I get the impression that the compassBeta does not run after de-powering the Arduino. If that is true, how can you say it is not getting lost ?

Because the OLED displays a value consistent with what it is programmed to do. If my program was ‘lost’ how would the OLED know how to display anything? The display is displaying a value properly.
The issue is once power is removed the HCM5883L is not changing the values it produces for the display to show.

When it is ‘working’ the HCM5883L is outputting values like this below when it is not even being moved

302.05
302.05
301.87
301.87
301.87
301.87
301.87
301.87
301.87
301.87
302.01
302.01
302.01
302.01

But after power is removed and restored the output is this

302.07
302.07
302.07
302.07
302.07
302.07
302.07
302.07
302.07
302.07
302.07
302.07
302.07
302.07
302.07
302.07

Just an initial value, over and over. Doesn’t matter if I turn the sensor any direction, flip it over or anything else. The value it outputs to serial does not change.
But it is still outputting ‘something’ and that ‘something’ is still being sent to the serial monitor. If the code was ‘lost’ none of that would happen. Right?

Robin2:
Also you say you first have to load another program and that certainly “loses” the compass code.

Quite right. But simply unplugging the nano does not do that.
I also immediately ‘lose’ the magseonsor code by uploading the compassBeta code. Yet that brief period of the magsensor being installed and over-written is enough to make compassBeta work until it is disconnected from USB.

Robin2:
Second, the magsensor code must be doing something beneficial to “untangle” the Arduino.
(Of course if something needs to be untangled it may be evidence that it was not “lost”)

I agree to both points.

Robin2:
And I am not quite sure which is the code you actually want to work ? I am assuming it is the compass code (and, as I say, I don’t recall where you posted that).

Yes, it is compassBeta I want to work when unplugged from USB. Makes it quite difficult to take it outside to test otherwise :slight_smile:
I will attach the compassBeta code to this message so you don’t have to go back to post #1 or #10 to find it.

Robin2:
Finally, am I correct to assume that the magsensor program runs perfectly in all circumstances ?

I can have it (magsensor code) running, unplug it from USB, plug it back in to USB and it starts running again. I just have to close and reopen the serial monitor because it quits when the USB is disconnected.

compassBeta.ino (1.71 KB)

Grumpy_Mike: Two points:- 1) You seem to have no pull up resistors on the I2C bus. You do need them. Look here at the results of not having them. http://www.dsscircuits.com/index.php/articles/47-effects-of-varying-i2c-pull-up-resistors

2) It could be that the first program is doing some initialization of your compass module that the second program is not doing. This would explain why you get it working when you load one followed by another, but on a power down this initialization is lost and therefore just having the second program will not work.

As to point 1. I 'think' the required resistors are built into the board the HMC5883L is on. Not 100% sure though since my board is slightly different. See here.

I could be easily 100% wrong on that. But I would think the problem with new data being stopped would happen all the time, not just on USB removal if it was an I2C issue lacking resistors. And since the board does some type of voltage regulation(it allows 5v input for the HMC5883L which has a max input of like 3.6v per the datasheet) I do not know what value of resistors I would need. Datasheet shows 2.2k from I2C to VDD but I don't know if that value is based on typical voltage of 2.5 or max of 3.6 or if voltage in this situation matters.

I think your point #2 is exactly right. But I can't see what is different in the magsensor code from my compassBeta code other than a bunch of serial.prints that I don't want/need. Which is why this thread started. I basically know magsensor does 'something' I did not copy to compassBeta. I just don't know what that 'something' is.

There's no mag.begin() in the compassBeta.ino code. There is in the magsensor.ino code.

mag.begin() must be doing something to initialise the sensor, not just the Arduino.

It's a little hidden in the demo code, because it's used as a conditional inside an if() statement. Basically, that is being used to print something if the sensor is not connected and mag.begin() fails. However, this test has a side-effect of initialising the sensor.

You can use mag.begin() without wrapping it in an if() statement. Put it in setup().