Servo motor connection tutorial

From Sim Innovations Wiki
Jump to navigation Jump to search


Servo motors require a PWM signal to set the position. Therefore these motors can only be controlled by PWM pins, with the hw_output_pwm_add API function. Have a look at the Hardware id list to see which pins can be used for PWM. Generally servo motors require a 50 Hz signal, except for some digital servo's. We recommend to look at the specifications of your servo motor to find the correct frequency.

The duty cycle is used to set the position, this duty cycle value runs from 0 to 1.

Info Servo motors are not stable, can be jerky, and are influenced by temperature and electrical noise. If you require precise motor control, then we recommend to use a stepper motor.


In this example we use a Towerpro SG90 servo motor. This is one of the most common cheap servo motors on the market, not necessarily the best one.

Engine RPM indicator

In this example we use the engine RPM from X-Plane to control the servo over 180 degrees. Note that the duty cycle (and possibly frequency) might be different for your servo motor.

-- Add the servo motor to the script
pwm_engine_rpm = hw_output_pwm_add("RPM servo", 50, 0.02)

-- In this case the duty cycle ranges from 0.02 to 0.10
-- We have an engine that goes from 0 to 2500 RPM
function new_rpm(rpm)

    hw_output_pwm_duty_cycle(pwm_engine_rpm, 0.02 + ((0.10 - 0.02) / 2500) * rpm[1])


xpl_dataref_subscribe("sim/cockpit2/engine/indicators/engine_speed_rpm", "FLOAT[8]", new_rpm)