 # Sine wave conversion through a to d

Hi all, I am working with an amr based angle detection sensor(KMZ60). KMZ60 is magnetic angle detection sensor. The sensor continuously gives cosine and sine wave output on varying magnetic field. On feeding this output to UNO's analog pin, I get continously varying values from 0-1024 as i move the magnet around the sensor. How should i interpret this data ? How am i supposed to find the angle from this data ? Can you suggest me any other method, if there are any ? Thanks.

If you point the compass to North, does it give stable results? if so you can derive a formula like

angle = analogRead() * 359/1023 (assuming 0 == 0 and 1023 = 359 degrees.

Hi rob, Thank you for your reply. It does give stable results. The sensor is generally used for contactless angle measurement to detect the angle of a rotating magnetic field like window wiper position detection. The sensor gives a sine and cosine output. I can definitely use the method suggested by you. Is it how a sine wave should be interpreted ? in the absence of magnetic field the a2d gives a constant value of 326 . So if its your method, i will get 130 degrees with absence of magnetic field which doesnt quite make sense. I am having a hard time interpreting these values.

In the sensor's application note, it is mentioned that the two waves are proportional to sin2a and cos2a. Its also mentioned that the angle can be found using angle = 1/2 arctan(x/y). any idea how to go about it ?

maybe time to post the link to the application note?

you probably need to measure both and do the following:

``````float x = analogRead(A0);
float y = analogRead(A1);

float angle = atan2(y, x) * 180 / PI;
of
float angle = atan(y/x) * 180 / PI;
``````

note that atan2 returns an angle between -PI, PI (-180..180) atan returns an angle between -PI/2, PI/2 (-90..90)

so I guess you need atan2()

Hi rob, I am posting the link to the application note,

http://www.arroweurope.com/nc/services/download-center.html?tx_abdownloads_pi1[action]=getviewclickeddownload&tx_abdownloads_pi1[uid]=1190&tx_abdownloads_pi1[cid]=9774

you can have a look at the angle masurement technique given from page 3. I did follow this technique, as mentioned. As the a2d gives a constant value of 326(absence of magnetic field), the final angle I get after the conversion is 20. I will post the results of the method that you had suggested earlier. I havent tried it out yet. Your help is much appreciated. Thanks rob !

how did you connect the KMZ60? Did you use the capacitors and resistors as mentioned in the datasheet? Can you post your schema?

Hi rob,
I connected them directly to the input pins of the arduino. The connections are pretty straightforward. R1,R2,R3,R4 are permalloy strips. I think you have misinterpreted them for resistors. Based on your suggestion,I used atan2().

``````float mx = sine - 512.0f;
float my = cosine - 512.0f;
float angle = atan2( my , mx );
if (angle < 0) { angle = angle + (2.0f * 3.14); };
angle = (angle * 90.0f / 3.14);
``````

Its now giving angles from 180 to -180 based on the position and proximity of the magnet. I am yet to experiment further. I ll keep you posted on my progress. Your help is much appreciated, rob ! Thanks a lot

Welcome, good to hear it starts working!

Please post the whole (minimal) sketch for future reference.

``````if (angle < 0) { angle = angle + (2.0f * 3.14); };
``````

There is a predefined constant M_PI which will give you much better precision than using 3.14.

But, atan2 returns a value in the range -180 to +180, in which case converting it to the range 0 to 360 requires that you add M_PI (180), not 2*M_PI (360) .

I’m not sure that this statement is correct:

``````angle = (angle * 90.0f / 3.14);
``````

To convert angle from radians to degrees you should multiply by 180.0/M_PI.

Pete