Potentiometer connection tutorial

From Sim Innovations Wiki
Jump to navigation Jump to search

Description

There are two types of potentiometers: slide (linear) and thumb (rotating). Both create an analog signal, which is a voltage that ranges from ground (GND) to 5 volt. The analog inputs on the Arduino can measure this voltage and turn it into a digital value. This is done with a system which is called 'ADC': Analog-to-Digital Converter. A voltage equal to ground (GND) will be represented as 0, a voltage equal to 5 volt will be 1. All voltages in between will be a value between 0 and 1, so placing your potentiometer at half the range will give a digital value of 0.5. With this you can for example create a throttle input.

Info Note that the Raspberry Pi has no ADC! You will not be able to connect a potentiometer to a Raspberry Pi.
Two potentiometers connected to an Arduino Nano

Connecting a potentiometer

A potentiometer will be a resistor between ground and 5 volt, so make sure the resistance is not too low, or you will have a shortcircuit. We recommend to use a potentiometer of at least 250Ω (Ohm).

Potentiometers have 3 connections. One of the outer pins has to be connected to ground, the other one has to be connected to 5 volt. The middle connection is the pick-up, this is the pin that puts out a voltage from ground to 5 volt and has to be connected to one of the analog pins of the Arduino. All pins with an A are analog pins, so for example A0 or A7.

Example

We will show you an example script on how to connect a potentiometer for a throttle input. For this we use the analog input API functions

Throttle

-- For X-Plane we need to override the joystick input first
xpl_dataref_write("sim/operation/override/override_joystick", "INT", 1)

-- Callback function which is called when the ADC input state changes
-- 0.0 : GND (lowest voltage)
-- 1.0 : VCC (highest voltage)
function adc_input_throttle(value)
  -- FSX and Prepar3D expect a value between 0 and 100
  fsx_variable_write("GENERAL ENG THROTTLE LEVER POSITION:1", "Percent", value * 100)

  -- X-Plane expects a value between 0 and 1, no conversion necessary
  xpl_dataref_write("sim/flightmodel/engine/ENGN_thro", "FLOAT[8]", {value})
end

-- Create a ADC input
hw_adc_input_add("Throttle", adc_input_throttle)