Speed Controllers for Cheap Robots, Part 1: Shaft Encoders





Introduction: Speed Controllers for Cheap Robots, Part 1: Shaft Encoders

About: I build robots out of boxes! I love teaching what I've learned and seeing people add their own ideas to what they've learned. Nothing excites me more than seeing a student really take an idea and run with it!

Can you tell why I've been excited for this project? Do you know how seriously cool this is?

"No, Jay," I can hear you say, sighing as you fortify yourself for another excited explanation. "Why don't you explain it to us?"

Okay, let me back up and calm down a bit.

I've had the idea for this project banging around in my head for the better part of a year now, and I've been actively working on it since August. All the I'bles I've been posting recently, Metal Wheels, ExternalInterrupts, as well as my two Debouncingtutorials, have been research I've been doing in preparation for this project!

In addition to that, as luck should have it, a couple of competitions have opened up that would fit this project perfectly! So not only is this the fruition of several months of working, it's also my very first competition submission! So if you like this tutorial, or if you're a fan of the For Cheap Robots series in general, please consider voting for me!

Of course all of this is nowhere near as cool as the project itself! Shaft encoders and speed controllers are complicated things, and building one from scratch (not to mention out of craft supplies) is no simple task, but I promise you, we can make it easy!

Let's get started!

For more Instructables on building cheap robots, please check out the For Cheap Robots collection!

For more things that I've done, you can check out my profile page!

For more info from Digilent or the Digilent Makerspace, check out the Digilent blog!

Step 1: What Are Shaft Encoders?

Simple answer is that they're a type of sensor you can use to tell how fast your robot's wheels are spinning.

As your motor or wheel spins, the encoder will send pulses of high and low signal to your robot. These pulses get faster or slower, depending on how fast your motor is spinning. By timing the pulses, your robot knows exactly how fast the motor is spinning, and doesn't have to guess!

This is super important if you want to make very precise movements, and they're used all over the place. In fact, most of the motors in your printer at home will have a shaft encoder.

Problem is, they're not cheap!

In order to make such precise measurements, shaft encoders usually have to be very precisely manufactured. Even cheap shaft encoders from robot kits usually rely on specialized magnets and Hall effect sensors. You can build your own for cheap, using infra-red lights, and there are some pretty neat ways you can salvage optical encoders from old hardware, but I wanted to make mine even more accessible than that.

Step 2: But... Can We Actually Build One From Craft Supplies?


Here's what you'll need!

  • Hammer
  • Thumb tack, or thin nail (I'm using a finishing nail)
  • Phillips screw driver you can abuse a bit
  • Sand paper or wire brush. I use a nail file because they're common and work really well!
  • Scrap wood or a cutting board you can hammer into
  • A gearmotor
  • A pair of pliers, to hold the thumb tack or nail

These supplies are for the Metal Wheels tutorial, which this tutorial uses.

You're also going to need the following:

  • Scissors or an x-acto knife
  • Electrical tape. Other tape will work, but electrical tape is the best.
  • Pliers
  • Wire cutter
  • Wire stripper
  • Four lengths of wire (preferably colored)
  • Header pins (optional)
  • Soldering Iron
  • Hot glue gun (as always)
  • A small, thin spring. You can find these inside retractable ball-point pens.

That's everything for the shaft encoder itself! I'm going to cover wiring in the next tutorial, when I explain how to actually use this thing.

Step 3: Make Your Wheel

Before you get started on the Metal Wheels tutorial, you're going to want to sand their rims. This is actually a very important step, and can take a while to get right.

Even bottle caps like the Snapple caps I'm using have a thin layer of clear paint on them to prevent rusting. Check out the second picture in this step. Can you see how the area I sanded is a little darker? You're going to want to sand until the entire rim is nicely sanded like this. I recommend pulling up a video on YouTube and spending some time carefully sanding.

After that, use my Metal Wheels for Cheap Robots tutorial to complete your wheels! For this tutorial, don't worry about gluing the completed wheel onto your motor shaft, as we'll be doing some more modifications to the wheel before we're done.

Step 4: Cut Some Tape Pieces

There are two ways to cut tape for this step.

My preferred method is to lay a short strip of tape out on my cutting board, and use an x-acto knife to cut pieces.

If you're not comfortable with that (or if your parent's won't let you use a knife) you can just cut the pieces with a pair of scissors.

Each piece of tape should only be about 1/4 of an inch long (or 6 millimeters if you're using a more sensible unit system). Cut three or four of them. I prefer four, but it's a little harder to fit them all on my wheel that way.

For this step, the length of each piece isn't as important as the width, which should just be the width of whatever tape you're using. This is good, because this means that the width of each piece of tape should be exactly the same.

Step 5: Stick the Tape on Your Wheel

Once you have your tape pieces, carefully stick them to the rim of your wheel. Try to space them evenly, but this isn't very important, so you can just eyeball it.

What is important is to make sure there's a nice, large section of bare metal between each strip of tape. This will make sure that our encoder sensor makes contact with the conductive wheel in between strips of tape.

It's also important to make sure your strips of tape are fairly straight. That's because our encoder will be measuring the width of each strip. (This is also why it was important that they all be the same width.)

Step 6: Sizing the Contact Spring

This is probably the most fiddly step in this whole process. If you're very young, you may want to get an adult to do it for you, because it takes some very fine manual dexterity. If you do want to do it yourself, take your time and be careful to do it right. I know I've gone through several springs myself trying to get this step right.

Slip your wheel onto your motor, and use this to get an idea of how high your contact spring will have to reach (like in the first picture).

(I'd like to point out that in the first picture in this step, I'm not actually compressing the spring with my finger. I'm only holding it so you can get a good view of the spring in relation to the wheel.)

Check out the second picture in this step. See how my spring has a slightly acute angle? My goal is to make sure the spring bends down slightly to touch the rim of the wheel. Furthermore, I make sure to bend my spring slightly too far, that way the tip will press gently against the edge of the wheel. This means that the spring will always be in contact with the wheel (even if the hole I punched previously is slightly off-center).

Once you have a good idea of where to bend your spring, use your pliers to make a sharp bend there. You may want to use two pliers to get a really precise bend for this part.

Step 7: Soldering Wire Connectors

Take one end of one of your wires, and slip it into the base of your contact spring. To do this, slip the wire in between two coils of the spring and rotate until it's pinched between the flat coils at the end. If you do it right, the spring itself will hold your wire while you solder it. I've found that a very hot solder works well here.

If you have them, you can also use this as a moment to solder header pins onto the other end of your wires. I needed to do this because I was using stranded wires that were too thick to fit neatly into my breadboard.

Finally, I glued the other of my two wires directly onto the metal case of my motor. This is important, because the motor case is electrically connected to the metal shaft, which will also be connected to our metal wheel! At the same time, the wheel, shaft, and case are electrically insulated from the motor's inner-workings. This means that our motor case gives us a fixed and constant connection to our metal wheel!

Step 8: Glue the Contact Spring Onto Your Motor

Start by putting a strip of insulating electrical tape over your motor case. We've just connected one wire directly to the case, so we want to insulate our contact spring from that circuit. That way, the connection is only completed when the spring touches one of the bare metal parts of the wheel. See how our encoder is going to work now?

Before we glue anything, we want to get a good idea where our spring is going to go. Slip the wheel back onto our motor, and just hold the contact spring in place. Notice how, in the second picture, the tip of the spring contacts the wheel in the middle of the strip of tape? Remember where the base of the contact spring is going to go, because you'll want to put it back there next step.

Remove the wheel, because this next step is going to happen very quickly and we don't want it getting in the way.

Put a generous amount of glue on the motor case (over the taped-up section). Then, using a pair of pliers to grip the wire connected to the base of the contact spring (as in picture 3) firmly place the contact spring into the glue blob. Hold it steady while it cools.

Once the glue cools, slip the wheel back on and check the spring contact point again. As you can see in picture 4, mine was a little too close to one edge of the tape for my comfort, so I made a small adjustment to the spring by stretching it out. (You can also use your pliers to bend the spring down slightly if the spring does not keep contact with the rim of the wheel.) Be very careful if you're doing this, because you don't want to ruin the spring and have to start over from scratch!

Once you're happy with how your spring contacts the wheel, try hooking your motor up to a battery. That way you can watch the wheel spinning beneath the spring. Keep an eye on the tip of the spring and make sure it always contacts the tape as it passes.

Step 9: And You're Done!

That's our completed shaft encoder!

As you've probably guessed, it acts essentially like a switch. As the wheel turns, the tape breaks the connection between our two wires, and that's what we'll be sensing. Because we know that each strip of tape is exactly the same length, we can measure how long each break lasts, and that tells us our motor speed!

Neat right?

But we're not quite finished! Up next I'll cover how to use a simple PID to control the speed of this motor. Because this is made with odds and ends you can find around the house, we can't just use it and expect it to work perfectly right away. There's a little bit of signal processing that we need to apply, similar to what I cover in my DebouncingInterrupt tutorials.

Once again, this is my first entrance into a competition, so I hope you'll consider voting for me! It's also part 1 of a pair of I'bles that complete this project, so please read my PID tutorial when that comes out!

Thank you very much and I hope you enjoyed this!



    • Water Contest

      Water Contest
    • Creative Misuse Contest

      Creative Misuse Contest
    • Clocks Contest

      Clocks Contest

    27 Discussions

    Ha! You got my vote simply for channeling Professor Branestawm :) I love the ingenuity, simplicity and cheapness of it. Too many of these instructables are too hard to get into if you are a beginner. Love the add-on comments too which make this a useful resource that I will be bookmarking for the future. This would make a really good school project.

    1 reply

    I had never heard of Professor Branestawm before! I'll have to check that out!

    Also thanks for voting for me. I'm glad you liked it!

    The quadrature encoder was a wonderful invention based upon years of research: noise resistant, robust handling of direction, simple software and dirt cheap. Why start over throwing all that away? Did you know about quad encoders before you did this? Is this somehow better that a quad encoder? Doesn't look like it but maybe I'm missing something.

    8 replies

    Actually no I didn't know about quadrature encoders before this. I understood the basic concept behind encoders, but I hadn't considered rotation direction because of how I planned to use these encoders. If you check out my Motor Controller tutorial, you'll see that so far my motors only spin one way. Later on I'll probably make an H-bridge tutorial, but I'm in no rush. Last time I used an H-bridge, I shorted it out and it ended up glowing like a Christmas tree before making my battery explode!

    To answer your question, however, the reason I'm not using commercial encoders is that I'm specifically focusing on using craft supplies for as much of my robots as possible. This is partially due to cost, but it's also an issue of availability. When I was a kid, I didn't think I had the money for robotics kits, but I certainly didn't have the patience to order specialized components, nor did I have the confidence or the education to know which components I needed. The For Cheap Robots series is inspired by that. I figure that if kids (or anyone new to robotics) can make their robots out of craft supplies that they already have around the house, then it'll be more accessible to them.

    Does that make sense?

    Anyway. Thanks for the feedback, and definitely thank you for cluing me into quadrature encoders!

    You speak as if there are two kinds of encoders, your cheap one and all others that are "commercial and expensive." I'm not saying use a commercial quad encoder. I'm saying make your own quad encoder with your own methods and craft supplies. I've made them and the reason I did was I couldn't afford a commercial encoder. I liked the quad encoder because it would give me guaranteed position over a long time (lots of rotations, stops and starts) with motion in both directions AND I could make it myself from found parts. AND it was simple to make. What I'm saying is you could have made a cheap quadrature encoder out of craft supplies. You could make one with your method with no problem. You should consider doing so for one of your future Instructables. What makes it a quadrature encoder is 2 sensors with one of them ahead of the other so they don't see the same edge at the same time. As simple as that. A fancy name for a simple concept. There are two optical quad encoders in ball style computer mice, i.e. before the optical ones. That is where most people scavenge them. I'd be surprised if there wasn't an Instructable about that. Who doesn't have a dead mouse laying around somewhere. Consider what happens with your sensor if the shaft stops with the sensor right on the edge of the tape. Every little jiggle will cause edges (on off on off...) which will confuse your sensor software. A quad encoder will not be confused. Each edge will be recognized for the right direction. Add a second sensor to your design and the problem goes away. Instant quad encoder. Two springs instead of one. The distance between them should be about half the width of tape and gaps so one hits an edge while the other is halfway between edges. That's it. No high precision manufacturing, no expensive parts. This design you can run in both directions and at any speeds (as long as the processor can keep up with edges) with stops and starts and it always knows exactly where it is and how fast it is moving. And there is a fantastic open source library available which handles the edges (with interrupts) and will keep a position count in a memory variable where the rest of the software can use it. Cheap enough for you? ;-) And it is accurate enough to drive a CNC machine or 3D printer without needing stepper motors. It would be a great addition to a "Cheap Robots" series. Why shouldn't cheap robots be accurate?

    That being said my preference is to make an optical version. I suspect the spring will not be reliable. It is hard to make a spring tough good enough for reliable contact but also low friction. I prefer to use the interrupting optical switch. They are shaped like a tiny "U" or slot with an infrared LED on one side and a sensor on he other. On the shaft I put a cardboard disk that has slots cut out of the edge cut with Xacto knife or razor blade. The optical switches are so cheap most places don't sell one at a time. I got a pack of 10 for around $5.00. You need two but that still seemed pretty cheap to me. I also usually mount the encoder disk on a robot wheel instead of the motor. That way even if the connection slips because the robot jams I am still measuring accurate wheel position. Here is a sketch of the disk with cutouts showing in red where to put the sensors, one on the middle, the other on the edge. Obviously more slots means more accuracy.

    encoder disc.png

    Oh! I understand! I think we're in violent agreement actually.

    Earlier on somebody suggested adding a second spring, which I thought was a great idea! I was skimming the Wiki page on shaft encoders, which includes a couple different techniques as well, but I haven't had a chance to really read it yet. I do want to try applying some of these ideas. I didn't mean to give you the wrong impression. Sorry about that!

    As for mouse wheel encoders, I've found this tutorial before, but it just salvages the sensors from a mouse. It doesn't use the disk. I've also salvaged a ton of encoders (rotary and linear) from old printers, and I'd like to do a tutorial on those as well.

    Of course, I've also considered using light sensors to make an optical encoder. In fact you're absolutely right, the spring gives me a very noisy signal, but I'll show you how I get around that with little bit of math and programming. I opted for a spring because I wanted to go for the most readily available part, and while light sensors are plentiful and cheap, pen springs are even more so.

    Again, I'd like to do an optical design as well, but I'm finding there are so many different ways I could branch out from here that I'm worried about getting bogged down with encoders.

    Thanks for the feedback!

    Sorry. My fault. I was so surprised it wasn't quadrature that I didn't make myself clear. I also confused the issue by using the technical name "quadrature encoder". That name baffles most people who stumble on it and makes it sound more complicated (or expensive) than it is. Who the heck knows what quadrature means. It comes from the math background of the inventor. I should have just called it a two sensor encoder. Also the usual "90 degrees apart" comment in most quad detector tutorials is misleading. It is 90 degrees apart in signal phase (another math thing!), not 90 degrees in the real world. It is only 90 degree physical if there is just one notch, i.e. the tape covers half the circle so a complete circle is one pulse, half the time on and half off. Instead of 90 degrees I just say that the distance between the two sensors needs to be "half the width" of the smallest on or off time (i.e. half the width of the tape or gaps in your sensor.) A single sensor encoder works perfectly well for servo-ing motor speed which is an important application but two sensors are usually needed for an encoder to accurately get position and maintain the position count when stopped.

    This is important because you don't know which application people who stumble on this Instructable will need it for. Most people don't even know there are two kinds of shaft encoders. They often think all encoder designs are the same so they are all good for figuring position. They would use your Instructable and be frustrated. It might be worth adding a sentence or two to the preamble to make this clear. This entire subject (different strategies for shaft encoding) is probably worth an Instructable in its own right. Knowing which to use is a pretty basic thing most people don't know and many suffer because of it.

    If you can't tell I am sensitive about it because I blew it the first time I tried. My very first embedded application was to drive a pen plotter, which needs position accuracy. I didn't know then about the quad encoder strategy and was forced to switch to steppers which are much more expensive, hundreds of dollars more in those days. Steppers were expensive.

    The good news is that with two sensors you don't need to do all the filtering math. A noise glitch just becomes an up tick followed by a down tick. Lots of glitches in a row are just sequences of ups and downs and the count is never off by more than one. If you want to go fast or measure small angles (lots of small pieces of tape around the edge or lots of notches in the disk) it is easy to swamp the software if there are any calculations. Some people use two sensors just to speed up the software even if they don't need position.

    My vote for the best single low cost encoder design for a "Cheap Robots" series (You already got my contest vote on this. Using pen springs was clever.) is to stick with the pen springs but use two of them. That is still cheap and needs simpler software; plus it covers all use cases so the user doesn't have to worry about it. One shaft encoder strategy "to rule them all." ;-)

    I know I've said too much already but the sensors don't actually need to be next to each other if there are multiple notches. I put them on opposite sides with one of them shifted half a notch from exact opposite. In other words I put the sensors on different notches. If the notches are roughly the same size the result is the same. I do that because it is easier to build if the sensors (springs) are further apart.

    By the way your design is perfect if you are just using the encoder to measure and control speed. One sensor is fine for that. You only need a quad encoder to accurately measure position (actually angle.)

    if you read his prolog he states that for him they are expensive.

    Please read through the complete thread. He said no such thing. He said commercial encoders are too expensive. I agree. That's why I make cheap quadrature encoders which are similar to the ones this instructable (and just as cheap) but have the advantage of being error free and can be used for robot tracking.

    I found this great, it remembers me the time I with a friend build things from scratch, like every kid, saving money as like buying nothing was really important for us to make our projects. Good luck for the contest!

    1 reply

    Thanks! I hope you voted for me! ;)

    With this design, you have no feedback about what direction the encoder is actually moving. You can infer this from what you are telling the motor, but this isn't always accurate. Instead, if you have one strip of tape go half way around the wheel, and place another contact 90 degrees from the first, you now have feedback about the actual direction the wheel is moving.

    6 replies

    In order to have direction of rotation (independent of context) we need one more wire. With two spring attached to the wheel we can do a lot of additional things. Oh yes ... but here we are talking about a proof of concept: using recycled material to save our money. Congratulations for Jay.


    1. The way you attached the spring to the motor is not entirely clear for me (probably a language barrier) but I understand the concept.

    2. The program is a little too advanced for me to understand. Perhaps, most of it is not critical for the first try.

    You've got it spot on. This whole project is about using craft supplies to build robots, and not expensive specialized components. The idea is to make a robot tutorial that is more accessible to kids and new learners, by using components that they probably have lying around the house.

    Thanks for letting me know where you got confused. I'm reviewing that step right now, and it is a little wordy, but I'm not sure how I could make it more clear. If you think it's a language barrier problem, could you let me know where specifically you get lost? (You can privately message me on my profile page. That way we don't clutter up the comments.)

    I also am not sure what you meant by "the program is a little too advanced". Are you talking about the code for the robot to use this encoder? If that's the case, I specifically haven't included any code in this particular tutorial because I want to spend an entirely separate tutorial just to explain that.

    Thanks for all the feedback!

    This is how work conventional encoders. Here, as you don't need accuracy, you count the number of turns after all, you can use different sizes of tape and bare metal, thus you can determine the rotation direction.

    "different sizes" is a VERY good idea. Thus we can easily zero the wheel and even have active breaks. For example (%perimeter): 10,20,30,40 pairs of half conductive and half insulated. I have an idea how to make it precise without special tools or skills :)

    Those are both neat ideas! I'll have to try them some time.

    Rafununu. In my next I'ble I'll be demonstrating how I use this encoder, and I do time each individual length of tape. I'll admit that my accuracy is only about ten milliseconds (if that) but it works well enough and this way I get more updates per rotation.

    it sounds like you are using a loop to continually check the value of an analog input. In addition, you can set up a trigger function that is called if the analog value drops or rises. in this trigger function you can update the tick count. this would be much faster than checking it in your update loop. it's also easier code to read.