PDA

View Full Version : New Twist on Rotary Encoders...



LeoL
02-21-2003, 10:04 PM
After searching the net for rotary encoder solutions, I decided why buy them when I can make my own at twice the cost;) Hey, it’s a hobby after all. No fun in it if you end up buying everything already made now is there!:D

My solution: I use inexpensive 10K pots. I open them up and remove the stop inside so that they spin freely.

I then connect the pot to the input of an A/D converter of a cheap PIC micro. Programmed the PIC micro to output an up pulse and a down pulse when you turn the pot cw / cww. Resolution is selectable via external jumpers. Up to 3 pots can be connected this way to a PIC micro.

Interfaced this to my home made 64 input USB module with keyboard emulation! I could hook up 32 encoders this way. You could conceivably connect these to an EPIC or Hagstrom module I suppose with some tweaks to the PIC source code.

Simple huh?

Here's my page with all the files you need to make your own including a PCB layout and PIC micro source code and HEX file.

http://www.vif.com/users/leolacava/misc/re-pot16f/re-pot16f.html

Cheers,

-Leo

Captain_Slarty
02-22-2003, 04:09 AM
Nice one Leo
but dont you hit a blank area of the track on the complete revolution ?? ;-)


check out part number 653019 at www.farnell.com

only 2 dollars each brand new !! a 16 cycle rotary endoder, tiny, and a pretty shade of blue lol...

heres a small sample using PicBasicPlus (www.picbasicplus.com)
this was using an 18f442, but would be identical on the 16F84 - I just happened to have the 18f in the development board at the time.
If you havent used PBP, then look closely at this prog !.. notice the print dec 0 ??.. that will output to an lcd display - and I dont even need extra lines in the code if I use the default connectors on portb 2-7.

Remarks On
Device 18F442
'Config WDT_OFF,XT_OSC

DIM Count1 as Byte
dim directn as byte
dim old as byte
dim new as byte
' gray code 2 bit. 00 01 11 10

TrisB=%00000011
Cls
Count1=0
print dec 0

Start: Directn=1
Gosub Enc
If Directn=1 then Count1=Count1+1
If Directn=0 then Count1=Count1-1
Print at 1,1,dec count1," "
Delayms 5
Goto start

' Before entering the main loop, the program stores the beginning
' state of the encoder bits into the variable 'new.'

Enc: Directn=1
old = PortB & %00000011 ' Get Port reading and mask it

Again: new = PortB & %00000011 ' Copy encoder bits to new.
If new = old then Goto again endif ' If no change, try again.
old = old >> 1 ' shift right 1 bit
new=new AND %00000001

directn = old ^ new ' XOR right bit of new w/ left bit of old.
Return

As you can see, PBP is excellent, and easy to read and program.
If you have difficulty getting the enoders (which you shouldnt have) I could get them in the uk and send them to you ?.. providing you are not in the UK in the first place lol.

Joe.

http://homepage.ntlworld.com/captainslarty/Images/coolskul.gif

System - >
3gig Intel + .. Size DOES matter

LeoL
02-22-2003, 10:18 AM
Hey thanks for your input Joe.

First off, yeah there is indeed a gap but for testing purposes it works just fine. It's actually not so bad when you spin the knob, it's not that noticeable. I am currently waiting for my full turn (endless) pots to come in so these will do for now.

As for the loopback (start-end, end-start), that's taken care of in code.

Thanks for the link to farnell encoder. I will check it out. As far as price goes, the PIC 16F676 is also about 2 bucks and pots are...well...practically free if you're a hobbyist.;)

Actually, my main reason for using pots is because there is a far greater variety in application then there are with pure rotaries. For example, the local electronic warehouse I often go to has an isle completely dedicated to pots. Many of these are surplus types from commercial products such as stereos, appliances and what not including back lit (for car stereo systems), concentric (vol / bal), and for lack of a better term, notched, as in you feel clicks when you turn them! Sadly they don't carry full turn ones and I have had to special order these.

It was primarily for this reason I decided pots would offer more flexibility that typical encoders.

Surprisingly the PIC's A/D (10-bit) is very accurate and I have been able to get up to 40 steps per turn! Higher counts are likely possible, but I haven't tried it and there's no need to, 40 is plenty. The limitation in accuracy turned out to be USB since it polls the devices only every 10 ms. You end up loosing some steps (1 or 2 per turn).

Thanks for the link to PicBasicPlus, but I've already bookmarked it from one of your previous posts. This was my first time programming PICs with only 35 instructions and I felt like I was typing with one hand tied behind my back!

I will indeed look into PicBasic! I had actually started looking into PIC C compilers since mpasm was starting to drive me crazy!

Thanks again for the info.

-Leo