Magnetic Incremental Rotary Encoder

A compact solution for your closed loop positioning requirements

Similar projects worth following
Rotary encoders are used to measure angular position of a shaft. Several types of rotary endcoders exist, the differerence being the strategy used to measure angular position increments, with the most common ones being mechanical and optical ones.

This project aims to provide a compact, easy to use, relatively high resolution and arguably cheap encoder. It uses the AS5040 IC from Austria Microsystems.

Quick Intro to Rotary Encoders

A mechanical rotary encoder uses a wiper (or wipers) that closes a circuit along a set of contacts distributed along a circumference. The wiper is attached to the shaft one is working with, and as this shaft turns, the wiper opens and closes a circuit, thus generating a pulse train that can be used to determine the angular position of the shaft (and its higher order derivatives with respect to time)by counting the number of pulses. A similar idea its used on the optical encoders, with the set of contacts replaced by a disk full of cutouts attached to the shaft, and a light source/photodetector that are placed on opposite sides of said disk to make the disk sequentially block and allow the light to shine onto the photodetector, again generating a pulse train containing useful Kinematic information.

One light source/photodetector pair is useful if one does not need to determine the direction of rotation of the shaft. By adding a second set of cutouts (or a second light source/photodetector pair) 90 degrees out of phase with respect to the first one, one can detect the direction of rotation of the disk. Devices that employ this technique re called quadrature encoders, and they normally designate the photodetectors as A and B.

Enter the AS5040

The AS5040 is sensor that detects changes in the angular position of the poles of a magnet, and outputs a two pulse trains, analog to the A and B outputs of optical encoders. I find this IC more versatile than an optical encoder. Some of its advantages are:

  • End user settale resolution: 128, 256, 512 or 1024 state changes per revolution with 4x decoding.
  • Only requires a two pole magnet to work.
  • On chip decoding of the quadrature singnal.
  • 30,000 rpm maximum speed!
  • Operates at 3.3 V or 5 V.
  • End user settable zero.
  • Can be used as a brushless motor sensor.

The end user settable resolution its useful for avoiding bogging down the controller decoding the pulse train, specially at high speeds. Also, gluing a magnet onto a shaft and then alligning the PCB containing the sensor its easy, in contrast to fabricating a disk with 256 cutouts and mounting it to the corresponding shaft and light source/photodetector. Also, the final result is far more compact compared to the cheap otical encoders with similar resolution out there.

  • 1 × 0.1 in male 90º header 3 pin header
  • 2 × CC0603KRX7R9BB104 CAP CER 0.1UF 50V X7R 0603
  • 1 × RC0603FR-0710KL RES SMD 10K OHM 1% 1/10W 0603

View all 7 components

  • The commercial side of this thing

    ottoragam06/03/2016 at 23:53 0 comments

    I've been working with James Newton, of Linnistepper fame, on an open source commercial low cost closed loop motor conversion system. An encoder board is available for sale, along with a PIC based PID controller.

    The relevant links are here: project

    Purchase here!

    This project may also be of interest to some of you. It is a low cost 40V 18A H bridge based on the same FET driver as my smaller servo drive project, with some easier to cool transistors.

  • Assembled and tested: check

    ottoragam11/23/2015 at 22:56 0 comments

    Everything is working perfectly! I assembled the cable harness for the sensor using a 4 way JST-PH connector and multiwire cable. All the work was done in less than 5 minutes thanks to my recently acquired a cheap crimping tool (you can see the harness in build instruction photos).

    The sensor was mounted on a DC motor in order to provide feedback to a PID servo drive I'm working with.

  • Milled case: check

    ottoragam11/20/2015 at 13:59 2 comments

    The PCB can be mounted using the two holes next to the IC, but I also wanted to have an enclosure for the module for scenarios that demand more protection. I designed and milled a case for the PCB (relevant design files in the repo). I made the base from pine (my plan was to use acetal or polyethylene, but I didn't have any of those materials stocked). The cover is made from clear acrylic, with two of the four bolt holes drilled to accept threading with a 3 mm tap.

    Here's the whole thing assembled. Notice that the IC must be facing the acrylic part.

    Mounting the case onto something like a motor prevents the user from being able to see the alignment LEDs directly. This is where the acrylic cover comes into play, because it allows the light to be seen on the edge of the part.

  • Assembled prototype: check

    ottoragam11/15/2015 at 00:06 0 comments

    The first PCB is ready, have a look at it!

    There are some design choices I would like to point out: rounded corners, separate connector for power/quadrature channels and programming, small size.

    First, the rounded corners are important if one is going to machine an enclosure for this. Milling can only leave an interior corner with a radius as small as the endmill one's using.

    With respect to the connectors, it is useful to have few signals in it. Instead of using the typical piece of ribbon cable for the signals, I planned to fabricate my own cable, using a JST-PH connector and shielded multiconductor cable for improved flexibility and noise inmunity. Good flexible multiconductor cable could get expensive, so it's better to use as few conductors as possible, leaving the signals that are not used all the time (i. e. spi programming header) on a secondary connector.

    Many of the AS5040 projects out there that I found are more focused on being a breakout board for experimenting with this chip, and thus they're big, include things that are not needed for standalone operation, or are not really a finished product, and that's why I decided (also, The Square Inch Project) to create this board.

View all 4 project logs

  • 1
    Step 1

    Glue the magnet onto the rotating object to be measured. Be sure it is centered. Epoxy is useful for this step.

  • 2
    Step 2

    Place the PCB near the magnet. When the sensor is too far away from the magnet or not alignet with it, the onboard LEDs will be lit. Move the sensor around a bit until the LEDs turn off.

  • 3
    Step 3

    When the LEDs turn off, rotate the object with the magnet. If you see the LEDs flash it means that the assembly is still not fully aligned. Adjust until you can rotate the magnet without seeing the LEDs flash.

View all 3 instructions

Enjoy this project?



ekaggrat singh kalsi wrote 12/19/2020 at 22:58 point

i am looking for a way to connect 3 of these sensors to a single arduino . Is it possible ? i cant seem to find anywhere that how multiple seonsors can be used on a single mcu. 

  Are you sure? yes | no

James Newton wrote 02/09/2016 at 20:14 point

I'm confused as to how you adjust the PCB... with the setup you have in the picture, it looks like it would be a fixed distance from the magnet. Actually... I'm confused as to how it's being held onto the back of the motor. Are there screws that I can't see in the picture? Or is it glued on?

  Are you sure? yes | no

ottoragam wrote 02/09/2016 at 20:44 point

The pcb adjustment is done by moving the thing parallel to the motor's endcap, because the magnet needs to be centered relative to the encoder IC.

In this case I glued the enclosure to the motor. You could drill and tap some holes in the endcap, if it's more convenient, to hold the enclosure (it has those two extra holes for that) or to hold the PCB directly. You could also glue some standoffs

  Are you sure? yes | no

James Newton wrote 02/12/2016 at 20:19 point

Excellent! I'm thinking about all the different ways to mount it mechanically...

  Are you sure? yes | no

alpha_ninja wrote 12/07/2015 at 01:06 point

[verified: no design files missing]

  Are you sure? yes | no

alpha_ninja wrote 12/02/2015 at 00:44 point

This is your one-week reminder to upload design documents:

  Are you sure? yes | no

Boris van Galvin wrote 11/15/2015 at 01:11 point

Hi ottoragam, here is a quick video of the shaft. 

The shaft now has an optical encoder strip on the side and works OK however i had contemplated replacing this with a magnetic strip, the same type of  magnetic sheet that is used for magnetic signs on cars set.  At the moment  the optical encoder gives me about 3mm of resolution and is accurate enough for this project. 

The shaft must be easy to swap in and out.

  Are you sure? yes | no

ottoragam wrote 11/15/2015 at 02:29 point

What resolution does the encoder you're using there have? If you want more resolution and want to keep using a rotary encoder maybe you could consider using the AS5045B. It's the higher resolution (12-bit) brother of the AS5040 and it's pin compatible with it.

  Are you sure? yes | no

Boris van Galvin wrote 11/15/2015 at 03:03 point

I only need about 3mm or so resolution. just needs to sense if the shaft jamms and does not move to the target that is set, either continue to try or reverse back to the zero point. 

at present the encoder strip is glued to the other side of the shaft and im using an optical pololu wheel encoder set so it can be used in a linear manner. 

The reason I can't use the encoder as a rotary is the friction drive from the motor to the shaft may slip so the sensor on the shaft was the best option. 

  Are you sure? yes | no

ottoragam wrote 11/15/2015 at 03:41 point

I don't know why the reply button ios missing from your most recent comment.

Regarding the encoder, I don't know the arrangement of the hall effect sensors inside the AS5040, so I can't say if using a magnetic strip with this particular sensor will work, as it is designed for rotary sensing. Maybe you're better off by using the AMS sensors specifically designed for linear motion. It also occurs to me that you could place a non powered roller contacting the bar, but this one covered in something compliant that has a high coefficient of friction, like poliurethane rubber, and mount it very close to the bar, so it presses hard against it, insuring no slippage. Then you can istall the rotary encoder on this extra roller and use it to determine the linear displacement.

  Are you sure? yes | no

Boris van Galvin wrote 11/14/2015 at 22:40 point

Looking forward on seeing more of this, would be interested to see if there was a way to convert it to sense linear motion as that is something that is a real pain in the rear. optical sensors seem to be the only option at the moment. 

  Are you sure? yes | no

ottoragam wrote 11/15/2015 at 00:01 point

Hi Boris, this could be used to measure linear motion, by attaching
it to a rolling element (e.g. a ball bearing) coupled to the object that
performs the motion. You could also check the AS5311 sensor, it is
somewhat analogous to the one I'm using here, but the downside is tht it
requires a magnetic strip to work.
May I ask how are you planning to use it?

  Are you sure? yes | no

Similar Projects

Does this project spark your interest?

Become a member to follow this project and never miss any updates