Connecting a switch to the Raspberry Pi

From Sim Innovations Wiki
Jump to navigation Jump to search

Switches have one or multiple solid states. This means that when the switch is set to a position, it will stay in that position until you operate the switch. For the switch to register an input, the I/O pin(s) have to be pulled to ground (GND). That means that one wire goes to ground (GND), and the other wires are attached to the I/O pins. You can find the switch API here: hw_switch_add

An example for a switch is turning on and off the avionics master switch. We will show a couple of examples.

Rocker switch example

An example of how to connect one switch.

In the first example we will use a 2 states rocker switch to turn on and off the avionics master. You can easily change it to a different function, or have two switches, one for the avionics master and one for the gear. The example switch either connects blue and black, or red and black. The blue I/O wire is connected to pin 10, the red I/O wire is connected to pin 16, and the black ground (GND) wire is connected to pin 14.

-- This function is called every time the switch has a new position
function switch_callback(position)
  -- The following print function is not necessary but can help you to verify the state of the switch
  print("The switch got changed to position " .. position)

  -- Turn the avionics master on and off. The first position is 0.
  if position == 0 then
    xpl_command("sim/systems/avionics_off")
    fsx_event("AVIONICS_MASTER_SET", 0)
  elseif position == 1 then
    xpl_command("sim/systems/avionics_on")
    fsx_event("AVIONICS_MASTER_SET", 1)
  end

end

-- Bind to Raspberry Pi pin 10 and 16
hw_switch_add("RPI_V2_P1_10", "RPI_V2_P1_16", switch_callback)

Rotary switch example

An example of how to connect a rotary switch with 4 positions.

A rotary switch (not to be confused with a rotary encoder!) has multiple positions. In this example we will make a fuel tank selector which has 4 positions: OFF, LEFT, RIGHT, ALL. The rotary switch in the wiring example has many more states, but we will just use 4. The ground (GND) wire is the black wire going to pin 14, blue is connected to pin 10, red to pin 12, yellow to pin 16 and green to pin 18. Having the rotary switch in the first position will connect ground (GND) to blue,(rotating counter clockwise) the second position ground (GND) to red, etc...

-- This function is called every time the switch has a new position
function switch_callback(position)
  -- The following print function is not necessary but can help you to verify the state of the switch
  print("The switch got changed to position " .. position)

  -- Switch between various fuel tank positions. The first position is 0, with 4 positions this makes the last position to be number 3.
  if position == 0 then
    xpl_command("sim/fuel/fuel_selector_none")
    fsx_event("FUEL_SELECTOR_OFF")
  elseif position == 1 then
    xpl_command("sim/fuel/fuel_selector_lft")
    fsx_event("FUEL_SELECTOR_LEFT")
  elseif position == 2 then
    xpl_command("sim/fuel/fuel_selector_rgt")
    fsx_event("FUEL_SELECTOR_RIGHT")
  elseif position == 3 then
    xpl_command("sim/fuel/fuel_selector_all")
    fsx_event("FUEL_SELECTOR_ALL")
  end

end

-- Bind to Raspberry Pi pin 10, 12, 16 and 18
hw_switch_add("RPI_V2_P1_10", "RPI_V2_P1_12", "RPI_V2_P1_16", "RPI_V2_P1_18", switch_callback)