Instructables

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
whoop_john9 months ago

Useful, thanks, but it is confusing. You don't declare any variables and there is no main routine code. Could we have a complete example?

dennisjss5 years ago
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?
hw640 (author)  dennisjss5 years ago
This interface was part of a MUCH larger project for one of my system design classes. We had a project sponsor (a licensed PE) who I tasked with selecting and purchasing the encoder since I had absolutely no experience in that area at the time. He said he had used a similar encoder in a previous project and would check things out. He came back with that one. That said, I didn't actually select that particular encoder. What I had originally spec'd was a digital knob with detents, a push button and no stops - unlike a potentiometer. This encoder fit the requirements. Through all the testing and requirements verification, the encoder was very reliable so the quality does appear to be very high. The feel is just barely too "easy" for me (especially with a knob attached) but it's not bad. I think there is a "high torque" model available for order if you're looking to put a little more effort into turning the knob. I would imagine the cheaper encoders would do as good of a job especially for this application as it's relatively slow but I think this switch will shine more in the long term reliability arena. I'm speculating though. Past this point, I think I may be out of my league. Hope this helps.
frollard5 years ago
Wonderful description - a bit advanced for me (I'm stuck with arduino for now). Good job on getting tagged on hackaday.
hw640 (author)  frollard5 years ago
Thanks! I wasn't expecting that at all! If you ever get adventurous and start playing with interrupts, this is a good application for them so don't be too scared: just apply the concepts.
frollard hw6405 years ago
*to qualify that, microcontrollers are advanced for me - I can learn C, but learning a new language WHILE learning hardware is a bit overwhelming.
frollard hw6405 years ago
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.
very well explained and illustrated. thanks hw