Let's Make Robots!

Adding a rotary encoder to a hacked servo

Provides encoder measurements to a geared motor

Recently I hacked some servos for fun. While I had the servos open and had removed all the electronics, it occurred to me that there was a lot of space in there now. I had seen some previous attempts by some on this site to add optical encoders inside gear motors. However, with all the room available inside the servo, I thought a rotary encoder might fit.


  1. Testing the gear motor after adding the encoder
  2. Testinge the encoder with a simple circuit

Some searching found me these rotary encoders from allelectronics.com for only 75 cents each! The size looked like it would fit. I ordered four of them, and they arrived today.

First, opened up my hacked servo, and removed all the gears. This is a GWS S03N STD servo.


You can see the unmodified rotary encoder next to the servo parts. So innocent and unaware of what is about to happen.

In the picture below, you can see the servo bearing in the upper part of the case. The 6mm encoder shaft fits in there perfectly.


Unfortunately, the plastic hole in the center part of the servo case is only about 4mm. I had to drill it out to fit the shaft. Use of a drill press and very careful positioning is highly recommended.

Now the encoder shaft fit the middle of the servo body. However, when I tried to reassemble the gears, I discovered that the middle gear rubbed up against the encoder shaft. I needed 1 or 2 more mm. If you look carefully at the encoder shaft in the picture above, you can see that there is a portion of the shaft that I had to grind away with a dremel tool. I needed to leave some of the 6mm diameter above and below this grinding, because this is where the shaft rides in the enlarged 6mm hole in the plastic body of the servo.

Next I had to cut down the length of the encoder shaft. It has to end up just about flush with the metal bearing when assembled.

In the picture below you can see the encoder in the center servo body piece with the bearing installed.

Next came what might have been the biggest challenge of the whole project. In the picture below, you can see the slotted top of the encoder shaft, and also the slot inside the last driven gear of the servo.

The slot inside the gear is wider, but shorter than the slot in the encoder shaft. I needed a piece of metal to mate these two parts together so the encoder would turn with the gear.

After some thinking, I decided to try to fabricate the part I needed using a left over piece of metal from when I cut down the encoder shaft.

Below is the result.

This took a lot of careful work with the Dremel. Grind a little and test fit. Grind and test fit. It is easier to take away material than it is to put it back. I also widened the slot in the shaft of the encoder.

Below you can see the new piece installed in the encoder shaft, with the bearing around the encoder shaft. The little white plastic bit sitting on the servo body gets placed over the new piece.

Next, the final gear in the servo gear train has to fit on top. There are little plastic tabs inside the gear that match the two indents in the white plastic sleeve that sits on top of the encoder shaft.

In the picture below, you can see the encoder in place along with the servo motor with blue wires attached.

That basically completes the mechanical build. Next, it was time to solder the wires.

The center pin of the encoder is ground, wired in green. The left (yellow) and right (orange) pins are Channel A and Channel B. It doesn't matter too much which is which. The blue wires are for the motor.

I hot glued the encoder firmly in place, since otherwise it will try to turn inside the servo case. I also used some hot glue to serve as a strain relief for all the wires.

Below is the finished product, all ready for testing.

The video shows my first test, which was just to determine if the servo motor and gears still turned OK after all of my hacking and bending. I don't want to give away the ending, but it seemed to work OK.

Next test will be to try out the encoder itself. That will be for another day, and I will update this post then.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

I just burned out a servo. It did like the 2803. I should have read all of the directions. Thanks the the encoder informatiom.

I never got this working cleanly. I might need to integrate a chip with schottky hysteresis to clean up the pulses.

I love cheap solutions!

The resolution is not too important. If you are using this as a drive motor on a robot then you have more than enough resolution for correcting the speed of ther motors so that the robot travels in a straight line. It is also enough to measure how far the robot has traveled.

Low resolution can be an advantage. If you use an interrupt to monitor a high resolution encoder then this can slow down your processor significantly.

A member who wishes to remain anonymous sent me this private email:

I'm too new and too embarrased to post this in public view....... I thought I understood what servos do, but what do you give up by removing the servo logic board, and what do you gain by adding the encoder?

This is a perfectly valid question, so I'm including it in the post, without narc'ing out the person who asked. ; j

A servo is simply a motor and controller that include some position feedback. The controllers for hobby-type servos we work with allow you to command the motor to a specific position, with about 180 degrees range of motion. 

This is quite useful, but there are several common hacks that people do on servos to get different, but also useful features. The most popular is probably the continuous rotation hack. This lets you use your servo as a gear motor with the drive controller already included. You only need one microcontroller pin to command it in forward and reverse, which is great compared to a normal h-bridge driven motor that needs two pins to control.

Sometimes people remove the servo controller board entirely, and just use the servo's motor and gearbox. In this case you still need an external h-bridge or other controller to use the motor, with the typical two output pins required to run it in forward and reverse. However, the servo package is very nice and enclosed with its own gearbox, so this is sometimes done. This is how I started in my original 'Hacked some servos for fun' post.

Now, adding a controller to the hacked servo inside the servo body is a very cool thing. You get the control in a nice servo package, just like the original servo, but with different features. The references on this post to supermodified servos is an example. That provides precision of less than 1 degree.

In my case I added a very cheap rotary encoder. I'll have much less precision (a few degrees), but it costs next to nothing.


The aspect of this code, not referred to in the video, is direction. The red/yellow sequence reverses when the direction reverses.

Count the moments when the yellow light comes on:

if the red light is still off
   you're turning it the same direction as in the video
   you're turning it in the opposite direction

BTW, is that 30 detents per rev per colour?

Yes, rik. Thanks for adding that. I'm not too interested in determinining direction, since I'll be setting the direction. However, there are many applications where this would be helpful.

I may be wrong about the resolution of this encoder. There was very little information provided with it. They stated 30 "detents" per revolution. You can definitely feel the detents as you rotate the shaft. However, some testing revealed that there are multiple transitions per detent.

Measuring by hand, it looks like a 1/4 turn moved through 24--28 state changes. It's challenging to test this way. I need to set up a proper test with a microcontroller doing the counting.

However, it seems very likely that my resoution will be MUCH better than I expected. I think I will have at least 96 state changes per revolution! 

If I would stop from what I'm working on to wonder on how useful it will end up to be, I would have to throw half of my stuff away.

It's a nice project, make another one and put them on something with wheels or tracks. 30 pulses per rotation should be enough to get something to go in a straight line at least.

That is some truly cool stuff, but way above my current budget. Supermodified servos beat my solution hands down as far as capabilities and performance.

You get what you pay for. My solution costs under $1, while a set of Supermodified boards for one servo mod costs over $50.

If the price ever comes down, or if I REALLY need that level of performance, I'd definitely look at the supermodified solution.

I wrote my last message at work with only a few seconds and didn't give proper credit to your servo mod.  I do think it was a cool hack and understand keeping it cheap.  I just wanted to quickly show the other project since it was a very interesting way of doing it.  I think they both have there merits.

Nice job!