Let's Make Robots!


Gives feedback to your robot brain...

Encoders, Encoders...

Encoders can be used to provide feedback info to your pic to let it know what the mechanics of the machine are doing. In the case of this post, I will be assuming you want to know how far your wheels have gone.

The first guy to look at is this one:


This sensor can also be found here in the componants section.

It is also found at SparkFun Electronics for  just $1.13! --These little guys are indeed not only tiny and cheap, but work great for encoding and also line follow applications. If you check out Sparkfun, and other supply houses, you will find this light/sensor set-up comes in a lot of different configurations. Note: the LED portion of this sensor will require a 330R resistor and the input pin should be pulled down with a 10k resistor to gnd (if it isn't allready pulled down on the board itself)


Next is this guy:


These work great when you can straddle a wheel or gear. Again, there is an IR light on one side and a sensor on the other. The same as above, when the sensor sees the light, it allows a high signal to pass through to a digital input pin on your pic. These are also available from Sparkfun and they do offer a PC Board for it. The sensor is just a couple bucks and the board is a dollar. Again, the LED requires a 330R resistor and space for one is included on the PC Board. 


Another cool encoder is a rotary unit:


First and Foremost, BaseOverApex has done a nifty post about these in the componant section.  I personally have never played with these and I actually have a couple on order right now. I ordered these from Digikey.   I am going to shy away from adding anyother info here as Mr. BOA has allready done so in his componant post.



One thing I found when starting to work with these was this: Figure you have white and black stripes painted on a spinning wheel. The sensor can tell the diference between them. However, your code is cycling faster than the wheel is spinning! Thus, your code could cycle through even a few times while the sensor is still on a white space thus counting it more than once. The solution is simple. The code is basically if the space WAS black and is NOW white then count it. It is not really counting white spaces as it is counting changes from black to white. Here is the basic code I use to do testing:


let b9=0 'start the count at 0

high portc 1 '**Change this to make your motor go
low 1 '** Change this to make your motor go
if pin5=0 then let b8=0 ''if the sensor sees black, remember that (b8 is the "remember" variable)
end if
if pin5=1 and b8=0 then gosub countone 'if the sensor sees white AND it used to be black, count it
goto main

let b9=b9+1 'count one unit
let b8=1 'switch the "remember" variable to "i'm seeing white now, untill I see black again
if b9=8 then gosub onerev 'if we have counted what we want go somewhere else

low portc 1 ' **Change this to make your motor stop
low 1 ' **Change this to make your motor stop
pause 2000 'Wait a bit
let b9=0 'Clear the count back to 0
goto main 'Do it all again



Comment viewing options

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

Great info ! Just one thing, how do you get those white marks on the wheels with such precision?   
I made a few encoders for a robot in a class project this semester and I just used a protractor to evenly measure my angles. I used a marker to make the black on a white wheel.
Seems simple, thanks, will try it. 
Nope, it clicks to high. To be honest, I don't really know which way is better, but the way I have it now is 5v to the collector, 5v (Through a 330R) to the led and the emiter going to the input pin with a 10K pull-down to ground. I am using this set-up on both the little sensors and the "U" shaped one with great results. Is there any advantage to having it click to low instead of high?

I checked over the datasheet for the QRD1114 to see if there was any application info, but didn't find it directly. The only thing I can point to is that the phototrans is marked as an NPN device, arrow pointing out, which generally means it should "sink" better than it "sources" current. There are also other examples of similar sensors being used with a pull-up (page 3 schematic).

On the LED, it would be possible to use a 180 or 150 ohm resistor, to get the current up closer to the 20 mA spec'd. There is probably about 10 mA going through the LED now with the 330 ohm, which saves batteries but decreases the light detectable for the sensor.

All that being said, if it is working well currently, you don't really need to change anything. If you get a glitch or 2, these might be some things to help them work better.

Hey CtC, don't you mean the phototrans collector should be pulled up to 5v with a 10k or better? So that when it turns on from getting a reflection from the LED, it switches the output attached to the collector from high to low? Think you said it right in talking about the code usage, but typed the hook-up different above.

Awesome info on encoders.


It would be neat to do a little bit on each part that can be used in robotics, as a lot of aspiring builders know some about one thing, but not about the other.

A huge undertaking, I know, but it could be made as you go along, with each type of component.

robotwiki here we come?

 32 holes to be exact you say?


I was wondering about how the robot collects information like that, I'm still a wee little newb to these here robots and I still understood :D



Putting your finger in that socket is NOT a good idea. Oh?

Well thank you Captain Obvious!

Handy!  I can see a lot of applications for these.  Pretty cheap from SparkFun, too.