Rotary encoder connection tutorial

From Sim Innovations Wiki
Jump to navigation Jump to search

Description

A rotary encoder is an (infinite) rotatable dial. The callback returns -1 for a counter-clockwise rotations and 1 for a clockwise rotations. Some examples of what we can create with a rotary encoder are an altimeter dial, frequency dial or an autopilot heading dial. In the example below we will create an altimeter dial, with the use of the hw_dial_add API function. Rotary encoders come in different number of detents, this means the number of pulses per step/click. If your rotary encoder gives too many callbacks per step/click, try a different type setting.

Example

Altimeter

function dial_altimeter_callback(direction)
  if direction == 1 then
    fsx_event("KOHLSMAN_INC")
    xpl_command("sim/instruments/barometer_up")
  elseif direction == -1 then
    fsx_event("KOHLSMAN_DEC")
    xpl_command("sim/instruments/barometer_down")
  end
end

-- Create a rotary encoder for the altimeter pressure setting
hw_dial_add("Altimeter", dial_altimeter_callback)

That is all there is to it. If your altimeter pressure setting is going in the wrong direction, then just swap the pin assignments in Air Manager.

Hardware setup

Connecting a rotary encoder is also very easy. Rotary encoders have an A, B and ground pin. A and B go to the I/O of your hardware device, and ground of course goes to ground (GND). Look at this schematic example below.

Rotaryencoderschematic.png