I have purchased this TMR, angle sensor evaluation kit. I want to understand how it works:
I have used example codes from NVE's YouTube and GitHub and I am having issues.
I have got a replacement unit, and it is still behaving in the exact same way. I have switched Arduinos and still, the exact same.
Exact issues:
At -90 degrees on the evaluation board, the sensor reads 80 degrees.
At +90 degrees on the eval board, the sensor reads 100 degrees.
0 degrees and 180 degrees on the eval board are correct
Please see my code and serial monitor output below.
I am trying to understand the atan2 function and how it is even working at 0 degrees. Surely for 0.72 degrees, the sin value and cos value in the atan function need to result in 0? I.e. 600/600?
Once In understand the application at 0 degrees, I then plan to apply that to the 90 degree position and see where it is going wrong. I have added the serial monitor output at 90 degrees at the bottom as well.
The for statement in the setup is there to "calibrate the sensor".
I rotate the magnet around and the code calculates the max and min readings for the sensor outputs. These are then used to calculate the offsets, which can then be used to normalise the reading later on.
I understand atan2 gives the angle in radians, hence in the inclusion of degrees and radians in the serial monitor and my conversion.
I understand atan2f produces the output as a float instead of a double.
Looks like the calibration is not done correctly. The sin term should be zero if the angle truly is zero degrees, and the cos term should be 1 (after normalization).
With the atan2 function, normalization of the X and Y terms are not required, but you should make sure that the offset-corrected minimum and maximum values of each are the same as you go around the compass rose. If they aren't, a relative scale factor is also required.
This has woke me up to realise what I should be expecting from the sin and cos outputs.
I will assess the outputs and their offsets again.
I have read the link you have sent. Quite complex. I appreciate it is related, but I think my issue might but a lack of understanding of the atan2 function and the requirements from my sensor. Like I said, following your advise, I will look at the sin output at 0 degrees.
atan2() is just the inverse tangent function, but rather than taking a single value of Y/X as input, they are kept separate to allow the angle quadrant to be distinguished.
Where MAX V is the maximum output of the sensor, MIN V is the minimum output, and 0V is the 0V offset:
Sensor outputs SHOULD be:
@ 0 degrees: Sin = 0V, Cos = MAX V
@ 90 deg: Sin = MAX V, Cos = 0V @180 deg: Sin = 0V, Cos = MIN V @270 deg: Sin = MIN V, Cos = 0V @360 deg: Sin = 0V, Cos = MAX V
Therefore, given the following, I believe I am using atan2 the wrong way around with my X and Y. But I do not understand how NVE have got their demos to work with this evaluation board. I am using their code, with the serial monitor additions to the code, and I have changed the pins used. I cannot see I have made any mistakes with the pins.
I believe this because of the readings I am getting on the serial monitor:
Sin:
Max = 604, Min = 430. Middle (0V offset) = 517.
E.g. 0 deg should be 517. (However, I get 597)
Cos:
Max = 602, Min - 419. Middle (0V offset) = 510.5.
E.g. 0 deg should be 602. (However, I get 511)
Sorry if this is a stupid question, but how am I supposed to determine whether the sin or the cos equates to the X or the Y please?
In the demo code, they have it set up in the configuration I have used in the code given. I know that they could have it wrong also. I am just trying to understand sorry.
There are some variations due to the electrical differences in the sensor. This is stipulated in some of the demos, and the MAX and MIN V for Sin and Cos is calculated as a result.
Plot the corrected X, Y values as you go around the compass circle. In the ideal case, they form a circle, centered on the origin. If not, either the calibration results are wrong, or they were incorrectly applied.
I would like to say again, thank you so much for your help.
In regard to this issue, in your experience, would this issue be present on all batches of these sensors?
NVE sell these eval kits and the sensors using their demos which I will link below. In absolutely none of their materials do they demonstrate any of the issues I am having/what you are helping me with.
I have no idea, but do look carefully at your setup for possible problems with current carrying wires or magnetic materials near the sensor, like the Neopixel strip surrounding the sensor in the video link above.
If you post a set of X,Y values, I will run it through the correction procedure described in the Sparkfun link, and see how it goes. Copy/paste into code tags.