Stepper motor connection tutorial

From Sim Innovations Wiki
Jump to navigation Jump to search


A stepper motor, also known as step motor or stepping motor, is a brushless DC electric motor that divides a full rotation into a number of equal steps. The motor's position can then be commanded to move and hold at one of these steps without any position sensor for feedback. This is both a pro and a con. Their position is very precise, but the motor has no clue about where this position is. This is different in comparison with servo motors, which do know their position, but are usually less precise.

Because the motor has no clue about its position, the 'zero' (starting) position has to be set before the motor is used. This can be done in various ways, a hall sensor, light sensor, or in case of the x25 and x27 motors, by turning them into one direction for a certain period of time and then taking that position as its starting position.


x27 / x25 motors

x27 / x25 pinout

These motors were initially build by Juken Swiss Technology, but are now available in cheap clones, and can be bought for less than $5 per motor. The range is 315 degrees, which means they have a mechanical stop. This mechanical stop can be used to set the starting position. By letting the motor run into its stop, you know that this is the starting position and from there on you can start setting the position.

In this example we will make a Cessna 172 engine RPM indicator going from 0 RPM to 3500 RPM. The 0 RPM position will be at 0 degrees, the 3500 RPM position will be at 315 degrees (one complete mechanical rotation). You can change all these numbers to your liking.

stepper_rpm = hw_stepper_motor_add("Engine RPM", "4WIRE_4STEP", 945, 30)

-- Initialization starting, rotate clockwise for 3 seconds
hw_stepper_motor_position(stepper_rpm, nil, "ENDLESS_CLOCKWISE")

timer_start(3000, function(count)
    hw_stepper_motor_calibrate(stepper_rpm, 0.0)
    hw_stepper_motor_position(stepper_rpm, 0.0)

    local function new_rpm_xpl(rpm)
      engine_rpm = var_cap(rpm[1], 0, 3500)
      -- The stepper motor position is set as a value from 0 to 1, so we have to convert the RPM to a position.
      -- Therefore we divide 1 by 3500, this is then multiplied with the current RPM.
      hw_stepper_motor_position(stepper_rpm, 1 / 3500 * engine_rpm)

    xpl_dataref_subscribe("sim/cockpit2/engine/indicators/prop_speed_rpm", "FLOAT[8]", new_rpm_xpl)