PayPal: View Cart

Project:  Rotary Encoder

The rotary quadrature encoder is a device that looks like a mechanical potentiometer but uses little mechanical switches or LED’s and optics to detect the rotation of the shaft.  There are two kinds of encoders: mechanical and optical.  Mechanical encoders are much cheaper but are generally not as accurate as optical encoders.  There are also two types of data outputs: absolute and incremental.  More information can be found on Wikipedia.


The encoder most commonly used in hobby projects is a mechanical, incremental encoder.  There are two outputs: A and B.  The direction of the rotation is determined by the change in logic state of out put pins A and B.

Device = 18F4620                 ' Assign CPU device type

Clock = 40                       ' Set effective clock frequency

Config osc = hspll               ' Set clock mode to 4x PLL

#option usart_brgh = true

#option usart_brg16 = true

#option RX_BUFFER_SIZE = 100      ' Set serial port rx buffer size

'=====[ Libraries Included ]====================================================

Include "USART.bas"              ' UART subroutines

'=====[ Variables ]=============================================================

Public Dim encoder_last_a   As Bit

Public Dim encoder_last_b   As Bit

Public Dim encoder_now_a    As Bit

Public Dim encoder_now_b    As Bit

'=====[ Physical Pins ]=========================================================

Public Dim button_l   As PORTD.0   ' Rotary encoder Left

Public Dim button_r   As PORTD.1   ' Rotary encoder Right

Public Dim button_pb  As PORTD.2   ' Rotary encoder Push Button

'=====[ Serial Port ]===========================================================

SetBaudrate (br9600)

'=====[ Main Program ]==========================================================


   '--- Detect if the encoder has rotated --------------------------------------

   encoder_now_a = button_l

   encoder_now_b = button_r

   If encoder_now_a <> encoder_last_a Or encoder_now_b <> encoder_last_b Then

      If encoder_last_a=1 And encoder_last_b=1 And encoder_now_a=0 And encoder_now_b=1 Then

         USART.Write ("Turn right",13,10)


      If encoder_last_a=0 And encoder_last_b=1 And encoder_now_a=1 And encoder_now_b=1 Then

         USART.Write ("Turn left",13,10)



   ' Do nothing


   '--- Detect if encoder's push button is pressed -----------------------------

   If button_pb = 0 Then                       ' Encoder push button pressed

      USART.Write ("Push button",13,10)

      While button_pb=0  DelayMS(10)  Wend     ' Debounce button in software



   encoder_last_a = encoder_now_a

   encoder_last_b = encoder_now_b

   DelayMS (1)

GoTo main_loop   


The picture above shows the HamStack rotary encoder installed on a PCB.

In the simple example below, when the knob is rotated to the left, the program will send the message “Turn left” out the serial port.  Turning the knob right will cause it to send “Turn right” and when pressing the encoder knob in, the program sends “Push button”.   

Example written in Swordfish Basic

The HamStack Rotary Encoder kit includes the encoder, PCB, knob, pullup resistors and header connector.

HamStack Rotary Encoder kit

Includes the encoder, PCB, knob, pullup resistors and header connector.

This example uses a mechanical rotary encoder made by Panasonic.  The model EVE-JBBF2020B.

It provides 20 detented positions per turn of the shaft. The encoder is connected to three of the HamStack CPU pins.  Each pin should have a 10k pull up resistor to +5v.

This view shows the encoder board as viewed from the top.

All 4 resistors are 10k ¼ watt.  The two “vertical” resistors on the diagram are current limiting resistors in series with the left and right direction input.  These can be replaced with a wire.  The two “horizontal” resistors are the pullups and must be installed.

The 5 I/O pins at the top are, from left to right...

“5”  +5v input

“L”  This is the left rotation output or the “A” output

“B”  This is the encoder push button switch closure

“R”  This is the right rotation output or the “B” output

“G”  Ground