From RepRap
Jump to: navigation, search

-- Main.AdrianBowyer - 17 May 2006

Magnetic Encoder DC Digital Servomotor

1. Characteristics

This device seems to work reasonably reliably.

Its bad point is:

  • It overshoots and oscillates about the final set position when set to move at high speeds. Though this settles quickly.

Its good points are:

  • It's cheap,
  • It gives high torque for low (100mA) current, and
  • It's accurate.

I don't think it's quite ready yet to replace a stepper motor, but it may well be possible to make it good enough just by improving the control algorithm in the PIC - that is, with no hardware modifications.

It is certainly a good way of driving general devices with rotary feedback, and (I have yet to try this) it should allow rotation speeds to be precisely set and not to change with load.

2. Introduction

Vik has been rabbiting on about the magnetic rotary encoder chips from Austria Microsystems for ages, and so I finally got round to trying them out. They only cost a couple of quid, after all.

Depend on it, that man Vik knows his onions. The encoders are beaut!

I rapid prototyped a holder for a small piece of stripboard to go on the back of one of the geared motors that I used for the Mark II Extruder (see that for a link to the supplier), soldered one to the board, and put the lot together:


A small magnet is mounted on the motor's shaft, and Hall-effect sensors in the chip encode the rotating magnetic field. Here's the scope trace:


There are two outputs which give quadrature (i.e. the phase tells you if the thing is going clockwise or anticlockwise).

The chip I used has a resolution of 256 steps per revolution, but the company makes them up to 4096 :-)

The whole thing should work very well with Simon's DC motor controller (Simon - is that Wiki-ed? Can't find the link. - A.)

3. Mechanical design

First, a simple modification had to be made to the motor's gearbox because it only has a drive shaft coming out one side. A shaft had to be led out on the oposite side to put the encoder on.

Take the gearbox apart (two self-tapping screws and a plastic clip). Remove the final drive, which is a pink plastic part that engages with the final gear. This is mounted on a 2mm diameter steel shaft. Throw away the shaft, and replace it with a longer one of the same diameter:


Before fitting the new shaft thoroughly clean and degrease the hole in the pink drive, then put a drop of cyanoacrylate (super) glue in it to hold the shaft firm.

Then use a 2mm drill to drill through the blind hole that the original shaft fitted in:

Scrupulously clean away any swarf. Re-grease (use light silicone grease) and re-assemble the whole gearbox (you did note-down/phone-camera which gear went where, didn't you?) then use a thin grinding disc in a minidrill to cut off the new shaft so it protrudes 4mm from the yellow back of the gearbox. Hold the thing upside-down when you do this so swarf falls away and does not get in the works.

Here are the rapid-prototyped parts:


They are in the AoI file that you can download from the bottom of this document; the two corresponding STL files are there too.

The cylindrical part in the middle holds the magnet and fits on the shaft that you have just added to the gearbox. No glue should be needed - it should be a tight fit, and it experiences negligible torque in operation.

The larger part holds the stripboard for the electronics, and attaches using two 30 mm M3 cap screws to the holes already provided in the motor's gearbox. You may have to run a 3mm drill down these first; in my experience they tend to be a shade undersized.

4. Electronics

If you can, buy the encoder chips and magnets from 2001 Electronic Components Ltd as they have been very helpful to the RepRap project.

I made the electronics on a piece of 4 x 11 hole 2.54 mm stripboard. Everything mounts on the copper side; the other side has to be flush for the magnet to turn against it.

Here is the circuit (the mag-servo.tgz file below contains the files for Kicad) for the entire thing including a controlling PIC.


The Zener, resistor, and Darlington top right allow the voltage used to drive the motor to be varied, but the software also allows that using PWM so, for most cases, those three components can be left out and pin 8 on IC4 can be connected straight to 12v.

The 4x11 stripboard contains IC2, C1, C2 and a 4-way connector for +5v, ground and the A and B signals out of the encoder chip. Use the extreme tracks for power and the middle ones for A and B. Break the A and B tracks half way along the board and solder the non-ground end of C1 to one of the new nodes thus created.

Take care when you do all this that nothing pokes through to the other side of the board - that needs to stay flush.

Start by soldering on the connector and the two capacitors.

Do the next bit on an earthed anti-static mat; the AS5035 is a CMOS device. Mark pin 1 of the AS5035 on its back (I used tippex - the white mark on the photos) and glue it upside-down onto the copper tracks using cyanoacrylate. Be very careful to get the chip square and symmetrical - I did this by eye using a stand magnifier and tweezers - and have pin 1 next to the +5v rail and on the side of the 4-pin connector.

Now the fiddly bit - you have to solder up some very thin wire to the chip pins that need to be connected and to the appropraite tracks on the stripboard. Again use the magnifier and tweezers, and also a very fine-tipped soldering iron. Here's a close-up:


Note the central disc mark on the chip - that's where the Hall-effect sensor ring is inside, and you are going to need to align that in a minute.

For more hints on hand soldering surface-mount chips see this link.

5. Assembly and preliminary testing

Here are the components part dis-assembled. You can see the magnet and its holder on the shaft I extended.

The stripboard has been glued to the rapid prototype support here, and that is the job to do next.


Push the magnet into its holder on the motor shaft. Bolt the circuit support onto the motor.

Note that the circuit support has four indentations in the top. These are aligned with the shaft centre.

Clamp the motor gearbox in a vice, place a magnifying glass above it, and place the stripboard in roughly the right place; that is, with the disc marked on the chip aligned with the centre marks.

Hold a straight-edge in mid-air between the device and the lens and move it and your head to line it up with the marks on the support with no paralax. Move the circuit to centre the disc. Do this in both directions at right angles.

When you are happy with the position, drop one drop of cyanoacrylate next to the circuit board without touching it with the glue nozzle, but so that the glue soaks between the circuit and the support by capillary action.

Wait until the glue has set. Then run round the rest of the circuit gluing it down more firmly. Take care not to get any glue on the magnet and the rapid prototyped holder for it.

The AS5035 specification says that the chip needs to be aligned within 0.1mm of the centre of the shaft that is driving it, but that its precision degrades gracefully if this is not quite achieved. By just aligning it by eye, I found that it gave a strong regular output.

To test the device connect it to a 5v supply, and connect the A and B outputs of the AS5035 to a scope. Set the motor running and you should get a square wave of the type shown above. Reversing the motor should reverse the phase relationship.

If you feed the A and B outputs into the S and R pins of a set/reset latch its Q output will be a count-up signal and its not-Q output will be count down. Or you can get the PIC to sort all this out by having A and B generate interrupts and getting each interrupt routine to examine the other signal to decide whether to count or not.

Now solder the rest of the circuit onto another piece of stripboard. Mount a 4-way socket on it to plug the small stripboard into, and check the layout to make sure that the motor and its attached encoder do not hit anything when you plug it in.

Take care when plugging and unplugging it - remember that the small stripboard is only held on by glue, so don't strain that.

Finally solder a 100nF capacitor across the motor terminals (not shown on the circuit above) to reduce electrical noise.

Here is a photograph of the finshed device:


All the files needed for it are in the tarball dc-digital-servo.tgz below.

There are four folders: Mechanics (contains the STL and AoI files), Pix (contains all the images on this page, and the high-resolution ones from which they were derived), Electronics (contains the circuit for Kicad), and Software (contains the firmware for the PIC stand-alone test program).

The firmware was compiled under SDCC 2.5.4 #1183. Note other versions of SDCC may well not work.

6. The test program

The test program communicates along a serial line (you need an adapter like Simon's described here) to a terminal program such as Hyperterminal or Minicom running on a PC at 19200 baud. It supports the following single letter commands ([b] means a byte in hex that you type, [n] means a 4-byte long; <b> and <n> are responses from the machine):

  • b<b> Return the brake interval (number of steps to reverse as approaching target).
  • B[b] Set the brake interval. 03 is a good number.
  • c<n> Return the current position.
  • 0 Set the current position as zero.
  • H Halt the current movement.
  • S[b] Set the required speed. 40 is OK.
  • s<b> Return the current speed.
  • M[b] Set the maximum speed that must not be exceeded. 60 is OK.
  • m<b> Return the maximum speed.
  • t<n> Return the target position.
  • T[n] Set the target position.

The action of setting a target automatically causes the device to seek it. The Brake interval determines how many steps before the target the motor is reversed to halt it quickly; if you set this too big the machine will hunt.

Further reading