Adding a rotary encoder to a hacked servo

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.

Videos:

  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.

https://www.youtube.com/watch?v=jhI2o9lsvbY

Resolution?

Does this kind of encoders have any usable resolution for using with a servo?

I think you are missing the

I think you are missing the point. This is a hacked servo so there is no controlling board inside. Its just a motor with a gear. Now its geared motor with full encoding and that is great stuff.

 

Geir is correct. In addition

Geir is correct. In addition to the fact that the servo board is gone, the encoder is installed where the servo’s potentiometer used to be. There’d be no way to fit an encoder into a standard servo that was still operating as a servo.

You didn’t understand what I am saying…

Well, of course there is no control board, as you can not interface it with the encoder. But I suppose you did not build this to be decorative! I suppose you are going to have the motor driven by an mcu (by means of an H-Bridge) and the encoder connected to the same mcu, to create a driver. What I am saying is that I don’t think you are going to have any usable resolution because these encoders are used to read human input (360 deg / 30 detents =  12 degrees / detent). I hope you get what I mean now…

(With a wheel attached on it you will have a very big error!)

Great stuff! I gave up this

Great stuff! I gave up this idea long ago, when I noticed that servos are noisy (loud) geared motors. I wanted to have the encoder soldered to a small PCB with a ATtiny85 on it connected to a second board with a FAN8082 H-bridge. Besides the noise, I discovered that is harder to keep the speed of 2 motors at the same value if they are connected to separate microcontrollers.

 

Note on the poor resolution of the encoders. You may use a timer to count ticks between clicks. That means you have a timer that generates interrupts every ms or so, and count how many interrupts occur between 2 clicks of the encoder. 

OK, I understand your point

OK, I understand your point now. Yes, you are correct, the resolution will only be 12 degrees, which isn’t great. However, It should be OK for my purposes. With smallish wheels the error will not be too great.

For comparison, SparkFun sells this wheel encoder, which gets 48 pulses per revolution. Not that much better than mine.

Birdmun provided me with this link to Build Your Own Robot, by Karl Lunt. In his book Karl shows how to add an optical (IR) encoder to a servo. He gets 45 pulses per rotation, and discusses add a 2nd encoder to get 90 pulses per rotation.

CtC managed to add an encoder to Walter’s motors, but he got it on the first gear in the gear train instead of the last. This provides MUCH better resolution. I believe he gets 360 pulses per rotation. Not bad.

I’ll post my results when I get to testing.

I disagreeA 12 degree

I disagree

A 12 degree resolution on a built in home made encoder is very good indeed. I challenge anyone to build anything better and fit it inside a servo casing. 

Here is another project

Here is another project similar to this one.

http://www.01mech.com/sites/default/files/docs/SuperModifiedAssemblyGuide.pdf

See in action here: http://www.youtube.com/watch?v=8jajnIBX2cU

-Glen

That is some truly cool

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.

Gray for all you black and white needs

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
else
   you’re turning it in the opposite direction
endif

BTW, is that 30 detents per rev per colour?

Yes, rik. Thanks for adding

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! 

ignoblegnomeI wrote my last

ignoblegnome


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!

-Glen

worse yet: Supermodified[R] is native LMR

You linked externally (I understand work: haste, distractions, pretty ladies, beer, robots perhaps even) where you could have linked to LMR!

Supermodification of hobby servos right here! And all designs are open too! Not for the faint of hearted either.

Thanks for the complement.

Thanks for the complement. It is fine that you provided a link to an alternative. It just makes this post a better source of information in the future. I had heard of supermodified (from the post rik mentions), but your link prompted me to read in more detail. They really are interesting!

A member who wishes to

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.

 

Thanks for the LMR link

Thanks for the LMR link Rik.  I didn’t remember where I saw the link form and just did a google for it.

That is fun

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

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

Did this work :)?

This is a very old thread, but I am stuck in the same situation. Continuous rotation servo motor that I need to measure and control its speed and position. Did this eventually work? I just ordered the same rotartry encoder module (I will have to deal with the board size somehow) but I am worried that it’s a mechanical encoder and can wear out quickly. 

How did it work out for you?