Low-cost and precise rotary encoder with magnetic spheres

You know what they say that constraints drive creativity, I think this post is just another little example of that.

Usually I do not put time constraints to my hobby projects, but I make sure to have some other kind of constraints, like keeping the cost low and trying to reuse stuff that I already have.

In this case I needed a way to measure the angle and speed of a rotating object for some of my projects, and I wanted a rotary encoder that was inexpensive, but precise enough to work at very low speeds.

I ruled out optical encoders (I could have reused a mouse) because there could have been noise issues in the final project, so I decided to go magnetic.

I purchased a couple of Hall effect sensors but I had no idea of how I was going to use them.

Later I realized that the US1881 sensors I bought were bipolar latching sensors (check out the Hallbook for a nice reference of magnetic sensing), meaning that their state would persist when the magnet was out of reach.

These kind of sensors are not designed to be used with the magnetic pole facing the sensor, in a reed-switch fashion, they require alternating magnetic poles to switch states; most of the time these Hall effect sensors are used with ring magnets.

I didn't want to buy a different sensor (unipolar ones may work better with sparse magnets) or a dedicated ring magnet, so I started to think about how to build a ring magnet from what I had lying around.

One way to build a ring magnet is to place small magnets with alternating poles in a circular arrangement, like proposed in Custom 3D Printed Magnetic Encoder Disks for Robotics Projects by Erich Styger.

However I don't have a 3D printer, nor such small magnets, but I had around a Tetramag: a toy made of magnetic spheres.

The Tetramag spheres have a diameter of 5mm, that means that a pole covers about 2.5mm (pole pitch) and that the "element pitch" (the distance between the two sensing elements) should be of about 1.25mm.

Check out the section about quadrature in the Hall-Effect IC Applications Guide from Allegro Microsystems for more info.

In practice, though, the two sensing elements do not necessarily need to be adjacent and cover the same magnet, it's enough that they are in a position which results in a quadrature signal: with the two channels being out of phase of about 90 degrees, like shown in the following picture:

Tetramag ring magnet - element pitch

And here is a photo of the sensors position in real-life:

Hall effect sensors position

In my demo setup, a piece of a pen was used as the spindle and pieces from a CD stack case are reused for the knob of the encoder:

Rotary encoder demo setup

The result is a low-cost rotary encoder, with a precision depending on the number of spheres used to build the ring.

Look at the demo built with 21 spheres:

In this case the encoder has 21 x 4 = 84 pulses per revolution (it's a 2 channels quadrature encoder) and can measure angles down to 360 ÷ 84 = 4.29 degrees.

The code of the experiment can be found at git.ao2.it/experiments/Hall_effect_rotary_encoder.git

And now that I know more about Hall effect sensing I can decide whether a better sensor can be used for my next project: it may actually make sense to use a single dual sensor instead of two discrete sensors.

But you don't know until you know, right?

CommentsSyndicate content

Awesome man. Thumbs up. :D By

Anonymous's picture

Awesome man. Thumbs up. :D

By the way, whats dfrobot board is that?

Thanks. The board is a “1602

ao2's picture


The board is a “1602 LCD Keypad Shield” mounted on an Arduino UNO.

Ciao, Antonio

Antonio, You sir are

Dave's picture

Antonio, You sir are brilliant. The method you have shown is the answer to one of my projects. This method can easily be made waterproof. I am going to try it as a front panel control to adjust digital potentiometers that are inside a waterproof housing.

It might be a good idea to mount the sensors above and below the disc ring instead of in front of it. This will make the finished device more compact. It will also make it easier to adjust the magnets and allow smaller 3mm magnets to be used. I will have to try this to see if I am right. As you say, You don't know until you know. Dave. * * *

Thanks Dave. I think

ao2's picture

Thanks Dave.

I think magnetic solutions are generally easier to waterproof, aren't they?

The advantages of my approach are mainly the flexibility and the affordability.

The tricky part is finding the right distance between the two sensors.

About the hall effect sensors and their positioning: I found out that there are also these "2D, Dual Channels, Hall-Effect latches" which would work independently of the distance between the magnets poles and can be placed in different positions.

Examples of such sensors should be the Allegro A1262 and APS12626.

I would like to experiment with these sensors sooner or later with both 5mm spheres, 3mm spheres, and maybe also those 1mm cylindrical magnets.

If you want to sponsor me some samples, of if you do such experiments yourself, please get in touch via email.

Keep me posted about your project.

Ciao, Antonio

Good job, I need to use a 2mm

Anonymous's picture

Good job, I need to use a 2mm neodymium spheres instead of 5mm, then I must use only a single dual hall sensor ?

My project will be to use a multiple of sphere placing in a circle of 5,1 cm or 5,7 cm diameter, about 80 or 90 spheres respectively.

This for controlling a stepper motor that move this circle, it is a feasible hypothesis ?

Using one or two hall sensors

ao2's picture

Using one or two hall sensors depends on the functionality you need, one latching sensor is enough to just "count" spheres, but to detect direction two sensors are needed.

If you are using smaller spheres it might not be possible to use two separate sensors because the pitch may be too small. In that case you can try with a 2D hall effect sensors as said in one of the previous comments.

Ciao, Antonio

Which resistors did you use.

Alex's picture

Which resistors did you use. Is there a parts list somewhere we can use to duplicate this set up?

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account associated with the e-mail address you provide, it will be used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options

This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Enter the code without spaces.