How to interpret the direction of rotation from a digital rotary switch with a PIC

How to interpret the direction of rotation from a digital rotary switch with a PIC
The objective for this Instructable is to illustrate how to interface a digital (quadrature coded) rotary switch with a microcontroller. Don't worry, I'll explain what quadrature coded means for us. This interface and the accompanying software will allow the microcontroller to recognize the direction of rotation for each move from one detent to another.

I recently used this type of switch in a microcontroller project that required a pressure set point to be entered using a knob with 16 detents instead of up/down buttons. The idea was to allow the user to "dial in" the desired pressure. As a result, we had to develop a software routine to get the position information from the switch and deduce the rotation direction in order to increment or decrement the pressure set point for the main system.

In this Instructable, I'll cover the physical interface to the microcontroller, the theory of operation for the rotary switch, the theory of operation for the software as well as the deduction routine. Finally, I'll show you my application of the deduction routine. As we progress, I'll try to keep things somewhat generic so that the idea can be applied on as many platforms as possible but I'll also share what I did so you can see a specific application.
 
Remove these adsRemove these ads by Signing Up
 

Step 1Parts

Parts
In order to implement this, you'll need:

A rotary switch (quadrature coded)
Pull up resistors
Suitable microcontroller platform

For my project, I used a Grayhill 61C22-01-04-02 optical encoder. The data sheet for the rotary switch calls for 8.2k ohm pull up resistors on the two data lines coming from the switch. You'll want to check the data sheet for the encoder you opt to use. The rotary switch I used can also be ordered with an axial push button switch. It's a useful feature for committing selections that have been dialed in, etc. but I will not be discussing its interface here. I have a "suitable microcontroller platform" listed because (I think) this can be implemented on more than one platform. I have seen a lot of people using other microcontrollers for Instructables so I want to show the general approach as well. I wrote all the code in PIC Basic Pro for use with a Microchip PIC16F877A. Really, the key thing that you need on the microcontroller is the ability to interrupt when there is a logic change on either of two pins. On the PIC16F877A, this is called the PORTB change interrupt. There may be other names for it on other controllers. This microcontroller interrupt feature is part of what makes this implementation so elegant.
« Previous StepDownload PDFView All StepsNext Step »
7 comments
Sep 1, 2009. 10:08 AMdennisjss says:
I was curious about your selection of the encoder. It's obviously a high quality encoder at $25 a pop. Does is have a nicer feel than other encoders? Do you think it's really worth the $25 as compared to cheaper alternatives?
Aug 27, 2009. 7:51 PMfrollard says:
Wonderful description - a bit advanced for me (I'm stuck with arduino for now). Good job on getting tagged on hackaday.
Aug 27, 2009. 11:12 PMfrollard says:
*to qualify that, microcontrollers are advanced for me - I can learn C, but learning a new language WHILE learning hardware is a bit overwhelming.
Aug 27, 2009. 11:09 PMfrollard says:
I specifically have a job in mind for this - and plan to integrate it with my arduino, as soon as I get a hold of an encoder. Only one I have is one out of an old ball mouse - with copper contacts!!! I have made a program that creates 4-bit (16 colour) pwm on all 8 pins of a shift register. Huge to compute, but simple on electronics. Then I'll make a turntable with leds around it - the sine-wave of brightness will match the motion of the wheel - and the whole thing will act as an input to the computer, like a scroll wheel.
Aug 27, 2009. 5:30 PMcalmconviction says:
very well explained and illustrated. thanks hw

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
0
Followers
1
Author:hw640