Monday, October 8, 2012

Rotary Encoder Interfacing with PIC Mirocontroller

       i am currently working with some power supply design and i can say using conventional pots(potentiometer)  and rotary  switch  to adjust the voltage and other stuff is quite old school. so i have decided to go for a bit high tech , actually bit digital.
so here is the solution

Incremental Rotary Encoder
first of all i would like to tell you , these type of rotary encoder is totally digital component so you can't directly replace these  with you conventional pots. so lets start what are Incremental Rotary Encoder , Incremental rotary encoders by the looks of it just like a general purpose pot looks like but output is quite different they  provide a pair of digital signals that allow a microcontroller to determine the direction of a shaft’s rotation. They can be used to monitor motors and mechanisms, or to provide a control-knob user interface.

   for a quick look here is a typical incremental rotary encoder

and hear the output wave form

Decoding with Microcontroller 

in this examples we will be decoding the rotary encoder with the help of sate machine.

view of state machine in the wave form

Schematic with PIC18F458

Source code and firmware

CLIK Here To download Source Code and Firmware. 

if you have any problem please leave in the comment section.


  1. hello gaurav
    please post the uc code for interfacing rotary encoder

  2. if possible use atmel mega x chips

  3. This code is incomplete, you must do the revision. Program increments the variable just to pin 4 and pin 5, with no requirement to complete full cycles of 10,11,01,00. These can lead to an incorrect reading of the encoder.

  4. See before going to use any encoder to interface with the micro find the PPR (Pulse Per Revolution) of the encoder.
    The encoder has only 4 states 11,01,00,10 i.e in hex 3,1,0,2 ( Every make has its own state )
    So 1st read the state of the encoder, then to detect whether the direction is cw and ccw use the logic.
    After the logic Either Increment the counts or, decrement the counts.

    Its Simple once you interface its quite easy to handle.

  5. To use XC8 compiler, which part of the code needs to be changed. ?

  6. This comment has been removed by the author.