Neodymium Angle Encoder

7.2K9517

Intro: Neodymium Angle Encoder

Neodymium button magnets are extremely cheap. This instructable explains how to make an angle encoder using 10 neodymium magnets and two hall effect transistors.

The encoder accuracy is within 1 degree.

The design is such that the components can be embedded within the joints of a robotic arm or servo plotter.

The estimated cost of the encoder, excluding the Arduino, is less than $10.00

Images

  • Photo 1 shows the assembled encoder
  • Photo2 to shows an exploded view of the encoder
  • The video shows the encoder working

STEP 1: Circuit

 The encoder circuit diagram is shown in photo1.

In the absence of a magnet field the A0 and A1 output voltages from each 49E Hall effect transistor is Vcc/2= 2.5 volts.

This equates to an Arduino ADC (analog to digital converter) reading of 1023/2=512.

STEP 2: Theory

Photo1 shows 5 button magnets arranged in a circle. The North pole of each magnet is facing upwards.

In photo2 a magnetic field viewer [1] has been placed over the magnets in photo1. For the purpose of explanation, the blue color represents maximum output change in a Hall effect transistor and the yellow color represents minimum output change. The reason the yellow color is circular is that the lines of force emanating from each of the North magnetic poles radiate in all directions to form a “magnetic globe” about each magnet.

In photo3 South magnetic poles have been inserted between each of the previous North magnetic poles.

Photo4 shows the magnetic field pattern when the magnets are arranged NSNSNSNSNS as in photo3. In this photo the North magnetic lines are attracted to the nearest South magnetic pole. This has the effect of segmenting the circle into 10 segments each of 36 degrees. The yellow lines indicate minimum change in the Hall effect transistor outputs [2]

Photo5 shows how I have positioned each of 2 Hall effect transistors. The “sine” Hall effect transistor is placed over a line of minimum output change. The “cosine” Hall effect transistor is placed over an area of maximum output change. “Sine” and “cosine” waveforms are produced if we keep the Hall effect transistors stationary and rotate the magnets.

Photo6 shows how “sine” and “cosine” waves are formed. For ease of explanation the magnets are shown in a straight line. [3]


Key points


  • There are an even number of magnets.
  • Every 2 magnets produces 1 sine wave
  • For 10 magnets we get 5 complete sine waves per shaft rotation.
  • Each sine wave represents 360/5=72 degrees shaft rotation
  • Each half cycle is 72/2=36 degrees
  • The distance between a maximum and a minimum is 36/2=18 degrees which means my sensors must be N*36+18 degrees apart. In my encoder I have set N=1 so the sensors are 54 degrees apart. Setting N=0 was not possible due to the size of the sensors.

The formula for calculating the shaft angle is therefore:

Shaft-angle = N*72 + atan2(B,A)/5 …………………………………..............….. (1) [4]

where:

  • N = number of completed sine waves.
  • A = magnitude of the “sine” waveform at any given instant.
  • B = magnitude of the “cosine” waveform at any given instant.
  • 5 = number of sine waves in 360 degrees.


Practical considerations


  • A spacer is required between the magnets and the Hall effect transistors to prevent the Hall effect transistors saturating. Optimum spacing is when the ADC (Analog to Digital Converter) readings keep changing (i.e. never stay still) when the magnets are rotated. A spacer thickness of 2.7mm proved optimum for my magnets.
  • The number of cycles are tracked in software.
  • Other magnet combinations are possible providing the magnetic-pairs divide evenly into 360 degrees. For example 12 magnets will produce 6 cycles, each representing 60 degrees shaft rotation, with zero crossings every 30 degrees. In this case the distance between a maximum and a minimum is 30/2=15 degrees which means your sensors must be N*30+15 degrees apart. In this example your shaft angle will be N*60 + atan2(B,A)/6

Notes

[1]

A magnetic field viewer is not required when constructing this encoder but may be obtained, should you wish, from https://www.aliexpress.com/item/1005004212472338.html

[2]

The maximum and minimum change in a Hall effect transistor output depends on the orientation of the Hall effect transistor with respect to the magnetic field direction. In my case the Hall effect transistors lie flat which means they produce greatest output change when they are directly above the magnets.

If the Hall effect transistors are rotated 90 degrees onto their edges, then minimum output change will occur above the magnets and maximum output change between the magnets.

[3]

In the absence of a magnetic field the Arduino ADC readings for A0 and A1 will be close to 512. These readings will vary either side of 512 by +/- 300 (approx.) when the magnets are rotated.

[4]

Regards equation (1)

  • sin(A) = opposite/hypotenuse
  • cos(B) = adjacent/hypotenuse
  • sin(A)/cos(B) = (opposite/hypotenuse)/(adjacent/hypotenuse) = opposite/adjacent = tangent.

STEP 3: 3D Printing

Photo1 shows the magnet arm. The *.stl file for this part is “10mm_magnet_arm.stl”

Photo2 shows the sensor arm. The *.stl file for this part is “10mm_magnet_sensor.stl”

Photo3 shows the spacer. The *.stl file for this part is “10mm_magnet_spacer.stl”

Photo4 shows the printed parts

Each part was printed on a Voxelab Aquila 3D printer using 1.75mm PLA. The nozzle size was 0.4 mm with a layer height of 0.2mm.

STEP 4: Software Installation

  • Download the attached file “neodymium_angle_encoder.ino”
  • Copy the contents into a new Arduino sketch. Use a text editor such as Notepad++ … NOT a word processor.
  • Save the sketch as “neodymium_angle_encoder” (without the quotes)
  • Compile and upload the sketch to your Arduino.


Calibration and Adjustment


  • Connect a jumper wire between Arduino pin8 and ground.
  • Rotate the magnet arm a few times until the display readings are constant.
  • Copy the display readings into the matching variables in the Arduino header.
  • Remove the jumper.
  • Recompile your code and upload it to your Arduino.
  • Further calibration should not be necessary.
  • All angles are relative to the position of the encoder arms when your Arduino is switched on. [1]

Note

[1]

Photo1 shows the transition from calibration readings to angle measurements. The transition occurs when the jumper wire from Arduino pin8 to ground is removed.

Copy the calibration values into the Arduino "header" code, and recompile, if you don't wish to calibrate each time you start your Arduino.

All angles are relative to the start position of the encoder arms when the Arduino is started.

STEP 5: Test Results

Photo1 shows my test setup. The radials lines are at 10 degree intervals.

Photo2 shows a plot of angle versus error.

The measured angles are all within 1 degree of the actual angle.

STEP 6: Summary

 This instructable explains how to make a high precision angle encoder using 10 neodymium magnets and two hall effect transistors.

The encoder accuracy is within 1 degree.

The design is such that the components can be embedded within the joints of a robotic arm or servo plotter.

The estimated cost of the encoder, excluding the Arduino, is less than $10.00


  Click here   to view my other instructables.


15 Comments

wer soll das nachverfolgen, ohne ein wirkliches ergebins zu sehen, praxis untauglich ... Tut mir Leid
Thank you for sharing your take on my project ... it's great :)
The "arms"in my version were placed on different planes to avoid them colliding when I was testing multiple turns.
Very nice concept, this is a great Instructables for the Magnets contest (just checked if you did apply).
I'm right now busy with magnetic encoders (AS5600 sensor), wherein everything i pre-packed, yet your way is muchj more creative.
Thank you for commenting :)
Your AS5600 project sounds interesting. Will be interested to see your results.
Regards the contest ... my entry is pending review.
I see your project in the contest, it makes a lot of sense.
Wrt the AS5600, there is an Instructables whit also the calibration part (https://www.instructables.com/AS5600-Magnetic-Angl...
So far I have made a single assembly, and only used the I2C (not tried the analog output yet); Without making any calibration, and by assuming a motor step to make equal steps, the deviation is within +/-1.5deg; Below chart is made by stopping the stepper (1600steps/rev) and reading the encoder
I planned on using an AS5600 to warn me when a a stepper had lost steps. It seemed to be usable up to 4 micro steps (800) steps per revolution. Beyond that it wasn’t accurate enough. Then I found a website where someone had tested the positional accuracy of microstepping. He used a laser fastened to the stepper shaft and shone that on a wall some distance away. His results indicated that micro steps are NOT positionally accurate. In fact the accuracy varied between different types of drivers.
Therefore you can’t tell whether it is the encoder or the stepper that is leading to your deviations.
On my measurements the AS5600 encoder counts a full revolution (4096+/-1) every time I send 1600steps to the stepper (a 200 steps/revolution stepper with a TMC2209 driver set in 8th microsteps).
This is also visible from the chart I shared, wherein the deviation at 360degrees equals the one at 0 degrees.
Loosing steps might depends on many factors.
Anyhow, I wouldn't use the AS5600 to control a stepper motor accuracy.
@lingib: I'm sorry, I went a bit off topic from your ingenius Neodymium Angle Encoder solution :-)
Thanks for sending your graph :)
The tail end of your link is missing but I think you are referring to my article https://www.instructables.com/AS5600-Magnetic-Angl...
In that article I get a similar variation before I correct using interpolation. I suspect the error is due to the magnet being slightly off-center
ha ha ha, I clearly didn't noticed that article was from you....
Well, I read it now more carefully and indeed the system inacuracy is rather similar.
This inacuracy shouldn't be an issue on the project I'm busy with.
I'll come back to you if interesting learnings on this aspects.
Regards, Andrea
Good luck with your project.
I look forward to hearing from you :)
Another great instructable Lingib
Thank you :)
Great work, I can foresee many applications for this.
Thank you :)