When driving my train in train simulator 2020 I wanted to look around by moving my head a little. I figured if I turn my head like ~15 or 20 degrees to the left that the monitor turns 90 degree and I can look out my side window.
At first I wanted to use a cheap accelerometer but those do not work for the Z-axis. So it was either this or a gyroscope. Gyros are more expensive and complex and I happened to stumble on a magnetic sensor.
I ordered a mini arduino board with an atmega32u4 and a HMC5883L magnetic compass sensor. The idea is that I tape these to my headset and connect to the computer with usb. The 32U4 can than simulate the mouse input.
I also want to add an on/off switch and calibrate the sensor's Z-axis upon power-up to cope with potential drifting.
Q. Is this a viable approach or would I need a gyroscope instead?
The sensor is advertised to have a resolution of no less than 2 degrees which is sufficient.
Bas
The HMC5883L has been out of production for a long time, and you most likely got a mislabeled clone, which is really the QMC5883L and requires a different library. Use the I2C address scanner program to be sure.
To be used as a digital compass, the sensor must be calibrated. Here is the simple, less accurate way and the best way to do that.
Well I looked into the links but seems a tad overkill for what I want. I do not need to know where the north pole exactly is.
I merely want to use the sensor to determen if I rotate my head left, right, up or down. When I turn on the arduino (and sensor) I will be looking at the monitor. At that moment in the setup, I want to read all axis of the sensor and use those values as my absolute zero point.
Therefor it should not matter if the sensor thinks that the magnetic northpole lies in China or Alaska. Atleast not in my thoughts.
For my implementation would such a calibration actually be needed?
Bas
A compass program will produce meaningless numbers if the sensor is badly out of calibration.
Do the experiment and it will be obvious.