Introduction: Neodymium Angle Encoder

About: 55+ years in electronics, computers, and teaching ... now retired.

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

Supplies

The following items were obtained from https://www.aliexpress.com/

  • 10 only 6mm x 3mm neodymium button magnets [1]
  • 2 only 49E Linear Hall Effect Transistors [2]

The following items were obtained locally:

  • 1 only M4 x 20mm bolt
  • 1 only M4 nut
  • 1 only Arduino header pin strip
  • Thin plastic coated hookup wire
  • 3D printed parts (described later)

Notes

[1]

https://www.aliexpress.com/item/1005001991968667.html or equivalent

[2]

The 49E Hall Effect transistors from KY-024 Linear Hall Effect Modules can be “borrowed” if you have some of these.

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.


Magnets Contest

First Prize in the
Magnets Contest