Hello, I am participating in a contest, details don't really matter but I need to use the compass to make the robot return to a certain heading when required. Using an arduino mega wires and the HMC5883L I managed to do this.
However I need to have everything in a PCB and decided to use the Teensy 4.1 as I didn't have to change my code much. I wired everything, HMC5883L's SDA going to pin 18 on the Teensy, and the HMC5883L's SCL going to pin 19 on the Teensy and I upload the code (same code that worked perfectly on the Arduino Mega), and I get values ranging from 112.9 to 112.41. Not at all the degrees I was hoping for.
The values also repeat at certain places. For example you may get 112.13 when the robot is facing me but get it again when its a bit more to the right for instance. Garbage values. I've spent the last 90 mins on this but haven't been able to crack it. If anyone can help, it would be much appreciated!
My code:
// Including appropriate libraries
#include <math.h>
#include <Arduino.h>
#include <Wire.h>
#include <HMC5883L_Simple.h>
// Compass
HMC5883L_Simple Compass;
void setup() {
Wire.setSCL(19);
Wire.setSDA(18);
Wire.begin();
Compass.SetSamplingMode(COMPASS_SINGLE);
Compass.SetScale(COMPASS_SCALE_130);
Compass.SetOrientation(COMPASS_HORIZONTAL_X_NORTH);
}
void loop() {
Serial.println(Compass.GetHeadingDegrees());
}
Output from the above code:
Also I have checked my PCB design, everything is how it should be. I doubt it's an issue with that. If you would still like to see it I'd be happy to provide, it's just a bit messy.
Thank you again.